Class UniqueNameGenerator


  • public class UniqueNameGenerator
    extends Object
    Unique name generator. Generates unique names by adding digits to a given prefix.
    • Constructor Detail

      • UniqueNameGenerator

        public UniqueNameGenerator()
    • Method Detail

      • generate

        public String generate​(String namePrefix,
                               Collection<String> existingNames)
        Generate a unique name, based on the given name prefix and a collection of existing names. Default duplicate pattern will be used: .+digit+.

        Name will be generated as following example: if names Name1 and Name2 already exist, name Name3 will be generated.

        Parameters:
        namePrefix - the name prefix to generate name with, cannot be null
        existingNames - the collection of existing names to check uniqueness for, cannot be null
        Returns:
        a unique name, based on the given name prefix and collection of the existing names, never null
      • generate

        public String generate​(String namePrefix,
                               Collection<String> existingNames,
                               String numberFormat,
                               Pattern duplicatePattern)
        Generate unique name, based on the given name prefix and a collection of the existing names.

        Custom duplicate-check pattern will be used. Number format can be used to format generated number, before appending it to the generated name.

        Parameters:
        namePrefix - the name prefix to generate name with, cannot be null
        existingNames - the collection of the existing names to check uniqueness for, cannot be null
        numberFormat - the number format to format generated number, before appending it to the generated name; String#format style format is expected with one and only one %s; can be null if number formatting is not needed
        duplicatePattern - the duplicate-check pattern, cannot be null, must have regexp group with a digit inside it
        Returns:
        a unique name, based on the given name prefix and collection of the existing names, never null
      • formatNumber

        protected String formatNumber​(String numberFormat,
                                      int number)
        Format the generated number to string using the given format.
        Parameters:
        numberFormat - the number format to format generated number, before appending it to the generated name; String#format style format is expected with one and only one %s; can be null if number formatting is not needed
        number - the number to format
        Returns:
        formatted number, never null