Class WeightedSet<K extends FieldValue>

  • All Implemented Interfaces:
    java.lang.Cloneable, java.lang.Comparable<FieldValue>, java.util.Map<K,​java.lang.Integer>

    public final class WeightedSet<K extends FieldValue>
    extends CollectionFieldValue<K>
    implements java.util.Map<K,​java.lang.Integer>
    A weighted set, a unique set of keys with an associated integer weight. This class uses an encapsulated Map (actually a LinkedHashMap) that associates each key with its weight (value).
    Author:
    Einar M R Rosenvinge
    • 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
      WeightedSet​(DataType type)
      Creates a new WeightedSet.
      WeightedSet​(DataType type, int initialCapacity)
      Creates a new weighted set with a given initial capacity.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      boolean add​(K value)  
      void assign​(java.lang.Object o)
      Assign this non-fieldvalue value to this field value.
      void clear()
      Remove all key-weight associations in this set.
      void clearAndReserve​(int count)
      Reserve space for this amount of keys in order to avoid resizing
      WeightedSet clone()  
      int compareTo​(FieldValue fieldValue)  
      boolean contains​(java.lang.Object o)  
      boolean containsKey​(java.lang.Object key)
      Checks whether this set contains the specified key.
      boolean containsValue​(java.lang.Object value)  
      void deserialize​(Field field, FieldReader reader)
      Read a field value from the specified reader
      java.util.Set<java.util.Map.Entry<K,​java.lang.Integer>> entrySet()  
      boolean equals​(java.lang.Object o)
      Checks if another object is equal to this set.
      java.util.Iterator<K> fieldValueIterator()  
      java.lang.Integer get​(java.lang.Object key)
      Returns the weight associated with the specified key.
      WeightedSetDataType getDataType()  
      java.lang.Object getWrappedValue()
      Used to retrieve wrapped type for simple types, such that you can use get methods to retrieve ints and floats directly instead of Int/Float field values.
      int hashCode()
      Uses hashCode() from the encapsulated Map.
      boolean isEmpty()
      Checks if this set is empty.
      java.util.Iterator<K> iterator()  
      java.util.Set<K> keySet()  
      void printXml​(XmlStream xml)  
      java.lang.Integer put​(K key, java.lang.Integer weight)
      Add a key with an associated weight to this set.
      void putAll​(java.util.Map<? extends K,​? extends java.lang.Integer> t)  
      IntegerFieldValue putUnChecked​(K key, IntegerFieldValue weight)
      Add a key with an associated weight to this set.
      java.lang.Integer remove​(java.lang.Object key)
      Remove a key-weight association from this set.
      boolean removeValue​(FieldValue o)  
      void serialize​(Field field, FieldWriter writer)
      Write out field value to the specified writer
      int size()
      Returns the number of key-weight pairs in this set.
      java.lang.String toString()
      Uses toString() from the encapsulated Map.
      java.util.Collection<java.lang.Integer> values()  
      • Methods inherited from class com.yahoo.vespa.objects.Identifiable

        create, createFromId, deserialize, deserializeOptional, deserializeWithId, equals, getClassId, getUtf8, onGetClassId, putUtf8, registerClass, serialize, serializeOptional, serializeWithId, visitMembers
      • Methods inherited from class com.yahoo.vespa.objects.Selectable

        select, select, selectMembers
      • Methods inherited from class java.lang.Object

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

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

      • WeightedSet

        public WeightedSet​(DataType type)
        Creates a new WeightedSet.
        Parameters:
        type - the data type for the field that this weighted set is associated with
      • WeightedSet

        public WeightedSet​(DataType type,
                           int initialCapacity)
        Creates a new weighted set with a given initial capacity.
        Parameters:
        initialCapacity - the initial capacity to use for the encapsulated Map
    • Method Detail

      • assign

        public void assign​(java.lang.Object o)
        Description copied from class: FieldValue
        Assign this non-fieldvalue value to this field value. This is used to be able to assign ints to Integer field values and List to Array field values and such.

        Override to accept the specific types that should be legal.

        Specified by:
        assign in class FieldValue
      • size

        public int size()
        Returns the number of key-weight pairs in this set.
        Specified by:
        size in interface java.util.Map<K extends FieldValue,​java.lang.Integer>
        Specified by:
        size in class CollectionFieldValue<K extends FieldValue>
        Returns:
        the number of key-weight pairs in this set
      • getWrappedValue

        public java.lang.Object getWrappedValue()
        Description copied from class: FieldValue
        Used to retrieve wrapped type for simple types, such that you can use get methods to retrieve ints and floats directly instead of Int/Float field values. Complex types that can't be specified by simple java types just return themself.
        Overrides:
        getWrappedValue in class FieldValue
      • isEmpty

        public boolean isEmpty()
        Checks if this set is empty.
        Specified by:
        isEmpty in interface java.util.Map<K extends FieldValue,​java.lang.Integer>
        Specified by:
        isEmpty in class CollectionFieldValue<K extends FieldValue>
        Returns:
        true if the set is empty
      • containsKey

        public boolean containsKey​(java.lang.Object key)
        Checks whether this set contains the specified key.
        Specified by:
        containsKey in interface java.util.Map<K extends FieldValue,​java.lang.Integer>
        Parameters:
        key - the key to search for
        Returns:
        true if this set contains this key
      • containsValue

        public boolean containsValue​(java.lang.Object value)
        Specified by:
        containsValue in interface java.util.Map<K extends FieldValue,​java.lang.Integer>
      • get

        public java.lang.Integer get​(java.lang.Object key)
        Returns the weight associated with the specified key.
        Specified by:
        get in interface java.util.Map<K extends FieldValue,​java.lang.Integer>
        Parameters:
        key - the key to return the weight for
        Returns:
        the weight associated with the specified key, or null (if not found)
      • put

        public java.lang.Integer put​(K key,
                                     java.lang.Integer weight)
        Add a key with an associated weight to this set. If the key is already present in this set, the previous association is replaced. Checks to validate that all keys are of the same type.
        Specified by:
        put in interface java.util.Map<K extends FieldValue,​java.lang.Integer>
        Parameters:
        key - the key to add
        weight - the weight to associate with this key
        Returns:
        the weight that was previously associated with this key, or null (if there was no previous key)
      • putUnChecked

        public IntegerFieldValue putUnChecked​(K key,
                                              IntegerFieldValue weight)
        Add a key with an associated weight to this set. If the key is already present in this set, the previous association is replaced.
        Parameters:
        key - the key to add
        weight - the weight to associate with this key
        Returns:
        the weight that was previously associated with this key, or null (if there was no previous key)
      • remove

        public java.lang.Integer remove​(java.lang.Object key)
        Remove a key-weight association from this set.
        Specified by:
        remove in interface java.util.Map<K extends FieldValue,​java.lang.Integer>
        Parameters:
        key - the key to remove
        Returns:
        the weight that was previously associated with this key, or null (if there was no previous key)
      • putAll

        public void putAll​(java.util.Map<? extends K,​? extends java.lang.Integer> t)
        Specified by:
        putAll in interface java.util.Map<K extends FieldValue,​java.lang.Integer>
      • clear

        public void clear()
        Remove all key-weight associations in this set.
        Specified by:
        clear in interface java.util.Map<K extends FieldValue,​java.lang.Integer>
        Specified by:
        clear in class FieldValue
      • clearAndReserve

        public void clearAndReserve​(int count)
        Reserve space for this amount of keys in order to avoid resizing
      • values

        public java.util.Collection<java.lang.Integer> values()
        Specified by:
        values in interface java.util.Map<K extends FieldValue,​java.lang.Integer>
      • keySet

        public java.util.Set<K> keySet()
        Specified by:
        keySet in interface java.util.Map<K extends FieldValue,​java.lang.Integer>
      • entrySet

        public java.util.Set<java.util.Map.Entry<K,​java.lang.Integer>> entrySet()
        Specified by:
        entrySet in interface java.util.Map<K extends FieldValue,​java.lang.Integer>
      • equals

        public boolean equals​(java.lang.Object o)
        Checks if another object is equal to this set.
        Specified by:
        equals in interface java.util.Map<K extends FieldValue,​java.lang.Integer>
        Overrides:
        equals in class CompositeFieldValue
        Parameters:
        o - the object to check for equality with
        Returns:
        true if o is an instance of WeightedSet and the two encapsulated Maps are equal, false otherwise
      • hashCode

        public int hashCode()
        Uses hashCode() from the encapsulated Map.
        Specified by:
        hashCode in interface java.util.Map<K extends FieldValue,​java.lang.Integer>
        Overrides:
        hashCode in class CompositeFieldValue
        Returns:
        the hash code of this set
      • toString

        public java.lang.String toString()
        Uses toString() from the encapsulated Map.
        Overrides:
        toString in class com.yahoo.vespa.objects.Identifiable
        Returns:
        the toString() of this set