Java Collection Create createUniqueName(String baseName, Collection names)

Here you can find the source of createUniqueName(String baseName, Collection names)

Description

Generate a unique name based on the given name and the name of already existing elements.

License

Open Source License

Parameter

Parameter Description
baseName - The default name to give.
names - The names to compare with each other.

Return

The newly created name. Of form baseName{int}

Declaration

public static String createUniqueName(String baseName, Collection<String> names) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.util.Collection;

public class Main {
    /**/* ww  w. jav a  2  s. c  om*/
     * Generate a unique name based on the given name and the name of already existing elements.
     *
     * @param baseName - The default name to give.
     * @param names - The names to compare with each other.
     * @return The newly created name. Of form baseName{int}
     */
    public static String createUniqueName(String baseName, Collection<String> names) {
        String uniqueName = baseName;
        int idx = -1;
        for (String name : names) {
            if (!name.startsWith(baseName)) {
                continue;
            }

            String suffix = name.substring(baseName.length());
            if (suffix.isEmpty()) {
                idx = Math.max(idx, 0);
            } else if (suffix.matches("\\d+")) {
                idx = Math.max(idx, Integer.parseInt(suffix));
            }
        }
        if (idx < 0) {
            return uniqueName;
        }
        return uniqueName + (idx + 1);
    }
}

Related

  1. createNominalDescription(Collection values)
  2. createParameterizedWhereClause(Collection columns)
  3. createStringCollection(Class clazz, String... elements)
  4. createStringFromCollection(Collection svcInterfaces)
  5. createTextFromList(Collection list)
  6. stringToCollection(String string)
  7. stringToCollection(String string)
  8. stringToCollection(String string, String delim)
  9. toCollection(C c, E... elements)