Java Collection Element Get getComponentName(Collection existingNames, String name)

Here you can find the source of getComponentName(Collection existingNames, String name)

Description

Create a unique name for an unnamed component

License

Apache License

Parameter

Parameter Description
existingNames Existing collection of names to test the new name against
name The name to test

Declaration

public static String getComponentName(Collection<String> existingNames, String name) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.util.Collection;

public class Main {
    /**/*from w w  w  . ja v  a  2 s.  co  m*/
     * Create a unique name for an unnamed component
     *
     * @param existingNames
     *            Existing collection of names to test the new name against
     * @param name
     *            The name to test
     */
    public static String getComponentName(Collection<String> existingNames, String name) {
        if (existingNames.contains(name)) {
            int n = 2;
            while (existingNames.contains(name + " (" + n + ")")) {
                n++;
            }

            return name + " (" + n + ")";
        } else {
            return name;
        }
    }
}

Related

  1. getCommaSeparatedString(Collection values)
  2. getCommaSeparatedStringFromCollection(Collection collections)
  3. getCommon(Collection c1, Collection c2)
  4. getCommonElements( Collection from, Collection to)
  5. getCommonSuffix(Collection c)
  6. getDocIdString(Collection docIds)
  7. getElement(final int index, final Collection coll)
  8. getElementClass(Collection collection)
  9. getElementFromSize1(Collection collection)