Package ca.uqac.lif.cep.tuples
Class CacheMap<T>
- java.lang.Object
-
- ca.uqac.lif.cep.tuples.CacheMap<T>
-
- All Implemented Interfaces:
java.util.Map<java.lang.String,T>
public final class CacheMap<T> extends java.lang.Object implements java.util.Map<java.lang.String,T>
Implementation of an immutable map.- After its instantiation, the object is immutable: all
its fields are declared
private final
and no method can ever change their value. (As a matter of fact, all its methods are alsofinal
.) This entails that methods that normally should be able to modify the contents of a Map (e.g.put()
orclear()
here have no effect. - Internally, the tuple uses plain arrays (instead of a
HashMap
) for storing keys and values. For tuples with, a small number of keys, this should actually provide faster access than a HashMap. In all cases, arrays use up less memory than a HashMap. - Because of this, one can also ask for the index of a key/value pair, and obtain a value based on its index (rather than its key). Assuming that all tuples in a stream have their key/value pairs arranged in the same order, this means one can ask for the index of a key a first time, and from that point on query the remaining tuples by directly providing the index.
- Author:
- Sylvain Hallé
-
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
clear()
boolean
containsKey(java.lang.Object key)
boolean
containsValue(java.lang.Object value)
java.util.Set<java.util.Map.Entry<java.lang.String,T>>
entrySet()
T
get(java.lang.Object key)
int
getIndexOf(java.lang.String s)
Get the index of a key in the current mapint
getIndexOf(java.lang.String key, int index, T out)
Retrieves a value, possibly using an index.java.lang.Object
getValue(int index)
boolean
isEmpty()
java.util.Set<java.lang.String>
keySet()
T
put(java.lang.String key, T value)
void
putAll(java.util.Map<? extends java.lang.String,? extends T> m)
void
putAll(T[] values)
void
putAt(int index, T value)
T
remove(java.lang.Object key)
int
size()
java.lang.String
toString()
java.util.Collection<T>
values()
-
-
-
Method Detail
-
clear
public final void clear()
- Specified by:
clear
in interfacejava.util.Map<java.lang.String,T>
-
containsKey
public final boolean containsKey(java.lang.Object key)
- Specified by:
containsKey
in interfacejava.util.Map<java.lang.String,T>
-
containsValue
public final boolean containsValue(java.lang.Object value)
- Specified by:
containsValue
in interfacejava.util.Map<java.lang.String,T>
-
entrySet
public final java.util.Set<java.util.Map.Entry<java.lang.String,T>> entrySet()
- Specified by:
entrySet
in interfacejava.util.Map<java.lang.String,T>
-
get
public final T get(java.lang.Object key)
- Specified by:
get
in interfacejava.util.Map<java.lang.String,T>
-
getValue
public final java.lang.Object getValue(int index)
-
getIndexOf
public final int getIndexOf(java.lang.String s)
Get the index of a key in the current map- Parameters:
s
- The key- Returns:
- The index of that key, or -1 if key does not exist
-
getIndexOf
public final int getIndexOf(java.lang.String key, int index, T out)
Retrieves a value, possibly using an index. This allows one to both get the direct index of a value in the map, if not known, and to fetch that value.Object o; cached_index = map.getIndexOf("mykey", cached_index, o);
This will put the value corresponding tomykey
ino
, and updatecached_index
to the position in the array where this key was found. Later calls togetIndexOf
will use that value to directly access the element, rather than look for it.- Parameters:
key
- The key to get in the mapindex
- If negative, the method will look for the key in the map to get the value. If greater than or equal to 0, the method will directly use that value to fetch the object to return.out
- After the call, will contain a reference to the value one is looking for- Returns:
- The index value
-
isEmpty
public final boolean isEmpty()
- Specified by:
isEmpty
in interfacejava.util.Map<java.lang.String,T>
-
keySet
public final java.util.Set<java.lang.String> keySet()
- Specified by:
keySet
in interfacejava.util.Map<java.lang.String,T>
-
put
public final T put(java.lang.String key, T value)
- Specified by:
put
in interfacejava.util.Map<java.lang.String,T>
-
putAt
public final void putAt(int index, T value)
-
putAll
public final void putAll(T[] values)
-
putAll
public final void putAll(java.util.Map<? extends java.lang.String,? extends T> m)
- Specified by:
putAll
in interfacejava.util.Map<java.lang.String,T>
-
remove
public final T remove(java.lang.Object key)
- Specified by:
remove
in interfacejava.util.Map<java.lang.String,T>
-
size
public final int size()
- Specified by:
size
in interfacejava.util.Map<java.lang.String,T>
-
values
public final java.util.Collection<T> values()
- Specified by:
values
in interfacejava.util.Map<java.lang.String,T>
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
-