Class UniqueNameGenerator

java.lang.Object
com.amalgamasimulation.desktop.appelements.utils.UniqueNameGenerator

public final class UniqueNameGenerator extends Object
Generates string identifiers for EMF objects from a name pattern. The pattern may contain a text base alone (Item) or a base with a trailing number (Item 2). When a name is generated, the result consists of the base, separator, and next number (Item 3). A separate method can prepend a caller-supplied prefix. This class also provides helpers for ID and name attributes. The generator separates a trailing number from the pattern and existing identifiers, finds the highest number with the same base, and returns the next number. If there are no matches, it uses 1. The caller specifies the separator and, when needed, the prefix.
  • Method Summary

    Modifier and Type
    Method
    Description
    static long
    findHighestSuffix(Iterable<? extends org.eclipse.emf.ecore.EObject> objects, String pattern)
    Finds the highest number in the identifiers whose name base matches pattern.
    static long
    findHighestSuffix(Iterable<? extends org.eclipse.emf.ecore.EObject> objects, org.eclipse.emf.ecore.EStructuralFeature nameFeature, String pattern)
    Finds the highest number in values of nameFeature whose name base matches pattern.
    static org.eclipse.emf.ecore.EAttribute
    findIdAttribute(org.eclipse.emf.ecore.EClass eClass)
    Finds the first string attribute marked as ID in the class or its supertypes.
    static org.eclipse.emf.ecore.EAttribute
    findNameAttribute(org.eclipse.emf.ecore.EClass eClass)
    Finds the first attribute named "name" in the class or its supertypes.
    static String
    idOf(org.eclipse.emf.ecore.EObject object)
    Returns the value of the object's string attribute marked as ID.
    static String
    nextName(Iterable<? extends org.eclipse.emf.ecore.EObject> objects, String pattern, String separator)
    Returns the next identifier based on the ID attributes of objects.
    static String
    nextName(org.eclipse.emf.ecore.EObject container, org.eclipse.emf.ecore.EObject object, String pattern)
    Returns the next identifier for object among the objects in its collection in container.
    static String
    nextName(org.eclipse.emf.ecore.EObject container, org.eclipse.emf.ecore.EObject object, String pattern, String separator)
    Returns the next identifier for object among the objects in its collection in container.
    static String
    nextName(org.eclipse.emf.ecore.EObject container, org.eclipse.emf.ecore.EObject object, String pattern, String separator, Iterable<? extends org.eclipse.emf.ecore.EObject> additionalObjects)
    Returns the next identifier for object using the objects in its collection in container and the objects in additionalObjects.
    static String
    nextNameWithPrefix(org.eclipse.emf.ecore.EObject container, org.eclipse.emf.ecore.EObject object, String pattern, String separator, String namePrefix)
    Returns the next identifier for object among the objects in its collection in container and prepends namePrefix to the result.
    static void
    setId(org.eclipse.emf.ecore.EObject object, String id)
    Sets id on the object's string attribute marked as ID.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • nextName

      public static String nextName(org.eclipse.emf.ecore.EObject container, org.eclipse.emf.ecore.EObject object, String pattern)
      Returns the next identifier for object among the objects in its collection in container. This overload uses an empty separator: the number immediately follows the name base, so Item3 is proposed after Item2. The type of object must have a container reference, and the type of container must have its opposite reference to a list of objects. If this relationship or the list is missing, the method returns pattern unchanged. Objects in the list must have a string attribute marked as ID. Without one, their identifiers are ignored; if no object has an ID attribute, the method repeatedly proposes a name ending in 1. It does not check whether object itself has an ID attribute.
      Parameters:
      container - the container whose object list is searched
      object - the object for which an identifier is proposed
      pattern - the proposed name, with or without a trailing number
      Returns:
      the next identifier, or pattern unchanged if no suitable list exists
    • nextName

      public static String nextName(org.eclipse.emf.ecore.EObject container, org.eclipse.emf.ecore.EObject object, String pattern, String separator)
      Returns the next identifier for object among the objects in its collection in container. separator is placed between the name base and number: with " " as separator, Item 3 is proposed after Item 2. An empty separator places the number immediately after the base. The type of object must have a container reference, and the type of container must have its opposite reference to a list of objects. If this relationship or the list is missing, the method returns pattern unchanged. Objects in the list must have a string attribute marked as ID. Without one, their identifiers are ignored; if no object has an ID attribute, the method repeatedly proposes a name ending in 1. It does not check whether object itself has an ID attribute.
      Parameters:
      container - the container whose object list is searched
      object - the object for which an identifier is proposed
      pattern - the proposed name, with or without a trailing number
      separator - the string between the name base and number; may be empty
      Returns:
      the next identifier, or pattern unchanged if no suitable list exists
    • nextName

      public static String nextName(org.eclipse.emf.ecore.EObject container, org.eclipse.emf.ecore.EObject object, String pattern, String separator, Iterable<? extends org.eclipse.emf.ecore.EObject> additionalObjects)
      Returns the next identifier for object using the objects in its collection in container and the objects in additionalObjects. The identifiers from both sources are considered when finding the highest number. separator is placed between the name base and number; an empty separator places the number immediately after the base. The type of object must have a container reference, and the type of container must have its opposite reference to a list of objects. If this relationship or the list is missing, the method returns pattern unchanged and does not inspect additionalObjects. Objects in the container list and additionalObjects must have a string attribute marked as ID. Without one, their identifiers are ignored; if no object has an ID attribute, the method repeatedly proposes a name ending in 1. It does not check whether object itself has an ID attribute.
      Parameters:
      container - the container whose object list is searched
      object - the object for which an identifier is proposed
      pattern - the proposed name, with or without a trailing number
      separator - the string between the name base and number; may be empty
      additionalObjects - objects whose identifiers are also considered
      Returns:
      the next identifier, or pattern unchanged if no suitable list exists
    • nextName

      public static String nextName(Iterable<? extends org.eclipse.emf.ecore.EObject> objects, String pattern, String separator)
      Returns the next identifier based on the ID attributes of objects. separator is placed between the name base and number; an empty separator places the number immediately after the base. Objects without a string attribute marked as ID are ignored. If no matching identifier exists, the method returns a name ending in 1.
      Parameters:
      objects - the objects whose identifiers are searched
      pattern - the proposed name, with or without a trailing number
      separator - the string between the name base and number; may be empty
      Returns:
      the next identifier
    • nextNameWithPrefix

      public static String nextNameWithPrefix(org.eclipse.emf.ecore.EObject container, org.eclipse.emf.ecore.EObject object, String pattern, String separator, String namePrefix)
      Returns the next identifier for object among the objects in its collection in container and prepends namePrefix to the result. Before comparing numbers, the method removes this prefix from pattern and existing identifiers, so it does not add the prefix repeatedly. The type of object must have a container reference, and the type of container must have its opposite reference to a list of objects. If this relationship or the list is missing, the method returns pattern with namePrefix. Objects in the list must have a string attribute marked as ID. Without one, their identifiers are ignored; if no object has an ID attribute, the method repeatedly proposes a name ending in 1. It does not check whether object itself has an ID attribute.
      Parameters:
      container - the container whose object list is searched
      object - the object for which an identifier is proposed
      pattern - the proposed name, with or without namePrefix and a trailing number
      separator - the string between the name base and number; may be empty
      namePrefix - the prefix added to the generated identifier
      Returns:
      the next identifier with namePrefix
    • findHighestSuffix

      public static long findHighestSuffix(Iterable<? extends org.eclipse.emf.ecore.EObject> objects, String pattern)
      Finds the highest number in the identifiers whose name base matches pattern. For pattern "Item", identifiers "Item 2" and "Item 5" produce 5. A trailing number is not removed from pattern. The method uses string attributes marked as ID. If no matching identifier exists, it returns 0.
      Parameters:
      objects - the objects whose identifiers are searched
      pattern - the name base to match against identifiers
      Returns:
      the highest number found, or 0
    • findHighestSuffix

      public static long findHighestSuffix(Iterable<? extends org.eclipse.emf.ecore.EObject> objects, org.eclipse.emf.ecore.EStructuralFeature nameFeature, String pattern)
      Finds the highest number in values of nameFeature whose name base matches pattern. Values are converted to strings. A trailing number is not removed from pattern. If no matching value exists, the method returns 0.
      Parameters:
      objects - the objects whose names are searched
      nameFeature - the feature from which each object's name is read
      pattern - the name base to match against feature values
      Returns:
      the highest number found, or 0
    • findIdAttribute

      public static org.eclipse.emf.ecore.EAttribute findIdAttribute(org.eclipse.emf.ecore.EClass eClass)
      Finds the first string attribute marked as ID in the class or its supertypes.
      Parameters:
      eClass - the class whose attributes are searched
      Returns:
      the ID attribute, or null if none exists
    • findNameAttribute

      public static org.eclipse.emf.ecore.EAttribute findNameAttribute(org.eclipse.emf.ecore.EClass eClass)
      Finds the first attribute named "name" in the class or its supertypes.
      Parameters:
      eClass - the class whose attributes are searched
      Returns:
      the "name" attribute, or null if none exists
    • idOf

      public static String idOf(org.eclipse.emf.ecore.EObject object)
      Returns the value of the object's string attribute marked as ID. The search includes inherited attributes.
      Parameters:
      object - the object whose identifier is read; may be null
      Returns:
      the identifier, or null if object is null, no suitable attribute exists, or its value is null
    • setId

      public static void setId(org.eclipse.emf.ecore.EObject object, String id)
      Sets id on the object's string attribute marked as ID. The search includes inherited attributes. If no suitable attribute exists, the object is unchanged.
      Parameters:
      object - the object whose identifier is set
      id - the new identifier