Java Collection Unique getUniqueValue(Collection values, String initValue)

Here you can find the source of getUniqueValue(Collection values, String initValue)

Description

Makes the value passed in initValue unique among the String values contained in values by suffixing it with a decimal digit suffix.

License

Apache License

Declaration

public static String getUniqueValue(Collection values, String initValue) 

Method Source Code

//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 ... */

    }
}

Related

  1. generateUniqueName(String aName, Collection aStringCollection)
  2. getUnique(Collection c)
  3. getUnique(Collection collection)
  4. getUniqueName(String name, Collection collection)
  5. getUniqueNameWithNumbers(Collection names, String baseName)
  6. hasUniqueObject(Collection collection)
  7. hasUniqueObject(Collection collection)
  8. hasUniqueObject(Collection collection)
  9. unique(Collection c)