001/**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hdfs.util;
019
020import java.util.HashMap;
021import java.util.Map;
022
023import org.apache.hadoop.classification.InterfaceAudience;
024import org.apache.hadoop.classification.InterfaceStability;
025
026import com.google.common.annotations.VisibleForTesting;
027import com.google.common.collect.ImmutableList;
028
029/**
030 * Class for de-duplication of instances. <br>
031 * Hold the references count to a single instance. If there are no references
032 * then the entry will be removed.<br>
033 * Type E should implement {@link ReferenceCounter}<br>
034 * Note: This class is NOT thread-safe.
035 */
036@InterfaceAudience.Private
037@InterfaceStability.Evolving
038public class ReferenceCountMap<E extends ReferenceCountMap.ReferenceCounter> {
039
040  private Map<E, E> referenceMap = new HashMap<E, E>();
041
042  /**
043   * Add the reference. If the instance already present, just increase the
044   * reference count.
045   * 
046   * @param key Key to put in reference map
047   * @return Referenced instance
048   */
049  public E put(E key) {
050    E value = referenceMap.get(key);
051    if (value == null) {
052      value = key;
053      referenceMap.put(key, value);
054    }
055    value.incrementAndGetRefCount();
056    return value;
057  }
058
059  /**
060   * Delete the reference. Decrease the reference count for the instance, if
061   * any. On all references removal delete the instance from the map.
062   * 
063   * @param key Key to remove the reference.
064   */
065  public void remove(E key) {
066    E value = referenceMap.get(key);
067    if (value != null && value.decrementAndGetRefCount() == 0) {
068      referenceMap.remove(key);
069    }
070  }
071
072  /**
073   * Get entries in the reference Map.
074   * 
075   * @return
076   */
077  @VisibleForTesting
078  public ImmutableList<E> getEntries() {
079    return new ImmutableList.Builder<E>().addAll(referenceMap.keySet()).build();
080  }
081
082  /**
083   * Get the reference count for the key
084   */
085  public long getReferenceCount(E key) {
086    ReferenceCounter counter = referenceMap.get(key);
087    if (counter != null) {
088      return counter.getRefCount();
089    }
090    return 0;
091  }
092
093  /**
094   * Get the number of unique elements
095   */
096  public int getUniqueElementsSize() {
097    return referenceMap.size();
098  }
099
100  /**
101   * Clear the contents
102   */
103  @VisibleForTesting
104  public void clear() {
105    referenceMap.clear();
106  }
107
108  /**
109   * Interface for the reference count holder
110   */
111  public static interface ReferenceCounter {
112    public int getRefCount();
113
114    public int incrementAndGetRefCount();
115
116    public int decrementAndGetRefCount();
117  }
118}