Class 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 also final.) This entails that methods that normally should be able to modify the contents of a Map (e.g. put() or clear() 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é
    • Nested Class Summary

      • Nested classes/interfaces inherited from interface java.util.Map

        java.util.Map.Entry<K extends java.lang.Object,​V extends java.lang.Object>
    • Constructor Summary

      Constructors 
      Constructor Description
      CacheMap​(java.lang.String[] names)  
      CacheMap​(java.lang.String[] names, java.lang.Object[] values)  
    • 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 map
      int 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()  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
      • Methods inherited from interface java.util.Map

        compute, computeIfAbsent, computeIfPresent, equals, forEach, getOrDefault, hashCode, merge, putIfAbsent, remove, replace, replace, replaceAll
    • Constructor Detail

      • CacheMap

        public CacheMap​(java.lang.String[] names,
                        java.lang.Object[] values)
      • CacheMap

        public CacheMap​(java.lang.String[] names)
    • Method Detail

      • clear

        public final void clear()
        Specified by:
        clear in interface java.util.Map<java.lang.String,​T>
      • containsKey

        public final boolean containsKey​(java.lang.Object key)
        Specified by:
        containsKey in interface java.util.Map<java.lang.String,​T>
      • containsValue

        public final boolean containsValue​(java.lang.Object value)
        Specified by:
        containsValue in interface java.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 interface java.util.Map<java.lang.String,​T>
      • get

        public final T get​(java.lang.Object key)
        Specified by:
        get in interface java.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 to mykey in o, and update cached_index to the position in the array where this key was found. Later calls to getIndexOf will use that value to directly access the element, rather than look for it.
        Parameters:
        key - The key to get in the map
        index - 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 interface java.util.Map<java.lang.String,​T>
      • keySet

        public final java.util.Set<java.lang.String> keySet()
        Specified by:
        keySet in interface java.util.Map<java.lang.String,​T>
      • put

        public final T put​(java.lang.String key,
                           T value)
        Specified by:
        put in interface java.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 interface java.util.Map<java.lang.String,​T>
      • remove

        public final T remove​(java.lang.Object key)
        Specified by:
        remove in interface java.util.Map<java.lang.String,​T>
      • size

        public final int size()
        Specified by:
        size in interface java.util.Map<java.lang.String,​T>
      • values

        public final java.util.Collection<T> values()
        Specified by:
        values in interface java.util.Map<java.lang.String,​T>
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object