public interface Dict<K,V>
Map
, but optimized for
read-only handling and non-zero values.
This is defined as an immutable interface, but depending on its usage
it might nevertheless change:
Map
changes of
the underlying map will be reflected. If this is a problem
use frozen()
to get a decoupled Dict
.
Map
s. Any guarantees are void if a key changes.
Think twice before doing this.
viewMap(Map, Function)
to create the Dict
with a function which copies the value.
Thus each get(Object)
will copy the value before returning it,
so the internal value will never change.
If you want to have both freezing and copying the correct calling order
is dict = Dict.viewMap(map).frozen().valueView(copyFunction)
Dict.Base
as it provides some
standard object methods. Other implementations might want to make use of
equal(Dict, Object)
in their Object.equals(Object)
method
(use Types.hash(entries())
for your Object.hashCode()
implementation
then), and toString(Dict)
in their Object.toString()
method.
All methods of this interface which return a Dict
are silently returning
a Dict.Base
object.
Modifier and Type | Interface and Description |
---|---|
static class |
Dict.Base<KK,VV>
Abstract base class which provides useful implementations
for
Object.equals(Object) , Object.hashCode() ,
Object.toString() . |
static class |
Dict.Entry<EK,EV>
Entry access.
|
Modifier and Type | Field and Description |
---|---|
static Dict<?,?> |
EMPTY
Empty dictionary implementation.
|
Modifier and Type | Method and Description |
---|---|
default Dict.Base<K,V> |
asBase()
Convert this into a
Dict.Base to have useful implementations for
some standard Object methods. |
static <KT,VT> Dict<KT,VT> |
empty()
Get an empty dictionary.
|
Countable<Dict.Entry<K,V>> |
entries()
Get the entries of this dictionary.
|
static <KK,VV> boolean |
equal(Dict<KK,VV> dict,
java.lang.Object o)
Useful implementation for equality which can be used in
Dict implementations for Object.equals(Object) . |
default Dict<K,V> |
filtered(java.util.function.Predicate<? super Dict.Entry<? super K,? super V>> check)
Get a filtered version of this dictionary.
|
default Dict<K,V> |
frozen()
Get a frozen version of this dictionary.
|
V |
get(K key)
Get the value for the given key.
|
default V |
getNonNull(K key)
Get the value for the given key.
|
default V |
getOr(K key,
java.util.function.Function<? super K,? extends V> provider)
Get the value for the given key.
|
default V |
getOrDefault(K key,
V defaultValue)
Get the value for the given key.
|
default boolean |
hasKey(K key)
Does this dictionary provide a value for the given key?
|
default boolean |
isEmpty()
Is this map empty?
|
default Countable<K> |
keys()
Get the keys of this dictionary.
|
default V |
require(K key)
Get the value for the given key.
|
static <KT,VT> Dict<KT,VT> |
singleton(KT key,
VT value)
Get a dictionary with a single entry.
|
default int |
size()
Get the size of this dictionary.
|
static java.lang.String |
toString(Dict<?,?> dict)
Creat a string representation from the given dictionary.
|
default Countable<V> |
values()
Get the values of this dictionary.
|
default <NV> Dict<K,NV> |
valueView(java.util.function.Function<? super V,? extends NV> valueMapper)
Get a view of this dictionary which seems as it has a different value type.
|
static <MK,MV> Dict<MK,MV> |
viewMap(java.util.Map<MK,MV> map)
Create an read-only view of a standard map as if it is a dictionary.
|
static <MK,MV,DV> Dict<MK,DV> |
viewMap(java.util.Map<MK,MV> map,
java.util.function.Function<? super MV,? extends DV> valueMapper)
Create a read-only view of a standard map as if it is a dictionary and has a different value type.
|
static <MK1,MK2,MV> |
viewMap2(java.util.Map<MK1,java.util.Map<MK2,MV>> map)
View a standard 2-dimensional map as 2-dimensional dictionary.
|
static <MK1,MK2,MV,DV> |
viewMap2(java.util.Map<MK1,java.util.Map<MK2,MV>> map,
java.util.function.Function<? super MV,? extends DV> valueMapper)
View a standard 2-dimensional map as 2-dimensional dictionary
with a different inner value type.
|
static <MK,MV> Dict<MK,MV> |
viewNullableMap(java.util.Map<MK,MV> map)
Create a read-only view of a map which is possibly
null . |
static <MK,MV,DV> Dict<MK,DV> |
viewNullableMap(java.util.Map<MK,MV> map,
java.util.function.Function<? super MV,? extends DV> valueMapper)
Create a read-only view of a standard map as if it is a dictionary and has a different value type.
|
@Nullable V get(K key)
key
- keynull
if there is nonegetNonNull(Object)
@NotNull Countable<Dict.Entry<K,V>> entries()
@NotNull default V getNonNull(K key)
null
but throw a NullPointerException
instead.
Depending on the underlying mapping null
values might be acceptable, but will result in an exception
nevertheless.key
- keyjava.lang.NullPointerException
- if the return value would have been null
@NotNull default V getOrDefault(K key, @NotNull V defaultValue)
null
.key
- keydefaultValue
- default value used as fallbackdefaultValue
default V getOr(K key, @NotNull java.util.function.Function<? super K,? extends V> provider)
key
- keyprovider
- provider which is called when the key is not contain or the associated value is null
,
called with the key to provide the value which is returnedprovider
@Nullable default V require(K key)
NoSuchElementException
if the key is not contained.key
- keynull
if the associated value is null
java.util.NoSuchElementException
- if there is no such keydefault int size()
default boolean isEmpty()
true
if this map has no entriesfalse
otherwisedefault boolean hasKey(K key)
key
- key to checktrue
if the given key is contained in this dictionaryfalse
if not@NotNull default <NV> Dict<K,NV> valueView(@NotNull java.util.function.Function<? super V,? extends NV> valueMapper)
NV
- new value typevalueMapper
- mapper which maps outgoing values toi the expected type.
The mapper may assume non-null
values.@NotNull default Dict.Base<K,V> asBase()
Dict.Base
to have useful implementations for
some standard Object methods.@NotNull default Dict<K,V> frozen()
Often Dict
is used as a wrapper to an underlying Map
.
This means that changes to this map will be reflected by the wrapping Dict
which usually is counter-intuitive because this interface is immutable.
In cases where this might be a problem this method decouples the returned
Dict
so it is immutable under all sane circumstances.
The implementation takes care to keep the order of entries()
.
this
if this is already frozen@NotNull default Countable<V> values()
@NotNull default Countable<K> keys()
@NotNull default Dict<K,V> filtered(@NotNull java.util.function.Predicate<? super Dict.Entry<? super K,? super V>> check)
check
- check which decides which entries are kept@NotNull static <KT,VT> Dict<KT,VT> empty()
KT
- key typeVT
- value type@NotNull static <KT,VT> Dict<KT,VT> singleton(@NotNull KT key, @NotNull VT value)
KT
- key typeVT
- value typekey
- single keyvalue
- associated value@NotNull static <MK,MV> Dict<MK,MV> viewMap(@NotNull java.util.Map<MK,MV> map)
MK
- map and dictionary key typeMV
- map and dictionary value typemap
- map which is expected not to change nor to have null
values@NotNull static <MK,MV> Dict<MK,MV> viewNullableMap(@Nullable java.util.Map<MK,MV> map)
null
.
If it is null
return an empty dict, otherwise behave
lile viewMap(Map)
.MK
- map and dictionary key typeMV
- map and dictionary value typemap
- map which may itself be null
but is expected not to change nor to have null
valuesnull
@NotNull static <MK,MV,DV> Dict<MK,DV> viewMap(@NotNull java.util.Map<MK,MV> map, @NotNull java.util.function.Function<? super MV,? extends DV> valueMapper)
MK
- map and dictionary key typeMV
- map value typeDV
- dictionary value typemap
- map which is expected not to change nor to have null
keys
or valuesvalueMapper
- mapper which prepares values before they are returned@NotNull static <MK,MV,DV> Dict<MK,DV> viewNullableMap(@Nullable java.util.Map<MK,MV> map, @NotNull java.util.function.Function<? super MV,? extends DV> valueMapper)
null
values, otherwise it calls viewMap(Map, Function)
.MK
- map and dictionary key typeMV
- map value typeDV
- dictionary value typemap
- map which itself may be null
but is expected not to
change nor to have null
keys or valuesvalueMapper
- mapper which prepares values before they are returnedmap
is null
@NotNull static <MK1,MK2,MV> Dict<MK1,Dict<MK2,MV>> viewMap2(@NotNull java.util.Map<MK1,java.util.Map<MK2,MV>> map)
MK1
- outer key typeMK2
- inner key typeMV
- value typemap
- standard 2d map@NotNull static <MK1,MK2,MV,DV> Dict<MK1,Dict<MK2,DV>> viewMap2(@NotNull java.util.Map<MK1,java.util.Map<MK2,MV>> map, @NotNull java.util.function.Function<? super MV,? extends DV> valueMapper)
MK1
- outer key typeMK2
- inner key typeMV
- inner map value type (incoming)DV
- inner dictionary value type (returned)map
- standard 2d mapvalueMapper
- mapper which prepares values before they are returnedstatic <KK,VV> boolean equal(@NotNull Dict<KK,VV> dict, @NotNull java.lang.Object o)
Dict
implementations for Object.equals(Object)
.
It depends on the order of the entries returned by entries
,
so a Object.hashCode()
should return Types.hash(entries())
.
KK
- key typeVV
- value typedict
- dicto
- other objecttrue