001// ***************************************************************************************************************************
002// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.  See the NOTICE file *
003// * distributed with this work for additional information regarding copyright ownership.  The ASF licenses this file        *
004// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance            *
005// * with the License.  You may obtain a copy of the License at                                                              *
006// *                                                                                                                         *
007// *  http://www.apache.org/licenses/LICENSE-2.0                                                                             *
008// *                                                                                                                         *
009// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an  *
010// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the        *
011// * specific language governing permissions and limitations under the License.                                              *
012// ***************************************************************************************************************************
013package org.apache.juneau.utils;
014
015import java.util.concurrent.*;
016
017/**
018 * Map consisting of auto-generated atomic keys.
019 *
020 * <p>
021 * Useful for creating in-memory 'databases' of POJOs.
022 *
023 * @param <K> The key type.
024 * @param <V> The value type.
025 */
026public class IdMap<K,V> extends ConcurrentHashMap<K,V> {
027
028   private static final long serialVersionUID = 1L;
029
030   private final IdGenerator<K> idGen;
031
032
033   /**
034    * Creates a new ID map with integer keys with generator initialized to <code>1</code>.
035    *
036    * @param c The value type.
037    * @return A new map.
038    */
039   public static <T> IdMap<Integer,T> createIntMap(Class<T> c) {
040      return createIntMap(c, 1);
041   }
042
043   /**
044    * Creates a new ID map with integer keys with generator initialized to the specified value.
045    *
046    * @param c The value type.
047    * @param initValue The initial value of the generator.
048    * @return A new map.
049    */
050   public static <T> IdMap<Integer,T> createIntMap(Class<T> c, int initValue) {
051      return create(c, IdGenerators.createIntGenerator(initValue));
052   }
053
054   /**
055    * Creates a new ID map with long keys with generator initialized to <code>1</code>.
056    *
057    * @param c The value type.
058    * @return A new map.
059    */
060   public static <T> IdMap<Long,T> createLongMap(Class<T> c) {
061      return createLongMap(c, 1l);
062   }
063
064   /**
065    * Creates a new ID map with long keys with generator initialized to the specified value.
066    *
067    * @param c The value type.
068    * @param initValue The initial value of the generator.
069    * @return A new map.
070    */
071   public static <T> IdMap<Long,T> createLongMap(Class<T> c, long initValue) {
072      return create(c, IdGenerators.createLongGenerator(initValue));
073   }
074
075   /**
076    * Creates a new map.
077    *
078    * @param c The value type.
079    * @param idGen An ID generator.
080    * @return A new instance.
081    */
082   public static <K,T> IdMap<K,T> create(Class<T> c, IdGenerator<K> idGen) {
083      return new IdMap<>(c, idGen);
084   }
085
086   private IdMap(Class<V> c, IdGenerator<K> idGen) {
087      this.idGen = idGen;
088   }
089
090   /**
091    * Returns the next available ID.
092    *
093    * @return The next available ID.
094    */
095   public K nextId() {
096      return idGen.next();
097   }
098
099   /**
100    * Sets a lower bound on the specified ID.
101    * @param k The lower-bound key.
102    */
103   public void lbId(K k) {
104      idGen.lb(k);
105   }
106}