Package io.protostuff

Class GraphProtostuffOutput.IdentityMap

  • Enclosing class:
    GraphProtostuffOutput

    private static final class GraphProtostuffOutput.IdentityMap
    extends java.lang.Object
    A trimed-down version of IdentityHashMap w/c caters to the specific needs of GraphOutput.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private static int DEFAULT_CAPACITY
      The initial capacity used by the no-args constructor.
      private static int MAXIMUM_CAPACITY
      The maximum capacity, used if a higher value is implicitly specified by either of the constructors with arguments.
      private static int MINIMUM_CAPACITY
      The minimum capacity, used if a lower value is implicitly specified by either of the constructors with arguments.
      private int size
      The number of key-value mappings contained in this identity hash map.
      private java.lang.Object[] table
      The table, resized as necessary.
      private int threshold
      The next size value at which to resize (capacity * load factor).
    • Constructor Summary

      Constructors 
      Constructor Description
      IdentityMap()
      Constructs a new, empty identity hash map with a default expected maximum size (21).
      IdentityMap​(int expectedMaxSize)
      Constructs a new, empty map with the specified expected maximum size.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      private int capacity​(int expectedMaxSize)
      Returns the appropriate capacity for the specified expected maximum size.
      private static int hash​(java.lang.Object x, int length)
      Returns index for Object x.
      private void init​(int initCapacity)
      Initializes object to be an empty map with the specified initial capacity, which is assumed to be a power of two between MINIMUM_CAPACITY and MAXIMUM_CAPACITY inclusive.
      private static int nextKeyIndex​(int i, int len)
      Circularly traverses table of size len.
      private void resize​(int newCapacity)
      Resize the table to hold given capacity.
      boolean shouldIncrement​(int value, java.lang.Object k, WriteSession output, int fieldNumber)
      Returns true if the provided int should increment(unique index id).
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • DEFAULT_CAPACITY

        private static final int DEFAULT_CAPACITY
        The initial capacity used by the no-args constructor. MUST be a power of two. The value 32 corresponds to the (specified) expected maximum size of 21, given a load factor of 2/3.
        See Also:
        Constant Field Values
      • MINIMUM_CAPACITY

        private static final int MINIMUM_CAPACITY
        The minimum capacity, used if a lower value is implicitly specified by either of the constructors with arguments. The value 4 corresponds to an expected maximum size of 2, given a load factor of 2/3. MUST be a power of two.
        See Also:
        Constant Field Values
      • MAXIMUM_CAPACITY

        private static final int MAXIMUM_CAPACITY
        The maximum capacity, used if a higher value is implicitly specified by either of the constructors with arguments. MUST be a power of two <= 1<<29.
        See Also:
        Constant Field Values
      • table

        private transient java.lang.Object[] table
        The table, resized as necessary. Length MUST always be a power of two.
      • size

        private int size
        The number of key-value mappings contained in this identity hash map.
      • threshold

        private transient int threshold
        The next size value at which to resize (capacity * load factor).
    • Constructor Detail

      • IdentityMap

        public IdentityMap()
        Constructs a new, empty identity hash map with a default expected maximum size (21).
      • IdentityMap

        public IdentityMap​(int expectedMaxSize)
        Constructs a new, empty map with the specified expected maximum size. Putting more than the expected number of key-value mappings into the map may cause the internal data structure to grow, which may be somewhat time-consuming.
        Parameters:
        expectedMaxSize - the expected maximum size of the map
        Throws:
        java.lang.IllegalArgumentException - if expectedMaxSize is negative
    • Method Detail

      • capacity

        private int capacity​(int expectedMaxSize)
        Returns the appropriate capacity for the specified expected maximum size. Returns the smallest power of two between MINIMUM_CAPACITY and MAXIMUM_CAPACITY, inclusive, that is greater than (3 * expectedMaxSize)/2, if such a number exists. Otherwise returns MAXIMUM_CAPACITY. If (3 * expectedMaxSize)/2 is negative, it is assumed that overflow has occurred, and MAXIMUM_CAPACITY is returned.
      • init

        private void init​(int initCapacity)
        Initializes object to be an empty map with the specified initial capacity, which is assumed to be a power of two between MINIMUM_CAPACITY and MAXIMUM_CAPACITY inclusive.
      • hash

        private static int hash​(java.lang.Object x,
                                int length)
        Returns index for Object x.
      • nextKeyIndex

        private static int nextKeyIndex​(int i,
                                        int len)
        Circularly traverses table of size len.
      • shouldIncrement

        public boolean shouldIncrement​(int value,
                                       java.lang.Object k,
                                       WriteSession output,
                                       int fieldNumber)
                                throws java.io.IOException
        Returns true if the provided int should increment(unique index id).
        Throws:
        java.io.IOException
      • resize

        private void resize​(int newCapacity)
        Resize the table to hold given capacity.
        Parameters:
        newCapacity - the new capacity, must be a power of two.