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.*;
016import java.util.concurrent.*;
017
018/**
019 * A hashmap that allows for two-part keys.
020 * @param <K1> Key part 1 type.
021 * @param <K2> Key part 2 type.
022 * @param <V> Value type.
023 */
024public class TwoKeyConcurrentCache<K1,K2,V> extends ConcurrentHashMap<TwoKeyConcurrentCache.Key<K1,K2>,V> {
025   private static final long serialVersionUID = 1L;
026
027   private final boolean disabled;
028
029   /**
030    * Constructor.
031    */
032   public TwoKeyConcurrentCache() {
033      this.disabled = false;
034   }
035
036   /**
037    * Constructor.
038    * @param disabled If <jk>true</jk>, get/put operations are no-ops.
039    */
040   public TwoKeyConcurrentCache(boolean disabled) {
041      this.disabled = disabled;
042   }
043
044   /**
045    * Adds an entry to this map.
046    *
047    * @param key1 Key part 1.  Can be <jk>null</jk>.
048    * @param key2 Key part 2.  Can be <jk>null</jk>.
049    * @param value Value.
050    * @return The previous value if there was one.
051    */
052   public V put(K1 key1, K2 key2, V value) {
053      if (disabled)
054         return null;
055      Key<K1,K2> key = new Key<>(key1, key2);
056      return super.put(key, value);
057   }
058
059   /**
060    * Retrieves an entry from this map.
061    *
062    * @param key1 Key part 1.  Can be <jk>null</jk>.
063    * @param key2 Key part 2.  Can be <jk>null</jk>.
064    * @return The previous value if there was one.
065    */
066   public V get(K1 key1, K2 key2) {
067      if (disabled)
068         return null;
069      Key<K1,K2> key = new Key<>(key1, key2);
070      return super.get(key);
071   }
072
073   static class Key<K1,K2> {
074      final K1 k1;
075      final K2 k2;
076      final int hashCode;
077
078      Key(K1 k1, K2 k2) {
079         this.k1 = k1;
080         this.k2 = k2;
081         this.hashCode = 31*(k1 == null ? 0 : k1.hashCode()) + (k2 == null ? 0 : k2.hashCode());
082      }
083
084      @Override /* Object */
085      public int hashCode() {
086         return hashCode;
087      }
088
089      @Override /* Object */
090      @SuppressWarnings("unchecked")
091      public boolean equals(Object o) {
092         Key<K1,K2> ko = (Key<K1,K2>)o;
093         return Objects.equals(k1, ko.k1) && Objects.equals(k2, ko.k2);
094      }
095   }
096}