Java Now makeUniqueName(final String[] knownNames, final String pattern)

Here you can find the source of makeUniqueName(final String[] knownNames, final String pattern)

Description

Computes a unique name using the given known-names array as filter.

License

Open Source License

Parameter

Parameter Description
knownNames the list of known names.
pattern the name pattern, which should have one integer slot to create derived names.

Return

the unique name or null, if no unqiue name could be created.

Declaration

public static String makeUniqueName(final String[] knownNames, final String pattern) 

Method Source Code


//package com.java2s;
/*!/* w w w  .j  a v  a  2s  .  c o m*/
* This program is free software; you can redistribute it and/or modify it under the
* terms of the GNU Lesser General Public License, version 2.1 as published by the Free Software
* Foundation.
*
* You should have received a copy of the GNU Lesser General Public License along with this
* program; if not, you can obtain a copy at http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html
* or from the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Lesser General Public License for more details.
*
* Copyright (c) 2002-2013 Pentaho Corporation..  All rights reserved.
*/

import java.text.Format;
import java.text.MessageFormat;
import java.util.HashSet;

public class Main {
    /**
     * Computes a unique name using the given known-names array as filter. This method is not intended for large
     * datasets.
     *
     * @param knownNames the list of known names.
     * @param pattern    the name pattern, which should have one integer slot to create derived names.
     * @return the unique name or null, if no unqiue name could be created.
     */
    public static String makeUniqueName(final String[] knownNames, final String pattern) {
        final HashSet<String> knownNamesSet = new HashSet<String>(knownNames.length);
        for (int i = 0; i < knownNames.length; i++) {
            final String name = knownNames[i];
            knownNamesSet.add(name);
        }

        final MessageFormat message = new MessageFormat(pattern);
        final Object[] objects = { "" };
        final String plain = message.format(objects);
        if (knownNamesSet.contains(plain) == false) {
            return plain;
        }

        final Format[] formats = message.getFormats();
        if (formats.length == 0) {
            // there is no variation in this name.
            return null;
        }

        int count = 1;
        while (count < 2000000) {
            objects[0] = String.valueOf(count);
            final String testFile = message.format(objects);
            if (knownNamesSet.contains(testFile) == false) {
                return testFile;
            }
            count += 1;
        }

        return null;
    }
}

Related

  1. getTimeNow(Date theTime)
  2. getTimeNowAsString(String pattern)
  3. getTimeSubFromNowTime(String time)
  4. getValidDate(String unknownDate)
  5. isNowInInterval(String start, String end)
  6. now()
  7. now()
  8. now()
  9. now()