Class UniqueNameGenerator
java.lang.Object
com.amalgamasimulation.desktop.appelements.utils.UniqueNameGenerator
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 TypeMethodDescriptionstatic longfindHighestSuffix(Iterable<? extends org.eclipse.emf.ecore.EObject> objects, String pattern) Finds the highest number in the identifiers whose name base matches pattern.static longfindHighestSuffix(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.EAttributefindIdAttribute(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.EAttributefindNameAttribute(org.eclipse.emf.ecore.EClass eClass) Finds the first attribute named "name" in the class or its supertypes.static StringidOf(org.eclipse.emf.ecore.EObject object) Returns the value of the object's string attribute marked as ID.static StringnextName(Iterable<? extends org.eclipse.emf.ecore.EObject> objects, String pattern, String separator) Returns the next identifier based on the ID attributes of objects.static StringnextName(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 StringnextName(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 StringnextName(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 StringnextNameWithPrefix(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 voidSets id on the object's string attribute marked as ID.
-
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 searchedobject- the object for which an identifier is proposedpattern- 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 searchedobject- the object for which an identifier is proposedpattern- the proposed name, with or without a trailing numberseparator- 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 searchedobject- the object for which an identifier is proposedpattern- the proposed name, with or without a trailing numberseparator- the string between the name base and number; may be emptyadditionalObjects- 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 searchedpattern- the proposed name, with or without a trailing numberseparator- 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 searchedobject- the object for which an identifier is proposedpattern- the proposed name, with or without namePrefix and a trailing numberseparator- the string between the name base and number; may be emptynamePrefix- 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 searchedpattern- 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 searchednameFeature- the feature from which each object's name is readpattern- 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
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
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 setid- the new identifier
-