Class FactMap<T extends @Nullable Object>
- Type Parameters:
T- The object/value type of the facts
- All Implemented Interfaces:
FactStore<T>,Map<String,@Nullable FactReference<T>>
FactStore: a map from each fact's name to its FactReference, backed by a
HashMap.
Every way of adding a fact rejects a null name, and rejects a key that differs from the
name of the fact stored under it, with IllegalArgumentException.
That includes Map.Entry.setValue(V) on an entrySet() entry and replaceAll(java.util.function.BiFunction<? super java.lang.String, ? super io.github.brantunger.unruly.api.FactReference<T>, ? extends io.github.brantunger.unruly.api.FactReference<T>>).
The varargs constructor also rejects two facts with the same name. getValue(java.lang.String) rejects a null name
too, while get(java.lang.Object), containsKey(java.lang.Object) and remove(java.lang.Object) follow the Map contract: a null
key is never stored, so they find nothing under it.
A FactMap is a Map as well as a FactStore, so a FactMap variable has every
Map method. asMap() returns a read-only view of it.
equals, hashCode and toString follow the Map contract: two maps holding equal
facts under the same names are equal, whatever their implementation.
Threads: not thread-safe, like the HashMap it wraps. Build one for each run, and don't change it
while a run reads it.
- See Also:
-
Nested Class Summary
-
Constructor Summary
ConstructorsConstructorDescriptionFactMap()Construct a new empty FactMap.FactMap(FactReference<T>... facts) Construct a new FactMap from an array of existing FactReference objectsFactMap(Map<String, ? extends @Nullable FactReference<T>> facts) Construct a new FactMap from a map of facts. -
Method Summary
Modifier and TypeMethodDescriptionMap<String, @Nullable FactReference<T>> asMap()Returns a read-only view of the facts, keyed by name.voidclear()booleancontainsKey(@Nullable Object key) booleancontainsValue(@Nullable Object value) Set<Map.Entry<String, @Nullable FactReference<T>>> entrySet()Returns a view of the entries backed by this map.booleanCompares by entries, as theMapcontract requires: equal to anyMap, of any implementation, holding equal facts under the same names.@Nullable FactReference<T> @Nullable TGets the value of the fact stored under a name.inthashCode()booleanisEmpty()keySet()@Nullable FactReference<T> put(FactReference<T> fact) Stores aFactReferenceunder its name.@Nullable FactReference<T> put(String key, @Nullable FactReference<T> fact) Storesfactunderkey.voidputAll(Map<? extends String, ? extends @Nullable FactReference<T>> map) Copies every entry ofmapinto this map.@Nullable FactReference<T> voidreplaceAll(BiFunction<? super String, ? super @Nullable FactReference<T>, ? extends @Nullable FactReference<T>> function) Replaces every fact with the function's result.voidSets the value of the named fact by storing a newFact.intsize()toString()Collection<@Nullable FactReference<T>> values()Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface java.util.Map
compute, computeIfAbsent, computeIfPresent, forEach, getOrDefault, merge, putIfAbsent, remove, replace, replace
-
Constructor Details
-
FactMap
public FactMap()Construct a new empty FactMap. -
FactMap
Construct a new FactMap from a map of facts. The map itself is copied, and theFactReferenceobjects are shared with it. Changing a value throughsetValue(java.lang.String, T)affects only this map.- Parameters:
facts- The fact map to construct the facts from. Its values may be anyFactReferenceimplementation, such as aMap<String, Fact<T>>.- Throws:
IllegalArgumentException- if a key isnullor differs from its fact's nameNullPointerException- iffactsisnull
-
FactMap
Construct a new FactMap from an array of existing FactReference objects- Parameters:
facts- The facts to construct a new FactMap with- Throws:
IllegalArgumentException- if a fact's name isnull, or two facts share a nameNullPointerException- iffactsor any element isnull
-
-
Method Details
-
getValue
Gets the value of the fact stored under a name.- Specified by:
getValuein interfaceFactStore<T extends @Nullable Object>- Parameters:
name- The fact's name- Returns:
- The value, or
nulleither if there is no fact with that name or if the fact's value isnull. To tell the two apart, askFactStore.asMap()whether itcontainsKey(name). - Throws:
IllegalArgumentException- ifnameisnull
-
setValue
Sets the value of the named fact by storing a newFact. An existingFactReferenceis replaced, never updated in place, so aFactMapcopied from another, or built from the sameFactobjects, is not changed by this call. A reference previously obtained fromget(java.lang.Object)keeps its old value.- Specified by:
setValuein interfaceFactStore<T extends @Nullable Object>- Parameters:
name- The name of the factobj- The new value- Throws:
IllegalArgumentException- ifnameisnull
-
put
Stores aFactReferenceunder its name.- Specified by:
putin interfaceFactStore<T extends @Nullable Object>- Parameters:
fact- TheFactReferenceto store- Returns:
- The previous
FactReferencestored under the fact's name, ornullif there was none, as withMap.put(Object, Object) - Throws:
IllegalArgumentException- if the fact's name isnullNullPointerException- iffactisnull
-
asMap
Description copied from interface:FactStoreReturns a read-only view of the facts, keyed by name. The view follows later changes to the store. A run copies its entries once, when it starts, binding each fact's value to its key.- Specified by:
asMapin interfaceFactStore<T extends @Nullable Object>- Returns:
- An unmodifiable map of each name to its
FactReference; must not benull: anullone fails the run with aNullPointerException
-
size
public int size() -
isEmpty
public boolean isEmpty() -
containsKey
- Specified by:
containsKeyin interfaceMap<String,@Nullable FactReference<T extends @Nullable Object>>
-
containsValue
- Specified by:
containsValuein interfaceMap<String,@Nullable FactReference<T extends @Nullable Object>>
-
get
-
put
Storesfactunderkey.- Specified by:
putin interfaceMap<String,@Nullable FactReference<T extends @Nullable Object>> - Parameters:
key- The fact's namefact- The fact. Unless it isnull, its own name must equalkey.- Returns:
- The fact previously stored under
key, ornullif there was none - Throws:
IllegalArgumentException- ifkeyisnullor differs from the fact's name
-
remove
-
putAll
Copies every entry ofmapinto this map. Every entry is checked likeput(String, FactReference)before any is stored, so an invalid one leaves this map unchanged.- Specified by:
putAllin interfaceMap<String,@Nullable FactReference<T extends @Nullable Object>> - Parameters:
map- The facts to add- Throws:
IllegalArgumentException- if a key isnullor differs from its fact's nameNullPointerException- ifmapisnull
-
clear
public void clear() -
keySet
-
values
-
replaceAll
public void replaceAll(BiFunction<? super String, ? super @Nullable FactReference<T>, ? extends @Nullable FactReference<T>> function) Replaces every fact with the function's result. Every result is checked likeput(String, FactReference)before any is stored, so an invalid one leaves this map unchanged.- Specified by:
replaceAllin interfaceMap<String,@Nullable FactReference<T extends @Nullable Object>> - Parameters:
function- Computes the replacement for each name and fact- Throws:
IllegalArgumentException- if a result's name differs from its key
-
entrySet
Returns a view of the entries backed by this map. Removing through the view works as usual, and an entry'sMap.Entry.setValue(V)is checked likeput(String, FactReference), so it can't store a fact under a key that differs from the fact's name. -
equals
Compares by entries, as theMapcontract requires: equal to anyMap, of any implementation, holding equal facts under the same names. -
hashCode
public int hashCode() -
toString
-