![JAR search and dependency download from the Maven repository](/logo.png)
org.apache.juneau.internal.TwoKeyConcurrentCache Maven / Gradle / Ivy
// ***************************************************************************************************************************
// * 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 org.apache.juneau.internal;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.*;
/**
* A hashmap that allows for two-part keys.
*
* See Also:
*
*
* @param Key part 1 type.
* @param Key part 2 type.
* @param Value type.
* @serial exclude
*/
public class TwoKeyConcurrentCache extends ConcurrentHashMap,V> {
private static final long serialVersionUID = 1L;
private final boolean disabled;
private final BiFunction supplier;
/**
* Constructor.
*/
public TwoKeyConcurrentCache() {
this(false, null);
}
/**
* Constructor.
* @param disabled If true , get/put operations are no-ops.
* @param supplier The supplier for this cache.
*/
public TwoKeyConcurrentCache(boolean disabled, BiFunction supplier) {
this.disabled = disabled;
this.supplier = supplier;
}
/**
* Adds an entry to this map.
*
* @param key1 Key part 1. Can be null .
* @param key2 Key part 2. Can be null .
* @param value Value.
* @return The previous value if there was one.
*/
public V put(K1 key1, K2 key2, V value) {
if (disabled)
return null;
Key key = new Key<>(key1, key2);
return super.put(key, value);
}
/**
* Retrieves an entry from this map.
*
* @param key1 Key part 1. Can be null .
* @param key2 Key part 2. Can be null .
* @return The previous value if there was one.
*/
public V get(K1 key1, K2 key2) {
if (disabled)
return (supplier == null ? null : supplier.apply(key1, key2));
Key key = new Key<>(key1, key2);
V v = super.get(key);
if (v == null && supplier != null) {
v = supplier.apply(key1, key2);
super.put(key, v);
}
return v;
}
static class Key {
final K1 k1;
final K2 k2;
final int hashCode;
Key(K1 k1, K2 k2) {
this.k1 = k1;
this.k2 = k2;
this.hashCode = 31*(k1 == null ? 0 : k1.hashCode()) + (k2 == null ? 0 : k2.hashCode());
}
@Override /* Object */
public int hashCode() {
return hashCode;
}
@Override /* Object */
@SuppressWarnings("unchecked")
public boolean equals(Object o) {
Key ko = (Key)o;
return Objects.equals(k1, ko.k1) && Objects.equals(k2, ko.k2);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy