Package com.yahoo.document.datatypes
Class WeightedSet<K extends FieldValue>
- java.lang.Object
-
- com.yahoo.vespa.objects.Selectable
-
- com.yahoo.vespa.objects.Identifiable
-
- com.yahoo.document.datatypes.FieldValue
-
- com.yahoo.document.datatypes.CompositeFieldValue
-
- com.yahoo.document.datatypes.CollectionFieldValue<K>
-
- com.yahoo.document.datatypes.WeightedSet<K>
-
- All Implemented Interfaces:
Cloneable
,Comparable<FieldValue>
,Map<K,Integer>
public final class WeightedSet<K extends FieldValue> extends CollectionFieldValue<K> implements Map<K,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
-
-
Field Summary
-
Fields inherited from class com.yahoo.document.datatypes.FieldValue
classId
-
-
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(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 resizingWeightedSet
clone()
int
compareTo(FieldValue fieldValue)
boolean
contains(Object o)
boolean
containsKey(Object key)
Checks whether this set contains the specified key.boolean
containsValue(Object value)
void
deserialize(Field field, FieldReader reader)
Read a field value from the specified readerSet<Map.Entry<K,Integer>>
entrySet()
boolean
equals(Object o)
Checks if another object is equal to this set.Iterator<K>
fieldValueIterator()
Integer
get(Object key)
Returns the weight associated with the specified key.WeightedSetDataType
getDataType()
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.Iterator<K>
iterator()
Set<K>
keySet()
void
printXml(XmlStream xml)
Integer
put(K key, Integer weight)
Add a key with an associated weight to this set.void
putAll(Map<? extends K,? extends Integer> t)
IntegerFieldValue
putUnChecked(K key, IntegerFieldValue weight)
Add a key with an associated weight to this set.Integer
remove(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 writerint
size()
Returns the number of key-weight pairs in this set.String
toString()
Uses toString() from the encapsulated Map.Collection<Integer>
values()
-
Methods inherited from class com.yahoo.document.datatypes.CollectionFieldValue
createFieldValue, isEmpty, removeValue, verifyElementCompatibility
-
Methods inherited from class com.yahoo.document.datatypes.CompositeFieldValue
setDataType
-
Methods inherited from class com.yahoo.document.datatypes.FieldValue
create, deserialize, getRecursiveValue, getRecursiveValue, onDeserialize, onSerialize, serialize, toXml
-
Methods inherited from class com.yahoo.vespa.objects.Identifiable
create, createFromId, deserialize, deserializeOptional, deserializeWithId, equals, getClassId, getRawUtf8Bytes, getUtf8, onGetClassId, putUtf8, registerClass, serialize, serializeOptional, serializeWithId, visitMembers
-
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
-
getDataType
public WeightedSetDataType getDataType()
- Overrides:
getDataType
in classCollectionFieldValue<K extends FieldValue>
-
fieldValueIterator
public Iterator<K> fieldValueIterator()
- Specified by:
fieldValueIterator
in classCollectionFieldValue<K extends FieldValue>
-
assign
public void assign(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 classFieldValue
-
clone
public WeightedSet clone()
- Overrides:
clone
in classFieldValue
-
printXml
public void printXml(XmlStream xml)
- Specified by:
printXml
in classFieldValue
-
size
public int size()
Returns the number of key-weight pairs in this set.- Specified by:
size
in interfaceMap<K extends FieldValue,Integer>
- Specified by:
size
in classCollectionFieldValue<K extends FieldValue>
- Returns:
- the number of key-weight pairs in this set
-
add
public boolean add(K value)
- Specified by:
add
in classCollectionFieldValue<K extends FieldValue>
-
getWrappedValue
public 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 classFieldValue
-
contains
public boolean contains(Object o)
- Specified by:
contains
in classCollectionFieldValue<K extends FieldValue>
-
isEmpty
public boolean isEmpty()
Checks if this set is empty.- Specified by:
isEmpty
in interfaceMap<K extends FieldValue,Integer>
- Specified by:
isEmpty
in classCollectionFieldValue<K extends FieldValue>
- Returns:
- true if the set is empty
-
iterator
public Iterator<K> iterator()
- Specified by:
iterator
in classCollectionFieldValue<K extends FieldValue>
-
removeValue
public boolean removeValue(FieldValue o)
- Specified by:
removeValue
in classCollectionFieldValue<K extends FieldValue>
-
containsKey
public boolean containsKey(Object key)
Checks whether this set contains the specified key.- Specified by:
containsKey
in interfaceMap<K extends FieldValue,Integer>
- Parameters:
key
- the key to search for- Returns:
- true if this set contains this key
-
containsValue
public boolean containsValue(Object value)
- Specified by:
containsValue
in interfaceMap<K extends FieldValue,Integer>
-
get
public Integer get(Object key)
Returns the weight associated with the specified key.- Specified by:
get
in interfaceMap<K extends FieldValue,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 Integer put(K key, 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 interfaceMap<K extends FieldValue,Integer>
- Parameters:
key
- the key to addweight
- 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 addweight
- 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 Integer remove(Object key)
Remove a key-weight association from this set.- Specified by:
remove
in interfaceMap<K extends FieldValue,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(Map<? extends K,? extends Integer> t)
- Specified by:
putAll
in interfaceMap<K extends FieldValue,Integer>
-
clear
public void clear()
Remove all key-weight associations in this set.- Specified by:
clear
in interfaceMap<K extends FieldValue,Integer>
- Specified by:
clear
in classFieldValue
-
clearAndReserve
public void clearAndReserve(int count)
Reserve space for this amount of keys in order to avoid resizing
-
values
public Collection<Integer> values()
- Specified by:
values
in interfaceMap<K extends FieldValue,Integer>
-
entrySet
public Set<Map.Entry<K,Integer>> entrySet()
- Specified by:
entrySet
in interfaceMap<K extends FieldValue,Integer>
-
equals
public boolean equals(Object o)
Checks if another object is equal to this set.- Specified by:
equals
in interfaceMap<K extends FieldValue,Integer>
- Overrides:
equals
in classCompositeFieldValue
- 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 interfaceMap<K extends FieldValue,Integer>
- Overrides:
hashCode
in classCompositeFieldValue
- Returns:
- the hash code of this set
-
toString
public String toString()
Uses toString() from the encapsulated Map.- Overrides:
toString
in classcom.yahoo.vespa.objects.Identifiable
- Returns:
- the toString() of this set
-
serialize
public void serialize(Field field, FieldWriter writer)
Description copied from class:FieldValue
Write out field value to the specified writer- Specified by:
serialize
in classFieldValue
-
deserialize
public void deserialize(Field field, FieldReader reader)
Description copied from class:FieldValue
Read a field value from the specified reader- Specified by:
deserialize
in classFieldValue
-
compareTo
public int compareTo(FieldValue fieldValue)
- Specified by:
compareTo
in interfaceComparable<K extends FieldValue>
- Overrides:
compareTo
in classFieldValue
-
-