Java AtomicInteger getThreadName(String pattern, String name)

Here you can find the source of getThreadName(String pattern, String name)

Description

Creates a new thread name with the given prefix

License

Apache License

Parameter

Parameter Description
pattern the pattern
name the name

Return

the thread name, which is unique

Declaration

public static String getThreadName(String pattern, String name) 

Method Source Code

//package com.java2s;
/*//from www  .j  a va  2  s.c o m
 * Copyright 2010-2011 the original author or authors.
 *
 * 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.concurrent.atomic.AtomicInteger;

public class Main {
    public static final String DEFAULT_PATTERN = "StarFlow Thread ${counter} - ${name}";
    private static AtomicInteger threadCounter = new AtomicInteger();

    /**
     * Creates a new thread name with the given prefix
     *
     * @param pattern the pattern
     * @param name    the name
     * @return the thread name, which is unique
     */
    public static String getThreadName(String pattern, String name) {
        if (pattern == null) {
            pattern = DEFAULT_PATTERN;
        }

        // the name could potential have a $ sign we want to keep
        if (name.indexOf("$") > -1) {
            name = name.replaceAll("\\$", "CAMEL_REPLACE_ME");
        }

        // we support ${longName} and ${name} as name placeholders
        String longName = name;
        String shortName = name.contains("?") ? before(name, "?") : name;

        String answer = pattern.replaceFirst("\\$\\{counter\\}", "" + nextThreadCounter());
        answer = answer.replaceFirst("\\$\\{longName\\}", longName);
        answer = answer.replaceFirst("\\$\\{name\\}", shortName);
        if (answer.indexOf("$") > -1 || answer.indexOf("${") > -1 || answer.indexOf("}") > -1) {
            throw new IllegalArgumentException("Pattern is invalid: " + pattern);
        }

        if (answer.indexOf("CAMEL_REPLACE_ME") > -1) {
            answer = answer.replaceAll("CAMEL_REPLACE_ME", "\\$");
        }

        return answer;
    }

    private static String before(String text, String before) {
        if (!text.contains(before)) {
            return null;
        }
        return text.substring(0, text.indexOf(before));
    }

    private static int nextThreadCounter() {
        return threadCounter.getAndIncrement();
    }
}

Related

  1. getRandomTenantId()
  2. getReply()
  3. getSerialNum()
  4. getTempFileName()
  5. getThreadDumpId()
  6. getTimeBasedUUID()
  7. getUniqueId()
  8. getUuidAsFourCharacterGroups()
  9. increase(A a, Map map)