Package com.cedarsoftware.util
Class CompactMap<K,V>
- java.lang.Object
-
- com.cedarsoftware.util.CompactMap<K,V>
-
- All Implemented Interfaces:
Map<K,V>
public abstract class CompactMap<K,V> extends Object implements Map<K,V>
`CompactMap` introduced. This `Map` is especially small when 0 and 1 entries are stored in it. When `>=2` entries are in the `Map` it acts as regular `Map`. You must override two methods in order to instantiate: protected abstract K getSingleValueKey(); protected abstract MapgetNewMap(); **Empty** This class only has one (1) member variable of type `Object`. If there are no entries in it, then the value of that member variable takes on a pointer (points to sentinel value.) **One entry** If the entry has a key that matches the value returned from `getSingleValueKey()` then there is no key stored and the internal single member points to the value (still retried with 100% proper Map semantics). If the single entry's key does not match the value returned from `getSingleValueKey()` then the internal field points to an internal `Class` `CompactMapEntry` which contains the key and the value (nothing else). Again, all APIs still operate the same. **Two or more entries** In this case, the single member variable points to a `Map` instance (supplied by `getNewMap()` API that user supplied.) This allows `CompactMap` to work with nearly all `Map` types. This Map supports null for the key and values. If the Map returned by getNewMap() does not support this, then this Map will not. A future version *may* support an additional option to allow it to maintain entries 2-n in an internal array (pointed to by the single member variable). This small array would be 'scanned' in linear time. Given a small *`n`* entries, the resultant `Map` would be significantly smaller than the equivalent `HashMap`, for instance. - Author:
- John DeRegnaucourt ([email protected])
Copyright (c) Cedar Software LLC
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.
-
-
Constructor Summary
Constructors Constructor Description CompactMap()
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description void
clear()
boolean
containsKey(Object key)
boolean
containsValue(Object value)
Set<Map.Entry<K,V>>
entrySet()
boolean
equals(Object obj)
V
get(Object key)
protected CompactMap.LogicalValueType
getLogicalValueType()
protected abstract Map<K,V>
getNewMap()
protected abstract K
getSingleValueKey()
int
hashCode()
protected boolean
isCaseInsensitive()
boolean
isEmpty()
Set<K>
keySet()
Map
minus(Object removeMe)
Map
plus(Object right)
V
put(K key, V value)
void
putAll(Map<? extends K,? extends V> m)
V
remove(Object key)
int
size()
Collection<V>
values()
-
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface java.util.Map
compute, computeIfAbsent, computeIfPresent, forEach, getOrDefault, merge, putIfAbsent, remove, replace, replace, replaceAll
-
-
-
-
Method Detail
-
containsKey
public boolean containsKey(Object key)
- Specified by:
containsKey
in interfaceMap<K,V>
-
containsValue
public boolean containsValue(Object value)
- Specified by:
containsValue
in interfaceMap<K,V>
-
hashCode
public int hashCode()
-
equals
public boolean equals(Object obj)
-
getLogicalValueType
protected CompactMap.LogicalValueType getLogicalValueType()
-
getSingleValueKey
protected abstract K getSingleValueKey()
- Returns:
- String key name when there is only one entry in the Map.
-
getNewMap
protected abstract Map<K,V> getNewMap()
- Returns:
- new empty Map instance to use when there is more than one entry.
-
isCaseInsensitive
protected boolean isCaseInsensitive()
-
-