Here you can find the source of getUniqueValue(Collection values, String initValue)
initValue
unique among the String values contained in values
by suffixing it with a decimal digit suffix.
public static String getUniqueValue(Collection values, String initValue)
//package com.java2s; /*/*from w w w .j ava 2s .com*/ * Copyright 2001-2004 The Apache Software Foundation. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import java.util.Collection; public class Main { /** * Makes the value passed in <code>initValue</code> unique among the {@link String} values contained in * <code>values</code> by suffixing it with a decimal digit suffix. */ public static String getUniqueValue(Collection values, String initValue) { if (!values.contains(initValue)) { return initValue; } else { StringBuffer unqVal = new StringBuffer(initValue); int beg = unqVal.length(), cur, end; while (Character.isDigit(unqVal.charAt(beg - 1))) { beg--; } if (beg == unqVal.length()) { unqVal.append('1'); } cur = end = unqVal.length() - 1; while (values.contains(unqVal.toString())) { if (unqVal.charAt(cur) < '9') { unqVal.setCharAt(cur, (char) (unqVal.charAt(cur) + 1)); } else { while (cur-- > beg) { if (unqVal.charAt(cur) < '9') { unqVal.setCharAt(cur, (char) (unqVal.charAt(cur) + 1)); break; } } // See if there's a need to insert a new digit. if (cur < beg) { unqVal.insert(++cur, '1'); end++; } while (cur < end) { unqVal.setCharAt(++cur, '0'); } } } return unqVal.toString(); } /* For else clause of selection-statement If(!values ... */ } }