io.proximax.core.utils.AbstractTwoLevelMap Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of java-xpx-chain-sdk Show documentation
Show all versions of java-xpx-chain-sdk Show documentation
The ProximaX Sirius Chain Java SDK is a Java library for interacting with the Sirius Blockchain.
The newest version!
/*
* Copyright 2018 NEM
*
* Licensed 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 io.proximax.core.utils;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
/**
* A two-level map of items.
*
* Items are automatically created on access.
* Item associations are order-dependent.
*/
public abstract class AbstractTwoLevelMap {
private final Map> impl = new ConcurrentHashMap<>();
/**
* Gets the TValue associated with key1 and key2.
*
* @param key1 The first key.
* @param key2 The second key.
* @return The value associated with key and key2.
*/
public TValue getItem(final TKey key1, final TKey key2) {
final Map keyOneValues = this.getItems(key1);
TValue value = keyOneValues.get(key2);
if (null == value) {
value = this.createValue();
keyOneValues.put(key2, value);
}
return value;
}
/**
* Gets the (TKey, TValue) map associated with key.
*
* @param key The first key.
* @return The map associated with key.
*/
public Map getItems(final TKey key) {
Map keyValues = this.impl.get(key);
if (null == keyValues) {
keyValues = new ConcurrentHashMap<>();
this.impl.put(key, keyValues);
}
return keyValues;
}
/**
* Removes a key from the map.
*
* @param key The key to remove.
*/
public void remove(final TKey key) {
this.impl.remove(key);
}
/**
* Gets the key set of this map.
*
* @return The key set.
*/
public Set keySet() {
return this.impl.keySet();
}
/**
* Creates a new blank value.
*
* @return A new value.
*/
protected abstract TValue createValue();
}