001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.juneau.internal;
018
019import java.util.*;
020import java.util.concurrent.*;
021import java.util.function.*;
022
023/**
024 * A hashmap that allows for two-part keys.
025 *
026 * <h5 class='section'>See Also:</h5><ul>
027 * </ul>
028 *
029 * @param <K1> Key part 1 type.
030 * @param <K2> Key part 2 type.
031 * @param <V> Value type.
032 * @serial exclude
033 */
034public class TwoKeyConcurrentCache<K1,K2,V> extends ConcurrentHashMap<TwoKeyConcurrentCache.Key<K1,K2>,V> {
035   private static final long serialVersionUID = 1L;
036
037   private final boolean disabled;
038   private final BiFunction<K1,K2,V> supplier;
039
040   /**
041    * Constructor.
042    */
043   public TwoKeyConcurrentCache() {
044      this(false, null);
045   }
046
047   /**
048    * Constructor.
049    * @param disabled If <jk>true</jk>, get/put operations are no-ops.
050    * @param supplier The supplier for this cache.
051    */
052   public TwoKeyConcurrentCache(boolean disabled, BiFunction<K1,K2,V> supplier) {
053      this.disabled = disabled;
054      this.supplier = supplier;
055   }
056
057   /**
058    * Adds an entry to this map.
059    *
060    * @param key1 Key part 1.  Can be <jk>null</jk>.
061    * @param key2 Key part 2.  Can be <jk>null</jk>.
062    * @param value Value.
063    * @return The previous value if there was one.
064    */
065   public V put(K1 key1, K2 key2, V value) {
066      if (disabled)
067         return null;
068      Key<K1,K2> key = new Key<>(key1, key2);
069      return super.put(key, value);
070   }
071
072   /**
073    * Retrieves an entry from this map.
074    *
075    * @param key1 Key part 1.  Can be <jk>null</jk>.
076    * @param key2 Key part 2.  Can be <jk>null</jk>.
077    * @return The previous value if there was one.
078    */
079   public V get(K1 key1, K2 key2) {
080      if (disabled)
081         return (supplier == null ? null : supplier.apply(key1, key2));
082      Key<K1,K2> key = new Key<>(key1, key2);
083      V v = super.get(key);
084      if (v == null && supplier != null) {
085         v = supplier.apply(key1, key2);
086         super.put(key, v);
087      }
088      return v;
089   }
090
091   static class Key<K1,K2> {
092      final K1 k1;
093      final K2 k2;
094      final int hashCode;
095
096      Key(K1 k1, K2 k2) {
097         this.k1 = k1;
098         this.k2 = k2;
099         this.hashCode = 31*(k1 == null ? 0 : k1.hashCode()) + (k2 == null ? 0 : k2.hashCode());
100      }
101
102      @Override /* Object */
103      public int hashCode() {
104         return hashCode;
105      }
106
107      @Override /* Object */
108      @SuppressWarnings("unchecked")
109      public boolean equals(Object o) {
110         Key<K1,K2> ko = (Key<K1,K2>)o;
111         return Objects.equals(k1, ko.k1) && Objects.equals(k2, ko.k2);
112      }
113   }
114}