content: /*
   * Licensed to the Apache Software Foundation (ASF) under one or more
   * contributor license agreements.  See the NOTICE file distributed with
   * this work for additional information regarding copyright ownership.
   * The ASF licenses this file to You 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.alibaba.metrics;
  
  import java.util.Set;
  import java.util.concurrent.ConcurrentHashMap;
  import java.util.concurrent.ConcurrentMap;
  
  /**
   * A map of shared, named metric registries.
   */
  public class SharedMetricRegistries {
      private static final ConcurrentMap<String, MetricRegistry> REGISTRIES =
              new ConcurrentHashMap<String, MetricRegistry>();
  
      private SharedMetricRegistries() { /* singleton */ }
  
      public static void clear() {
          REGISTRIES.clear();
      }
  
      public static Set<String> names() {
          return REGISTRIES.keySet();
      }
  
      public static void remove(String key) {
          REGISTRIES.remove(key);
      }
  
      public static MetricRegistry add(String name, MetricRegistry registry) {
          return REGISTRIES.putIfAbsent(name, registry);
      }
  
      public static MetricRegistry getOrCreate(String name) {
          final MetricRegistry existing = REGISTRIES.get(name);
          if (existing == null) {
              final MetricRegistry created = new MetricRegistryImpl();
              final MetricRegistry raced = add(name, created);
              if (raced == null) {
                  return created;
              }
              return raced;
          }
          return existing;
      }
  }
  