001/*
002 * SPDX-FileCopyrightText: none
003 * SPDX-License-Identifier: CC0-1.0
004 */
005
006package gov.nist.secauto.metaschema.databind.codegen;
007
008import org.apache.xmlbeans.impl.common.NameUtil;
009
010import java.util.Map;
011
012import edu.umd.cs.findbugs.annotations.NonNull;
013
014/**
015 * A variety of utility methods for normalizing Java class related names.
016 */
017public final class ClassUtils {
018  private static final Map<String, String> JAVA_NAME_MAPPER = Map.ofEntries(
019      Map.entry("Class", "Clazz"));
020
021  private ClassUtils() {
022    // disable construction
023  }
024
025  /**
026   * Transforms the provided name into a string suitable for use as a Java
027   * property name.
028   *
029   * @param name
030   *          the name of an information element definition
031   * @return a Java property name
032   */
033  @SuppressWarnings("null")
034  @NonNull
035  public static String toPropertyName(@NonNull String name) {
036    String property = NameUtil.upperCamelCase(name);
037    return JAVA_NAME_MAPPER.getOrDefault(property, property);
038  }
039
040  /**
041   * Transforms the provided name into a string suitable for use as a Java
042   * variable name.
043   *
044   * @param name
045   *          the name of an information element definition
046   * @return a Java variable name
047   */
048  @SuppressWarnings("null")
049  @NonNull
050  public static String toVariableName(@NonNull String name) {
051    return NameUtil.lowerCamelCase(name);
052  }
053
054  /**
055   * Transforms the provided name into a string suitable for use as a Java class
056   * name.
057   *
058   * @param name
059   *          the name of an information element definition
060   * @return a Java variable name
061   */
062  @SuppressWarnings("null")
063  @NonNull
064  public static String toClassName(@NonNull String name) {
065    return NameUtil.upperCamelCase(name, false);
066  }
067
068  /**
069   * Transforms the provided name into a string suitable for use as a Java package
070   * name.
071   *
072   * @param name
073   *          the name of an information element definition
074   * @return a Java variable name
075   */
076  @SuppressWarnings("null")
077  @NonNull
078  public static String toPackageName(@NonNull String name) {
079    return NameUtil.getPackageFromNamespace(name, false);
080  }
081}