Package com.github.tommyettinger.ds
Class FilteredStringMap<V>
java.lang.Object
com.github.tommyettinger.ds.ObjectObjectMap<String,V>
com.github.tommyettinger.ds.FilteredStringMap<V>
A custom variant on ObjectObjectMap that always uses String keys, but only considers any character in an item (for
equality and hashing purposes) if that character satisfies a predicate. This can also edit the characters that pass
the filter, such as by changing their case during comparisons (and hashing). You will usually want to call
You can use this class as a replacement for
Be advised that if you use some (most) checks in
setFilter(CharFilter) to change the behavior of hashing and equality before you enter any items, unless you
have specified the CharFilter you want in the constructor.
You can use this class as a replacement for
CaseInsensitiveMap if you set the editor to a method reference to
Character.toUpperCase(char) or Casing.caseUp(char). You can go further by setting the filter to make the hashing and equality checks
ignore characters that don't satisfy a predicate, such as Character.isLetter(char).
CaseInsensitiveMap does allow taking arbitrary CharSequence types as keys, but it doesn't permit modifying
them, so usually Strings are a good choice anyway.
Be advised that if you use some (most) checks in
Character for properties of a char, and you try to use them
on GWT, those checks will not work as expected for non-ASCII characters. Some other platforms might also be affected,
such as TeaVM, but it isn't clear yet which platforms have full Unicode support. You can consider depending upon
RegExodus for more cross-platform Unicode support; a method
reference to Category.L::contains acts like Character::isLetter, but works on GWT.
com.github.tommyettinger.ds.support.util.CharPredicates provides a few common CharPredicate constants that
will work identically on all platforms.-
Nested Class Summary
Nested classes/interfaces inherited from class com.github.tommyettinger.ds.ObjectObjectMap
ObjectObjectMap.Entries<K,V>, ObjectObjectMap.Entry<K, V>, ObjectObjectMap.Keys<K, V>, ObjectObjectMap.MapIterator<K, V, I>, ObjectObjectMap.Values<K, V> -
Field Summary
FieldsFields inherited from class com.github.tommyettinger.ds.ObjectObjectMap
defaultValue, entries1, entries2, hashMultiplier, keys1, keys2, keyTable, loadFactor, mask, shift, size, threshold, values1, values2, valueTable -
Constructor Summary
ConstructorsConstructorDescriptionCreates a new map with an initial capacity ofUtilities.getDefaultTableCapacity()and a load factor ofUtilities.getDefaultLoadFactor().FilteredStringMap(int initialCapacity) Creates a new map with the specified initial capacity and a load factor ofUtilities.getDefaultLoadFactor().FilteredStringMap(int initialCapacity, float loadFactor) Creates a new map with the specified initial capacity and load factor.FilteredStringMap(CharFilter filter) Creates a new map with an initial capacity ofUtilities.getDefaultTableCapacity()and a load factor ofUtilities.getDefaultLoadFactor().FilteredStringMap(CharFilter filter, int initialCapacity) Creates a new map with the specified initial capacity and the default load factor.FilteredStringMap(CharFilter filter, int initialCapacity, float loadFactor) Creates a new map with the specified initial capacity and load factor.FilteredStringMap(CharFilter filter, String[] keys, V[] values) Given two side-by-side arrays, one of keys, one of values, this constructs a map and inserts each pair of key and value into it.FilteredStringMap(CharFilter filter, Collection<String> keys, Collection<? extends V> values) Given two side-by-side collections, one of keys, one of values, this constructs a map and inserts each pair of key and value into it.FilteredStringMap(CharFilter filter, Map<String, ? extends V> map) Creates a new map identical to the specified map.FilteredStringMap(FilteredStringMap<? extends V> map) Creates a new map identical to the specified map. -
Method Summary
Modifier and TypeMethodDescriptionbooleanbooleanCompares two objects for equality by the rules this filtered data structure uses for keys.inthashCode()protected inthashHelper(String s) Gets a low-to-moderate quality 32-bit hash code from the given String.static <V> FilteredStringMap<V>parse(CharFilter filter, String str, String entrySeparator, String keyValueSeparator, PartialParser<V> valueParser) Creates a new map by parsing all ofstrwith the given PartialParser for values, with entries separated byentrySeparator, such as", "and the keys separated from values bykeyValueSeparator, such as"=".static <V> FilteredStringMap<V>parse(CharFilter filter, String str, String entrySeparator, String keyValueSeparator, PartialParser<V> valueParser, boolean brackets) Creates a new map by parsing all ofstr(or ifbracketsis true, all but the first and last chars) with the given PartialParser for values, with entries separated byentrySeparator, such as", "and the keys separated from values bykeyValueSeparator, such as"=".static <V> FilteredStringMap<V>parse(CharFilter filter, String str, String entrySeparator, String keyValueSeparator, PartialParser<V> valueParser, int offset, int length) Creates a new map by parsing the given subrange ofstrwith the given PartialParser for values, with entries separated byentrySeparator, such as", "and the keys separated from values bykeyValueSeparator, such as"=".protected intReturns an index >= 0 and <=ObjectObjectMap.maskfor the specifieditem, mixed.setFilter(CharFilter filter) Sets the CharFilter that determines which characters in a String are considered for equality and hashing, as well as any changes made to characters before hashing or equating, then returns this object, for chaining.static <V> FilteredStringMap<V>with(CharFilter filter) Constructs an empty map given just a CharFilter.static <V> FilteredStringMap<V>with(CharFilter filter, String key0, V value0) Constructs a single-entry map given a CharFilter, one key and one value.static <V> FilteredStringMap<V>with(CharFilter filter, String key0, V value0, Object... rest) Constructs a map given a CharFilter, then alternating keys and values.static <V> FilteredStringMap<V>with(CharFilter filter, String key0, V value0, String key1, V value1) Constructs a single-entry map given a CharFilter and two key-value pairs.static <V> FilteredStringMap<V>with(CharFilter filter, String key0, V value0, String key1, V value1, String key2, V value2) Constructs a single-entry map given a CharFilter and three key-value pairs.static <V> FilteredStringMap<V>with(CharFilter filter, String key0, V value0, String key1, V value1, String key2, V value2, String key3, V value3) Constructs a single-entry map given a CharFilter and four key-value pairs.Methods inherited from class com.github.tommyettinger.ds.ObjectObjectMap
appendTo, appendTo, clear, clear, combine, combine, containsKey, containsValue, containsValue, ensureCapacity, entrySet, equalsIdentity, findKey, findKey, get, getDefaultValue, getHashMultiplier, getLoadFactor, getOrDefault, getTableSize, isEmpty, iterator, keySet, locateKey, notEmpty, parse, parse, parse, put, putAll, putAll, putAll, putAll, putAll, putAll, putLegible, putLegible, putLegible, putLegible, putOrDefault, putPairs, putResize, remove, replace, resize, setDefaultValue, setHashMultiplier, setLoadFactor, shrink, size, toString, toString, toString, toString, truncate, values, with, with, with, with, with, withMethods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface java.lang.Iterable
forEach, spliteratorMethods inherited from interface java.util.Map
compute, computeIfAbsent, computeIfPresent, forEach, merge, putIfAbsent, remove, replace, replaceAll
-
Field Details
-
filter
-
-
Constructor Details
-
FilteredStringMap
public FilteredStringMap()Creates a new map with an initial capacity ofUtilities.getDefaultTableCapacity()and a load factor ofUtilities.getDefaultLoadFactor(). -
FilteredStringMap
public FilteredStringMap(int initialCapacity) Creates a new map with the specified initial capacity and a load factor ofUtilities.getDefaultLoadFactor(). This map will hold initialCapacity items before growing the backing table.- Parameters:
initialCapacity- If not a power of two, it is increased to the next nearest power of two.
-
FilteredStringMap
public FilteredStringMap(int initialCapacity, float loadFactor) Creates a new map with the specified initial capacity and load factor. This map will hold initialCapacity items before growing the backing table.- Parameters:
initialCapacity- If not a power of two, it is increased to the next nearest power of two.loadFactor- what fraction of the capacity can be filled before this has to resize; 0 < loadFactor <= 1
-
FilteredStringMap
Creates a new map with an initial capacity ofUtilities.getDefaultTableCapacity()and a load factor ofUtilities.getDefaultLoadFactor(). This uses the specified CharFilter.- Parameters:
filter- a CharFilter that can be obtained withCharFilter.getOrCreate(String, CharPredicate, CharToCharFunction)
-
FilteredStringMap
Creates a new map with the specified initial capacity and the default load factor. This map will hold initialCapacity items before growing the backing table. This uses the specified CharFilter.- Parameters:
filter- a CharFilter that can be obtained withCharFilter.getOrCreate(String, CharPredicate, CharToCharFunction)initialCapacity- If not a power of two, it is increased to the next nearest power of two.
-
FilteredStringMap
Creates a new map with the specified initial capacity and load factor. This map will hold initialCapacity items before growing the backing table. This uses the specified CharFilter.- Parameters:
filter- a CharFilter that can be obtained withCharFilter.getOrCreate(String, CharPredicate, CharToCharFunction)initialCapacity- If not a power of two, it is increased to the next nearest power of two.loadFactor- what fraction of the capacity can be filled before this has to resize; 0 < loadFactor <= 1
-
FilteredStringMap
Creates a new map identical to the specified map.- Parameters:
map- an FilteredStringMap to copy
-
FilteredStringMap
Creates a new map identical to the specified map. This uses the specified CharFilter.- Parameters:
filter- a CharFilter that can be obtained withCharFilter.getOrCreate(String, CharPredicate, CharToCharFunction)map- a Map to copy; ObjectObjectOrderedMap and subclasses of it will be faster to load from
-
FilteredStringMap
Given two side-by-side arrays, one of keys, one of values, this constructs a map and inserts each pair of key and value into it. If keys and values have different lengths, this only uses the length of the smaller array. This uses the specified CharFilter.- Parameters:
filter- a CharFilter that can be obtained withCharFilter.getOrCreate(String, CharPredicate, CharToCharFunction)keys- an array of keysvalues- an array of values
-
FilteredStringMap
public FilteredStringMap(CharFilter filter, Collection<String> keys, Collection<? extends V> values) Given two side-by-side collections, one of keys, one of values, this constructs a map and inserts each pair of key and value into it. If keys and values have different lengths, this only uses the length of the smaller collection. This uses the specified CharFilter.- Parameters:
filter- a CharFilter that can be obtained withCharFilter.getOrCreate(String, CharPredicate, CharToCharFunction)keys- a Collection of keysvalues- a Collection of values
-
-
Method Details
-
getFilter
-
setFilter
Sets the CharFilter that determines which characters in a String are considered for equality and hashing, as well as any changes made to characters before hashing or equating, then returns this object, for chaining. If the filter changes, that invalidates anything previously entered into this, so before changing the filter this clears the entire data structure, removing all existing items.- Parameters:
filter- a CharFilter that can be obtained withCharFilter.getOrCreate(String, CharPredicate, CharToCharFunction)- Returns:
- this, for chaining
-
hashHelper
Gets a low-to-moderate quality 32-bit hash code from the given String. This operates by checking if a char insmatches the filter, and if it does, it rotates the current hash, multiplies it by thehash multiplier, and XORs with the current char after editing. This finalizes the hash by multiplying it again by the hash multiplier, then using the reversible XOR-rotate-XOR-rotate sequence of operations to adequately jumble the bits.- Parameters:
s- a String to hash- Returns:
- a 32-bit hash of
s
-
place
Description copied from class:ObjectObjectMapReturns an index >= 0 and <=ObjectObjectMap.maskfor the specifieditem, mixed.- Overrides:
placein classObjectObjectMap<String,V> - Parameters:
item- a non-null Object; its hashCode() method should be used by most implementations- Returns:
- an index between 0 and
ObjectObjectMap.mask(both inclusive)
-
equate
Compares two objects for equality by the rules this filtered data structure uses for keys. This will return true if the arguments are reference-equivalent or both null. Otherwise, it requires that both areStrings and compares them using thefilterof this object.- Overrides:
equatein classObjectObjectMap<String,V> - Parameters:
left- must be non-null; typically a key being compared, but not necessarilyright- may be null; typically a key being compared, but can often be null for an empty key slot, or some other type- Returns:
- true if left and right are equivalent according to the rules this filtered type uses
-
hashCode
public int hashCode() -
equals
-
with
Constructs an empty map given just a CharFilter. This is usually less useful than just using the constructor, but can be handy in some code-generation scenarios when you don't know how many arguments you will have.- Type Parameters:
V- the type of values- Parameters:
filter- a CharFilter that can be obtained withCharFilter.getOrCreate(String, CharPredicate, CharToCharFunction)- Returns:
- a new map containing nothing
-
with
Constructs a single-entry map given a CharFilter, one key and one value. This is mostly useful as an optimization forObjectObjectMap.with(Object, Object, Object...)when there's no "rest" of the keys or values.- Type Parameters:
V- the type of value0- Parameters:
filter- a CharFilter that can be obtained withCharFilter.getOrCreate(String, CharPredicate, CharToCharFunction)key0- the first and only keyvalue0- the first and only value- Returns:
- a new map containing just the entry mapping key0 to value0
-
with
public static <V> FilteredStringMap<V> with(CharFilter filter, String key0, V value0, String key1, V value1) Constructs a single-entry map given a CharFilter and two key-value pairs. This is mostly useful as an optimization forObjectObjectMap.with(Object, Object, Object...)when there's no "rest" of the keys or values.- Type Parameters:
V- the type of value0- Parameters:
filter- a CharFilter that can be obtained withCharFilter.getOrCreate(String, CharPredicate, CharToCharFunction)key0- a String keyvalue0- a V valuekey1- a String keyvalue1- a V value- Returns:
- a new map containing entries mapping each key to the following value
-
with
public static <V> FilteredStringMap<V> with(CharFilter filter, String key0, V value0, String key1, V value1, String key2, V value2) Constructs a single-entry map given a CharFilter and three key-value pairs. This is mostly useful as an optimization forObjectObjectMap.with(Object, Object, Object...)when there's no "rest" of the keys or values.- Type Parameters:
V- the type of value0- Parameters:
filter- a CharFilter that can be obtained withCharFilter.getOrCreate(String, CharPredicate, CharToCharFunction)key0- a String keyvalue0- a V valuekey1- a String keyvalue1- a V valuekey2- a String keyvalue2- a V value- Returns:
- a new map containing entries mapping each key to the following value
-
with
public static <V> FilteredStringMap<V> with(CharFilter filter, String key0, V value0, String key1, V value1, String key2, V value2, String key3, V value3) Constructs a single-entry map given a CharFilter and four key-value pairs. This is mostly useful as an optimization forObjectObjectMap.with(Object, Object, Object...)when there's no "rest" of the keys or values.- Type Parameters:
V- the type of value0- Parameters:
filter- a CharFilter that can be obtained withCharFilter.getOrCreate(String, CharPredicate, CharToCharFunction)key0- a String keyvalue0- a V valuekey1- a String keyvalue1- a V valuekey2- a String keyvalue2- a V valuekey3- a String keyvalue3- a V value- Returns:
- a new map containing entries mapping each key to the following value
-
with
public static <V> FilteredStringMap<V> with(CharFilter filter, String key0, V value0, Object... rest) Constructs a map given a CharFilter, then alternating keys and values. This can be useful in some code-generation scenarios, or when you want to make a map conveniently by-hand and have it populated at the start. You can also useFilteredStringMap(CharFilter, String[], Object[]), which takes all keys and then all values. This needs all keys to have the same type and all values to have the same type, because it gets those types from the first key parameter and first value parameter. Any keys that don't have String as their type or values that don't have V as their type have that entry skipped.- Type Parameters:
V- the type of values, inferred from value0- Parameters:
filter- a CharFilter that can be obtained withCharFilter.getOrCreate(String, CharPredicate, CharToCharFunction)key0- the first key; will be used to determine the type of all keysvalue0- the first value; will be used to determine the type of all valuesrest- an array or varargs of alternating String, V, String, V... elements- Returns:
- a new map containing the given keys and values
-
parse
public static <V> FilteredStringMap<V> parse(CharFilter filter, String str, String entrySeparator, String keyValueSeparator, PartialParser<V> valueParser) Creates a new map by parsing all ofstrwith the given PartialParser for values, with entries separated byentrySeparator, such as", "and the keys separated from values bykeyValueSeparator, such as"=".
VariousPartialParserinstances are defined as constants, such asPartialParser.DEFAULT_STRING, and others can be created by static methods in PartialParser, such asPartialParser.objectListParser(PartialParser, String, boolean).- Parameters:
filter- a CharFilter that can be obtained withCharFilter.getOrCreate(String, CharPredicate, CharToCharFunction)str- a String containing parseable textentrySeparator- the String separating every key-value pairkeyValueSeparator- the String separating every key from its corresponding valuevalueParser- a PartialParser that returns aVvalue from a section ofstr
-
parse
public static <V> FilteredStringMap<V> parse(CharFilter filter, String str, String entrySeparator, String keyValueSeparator, PartialParser<V> valueParser, boolean brackets) Creates a new map by parsing all ofstr(or ifbracketsis true, all but the first and last chars) with the given PartialParser for values, with entries separated byentrySeparator, such as", "and the keys separated from values bykeyValueSeparator, such as"=".
VariousPartialParserinstances are defined as constants, such asPartialParser.DEFAULT_STRING, and others can be created by static methods in PartialParser, such asPartialParser.objectListParser(PartialParser, String, boolean).- Parameters:
filter- a CharFilter that can be obtained withCharFilter.getOrCreate(String, CharPredicate, CharToCharFunction)str- a String containing parseable textentrySeparator- the String separating every key-value pairkeyValueSeparator- the String separating every key from its corresponding valuevalueParser- a PartialParser that returns aVvalue from a section ofstrbrackets- if true, the first and last chars instrwill be ignored
-
parse
public static <V> FilteredStringMap<V> parse(CharFilter filter, String str, String entrySeparator, String keyValueSeparator, PartialParser<V> valueParser, int offset, int length) Creates a new map by parsing the given subrange ofstrwith the given PartialParser for values, with entries separated byentrySeparator, such as", "and the keys separated from values bykeyValueSeparator, such as"=".
VariousPartialParserinstances are defined as constants, such asPartialParser.DEFAULT_STRING, and others can be created by static methods in PartialParser, such asPartialParser.objectListParser(PartialParser, String, boolean).- Parameters:
filter- a CharFilter that can be obtained withCharFilter.getOrCreate(String, CharPredicate, CharToCharFunction)str- a String containing parseable textentrySeparator- the String separating every key-value pairkeyValueSeparator- the String separating every key from its corresponding valuevalueParser- a PartialParser that returns aVvalue from a section ofstroffset- the first position to read parseable text from instrlength- how many chars to read; -1 is treated as maximum length
-