com.tangosol.util.SetMap Maven / Gradle / Ivy
Show all versions of coherence Show documentation
/*
* Copyright (c) 2000, 2021, Oracle and/or its affiliates.
*
* Licensed under the Universal Permissive License v 1.0 as shown at
* http://oss.oracle.com/licenses/upl.
*/
package com.tangosol.util;
import java.util.AbstractMap;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import java.util.function.Supplier;
/**
* A Map implementation based on a known {@link Set set} of keys and a {@link
* Function} that when given a key can derive the value. Once a value has been
* derived for a key the function will not be called again for the same key.
*
* This map can be mutated outside of the original set of keys or within the
* set of keys. The latter will result in the function not being called for the
* respective keys.
*
* This implementation is the inverse to a {@link MapSet} which can trivially
* distill a Map of keys and values to a Set of keys. With the use of a Function
* this implementation allows a Set of keys to be converted lazily to a Map of
* keys and values.
*
* @author hr 2016.09.29
* @since 12.2.1.4.0
*/
public class SetMap
extends AbstractMap
{
// ----- constructors ---------------------------------------------------
/**
* Construct a SetMap.
*
* @param setKeys a set of keys to base this Map on
*/
public SetMap(Set setKeys)
{
this(setKeys, k -> null);
}
/**
* Construct a SetMap.
*
* @param setKeys a set of keys to base this Map on
* @param functionValue a {@link Function} to derive the value for a given
* key
*/
public SetMap(Set setKeys, Function functionValue)
{
this(setKeys, functionValue, HashMap::new);
}
/**
* Construct a SetMap.
*
* @param setKeys a set of keys to base this Map on
* @param functionValue a {@link Function} to derive the value for a given
* key
* @param supplierMap the Map to use to hold keys and values
*/
public SetMap(Set setKeys, Function functionValue, Supplier