/**
 * Copyright 2012 Netflix, Inc.
 * 
 * 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.
 */
package com.netflix.hystrix;

import java.util.concurrent.ConcurrentHashMap;

/**
 * A key to represent a {@link HystrixThreadPool} for monitoring, metrics publishing, caching and other such uses.
 * <p>
 * This interface is intended to work natively with Enums so that implementing code can be an enum that implements this interface.
 */
public interface HystrixThreadPoolKey {

    /**
     * The 'key' used as the name for a thread-pool.
     * <p>
     * The word 'name' is used instead of 'key' so that Enums can implement this interface and it work natively.
     * 
     * @return String
     */
    public String name();

    public static class Factory {

        private Factory() {
        }

        // used to intern instances so we don't keep re-creating them millions of times for the same key
        private static ConcurrentHashMap<String, HystrixThreadPoolKey> intern = new ConcurrentHashMap<String, HystrixThreadPoolKey>();

        /**
         * Retrieve (or create) an interned HystrixThreadPoolKey instance for a given name.
         * 
         * @param name thread pool name
         * @return HystrixThreadPoolKey instance that is interned (cached) so a given name will always retrieve the same instance.
         */
        public static HystrixThreadPoolKey asKey(String name) {
            HystrixThreadPoolKey k = intern.get(name);
            if (k == null) {
                k = new HystrixThreadPoolKeyDefault(name);
                intern.putIfAbsent(name, k);
            }
            return k;
        }

        private static class HystrixThreadPoolKeyDefault implements HystrixThreadPoolKey {

            private String name;

            private HystrixThreadPoolKeyDefault(String name) {
                this.name = name;
            }

            @Override
            public String name() {
                return name;
            }
            
            @Override
            public String toString() {
            	return name;
            }
        }

        /* package-private */ static int getThreadPoolCount() {
            return intern.size();
        }
    }
}
@@ -1422,16 +1422,22 @@
  static 
+final 
 Concurre
@@ -1937,17 +1937,27 @@
 PoolKey 
-k
+existingKey
  = inter
@@ -1985,13 +1985,71 @@
     
-if (k
+HystrixThreadPoolKey newKey = null;%0A            if (existingKey
  == 
@@ -2072,17 +2072,22 @@
         
-k
+newKey
  = new H
@@ -2135,16 +2135,30 @@
         
+ existingKey =
  intern.
@@ -2175,17 +2175,22 @@
 t(name, 
-k
+newKey
 );%0A     
@@ -2221,10 +2221,52 @@
 urn 
-k;
+existingKey != null ? existingKey : newKey;%0A
 %0A   
@@ -2385,16 +2385,22 @@
 private 
+final 
 String n
@@ -2857,8 +2857,9 @@
 %0A    %7D%0A%7D
+%0A

Fix race condition in HystrixThreadPoolKey.asKey method