Package org.jfree.data
Class DefaultKeyedValues<K extends java.lang.Comparable<K>>
- java.lang.Object
-
- org.jfree.data.DefaultKeyedValues<K>
-
- Type Parameters:
K- the key type (Stringis a good default).
- All Implemented Interfaces:
java.io.Serializable,java.lang.Cloneable,PublicCloneable,KeyedValues<K>,Values
public class DefaultKeyedValues<K extends java.lang.Comparable<K>> extends java.lang.Object implements KeyedValues<K>, java.lang.Cloneable, PublicCloneable, java.io.Serializable
An ordered list of (key, value) items. This class provides a default implementation of theKeyedValuesinterface.- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description private java.util.Map<K,java.lang.Integer>indexMapContains (key, Integer) mappings, where the Integer is the index for the key in the list.private java.util.List<K>keysStorage for the keys.private static longserialVersionUIDFor serialization.private java.util.List<java.lang.Number>valuesStorage for the values.
-
Constructor Summary
Constructors Constructor Description DefaultKeyedValues()Creates a new collection (initially empty).
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddValue(K key, double value)Updates an existing value, or adds a new value to the collection.voidaddValue(K key, java.lang.Number value)Adds a new value to the collection, or updates an existing value.voidclear()Clears all values from the collection.java.lang.Objectclone()Returns a clone.booleanequals(java.lang.Object obj)Tests if this object is equal to another.intgetIndex(K key)Returns the index for a given key.intgetItemCount()Returns the number of items (values) in the collection.KgetKey(int index)Returns a key.java.util.List<K>getKeys()Returns the keys for the values in the collection.java.lang.NumbergetValue(int item)Returns a value.java.lang.NumbergetValue(K key)Returns the value for a given key.inthashCode()Returns a hash code.voidinsertValue(int position, K key, double value)Inserts a new value at the specified position in the dataset or, if there is an existing item with the specified key, updates the value for that item and moves it to the specified position.voidinsertValue(int position, K key, java.lang.Number value)Inserts a new value at the specified position in the dataset or, if there is an existing item with the specified key, updates the value for that item and moves it to the specified position.private voidrebuildIndex()Rebuilds the key to indexed-position mapping after an positioned insert or a remove operation.voidremoveValue(int index)Removes a value from the collection.voidremoveValue(K key)Removes a value from the collection.voidsetValue(K key, double value)Updates an existing value, or adds a new value to the collection.voidsetValue(K key, java.lang.Number value)Updates an existing value, or adds a new value to the collection.voidsortByKeys(SortOrder order)Sorts the items in the list by key.voidsortByValues(SortOrder order)Sorts the items in the list by value.
-
-
-
Field Detail
-
serialVersionUID
private static final long serialVersionUID
For serialization.- See Also:
- Constant Field Values
-
values
private java.util.List<java.lang.Number> values
Storage for the values.
-
-
Method Detail
-
getItemCount
public int getItemCount()
Returns the number of items (values) in the collection.- Specified by:
getItemCountin interfaceValues- Returns:
- The item count.
-
getValue
public java.lang.Number getValue(int item)
Returns a value.
-
getKey
public K getKey(int index)
Returns a key.- Specified by:
getKeyin interfaceKeyedValues<K extends java.lang.Comparable<K>>- Parameters:
index- the item index (zero-based).- Returns:
- The row key.
- Throws:
java.lang.IndexOutOfBoundsException- ifitemis out of bounds.
-
getIndex
public int getIndex(K key)
Returns the index for a given key.- Specified by:
getIndexin interfaceKeyedValues<K extends java.lang.Comparable<K>>- Parameters:
key- the key (nullnot permitted).- Returns:
- The index, or
-1if the key is not recognised. - Throws:
java.lang.IllegalArgumentException- ifkeyisnull.
-
getKeys
public java.util.List<K> getKeys()
Returns the keys for the values in the collection.- Specified by:
getKeysin interfaceKeyedValues<K extends java.lang.Comparable<K>>- Returns:
- The keys (never
null).
-
getValue
public java.lang.Number getValue(K key)
Returns the value for a given key.- Specified by:
getValuein interfaceKeyedValues<K extends java.lang.Comparable<K>>- Parameters:
key- the key (nullnot permitted).- Returns:
- The value (possibly
null). - Throws:
UnknownKeyException- if the key is not recognised.- See Also:
getValue(int)
-
addValue
public void addValue(K key, double value)
Updates an existing value, or adds a new value to the collection.- Parameters:
key- the key (nullnot permitted).value- the value.- See Also:
addValue(Comparable, Number)
-
addValue
public void addValue(K key, java.lang.Number value)
Adds a new value to the collection, or updates an existing value. This method passes control directly to thesetValue(Comparable, Number)method.- Parameters:
key- the key (nullnot permitted).value- the value (nullpermitted).
-
setValue
public void setValue(K key, double value)
Updates an existing value, or adds a new value to the collection.- Parameters:
key- the key (nullnot permitted).value- the value.
-
setValue
public void setValue(K key, java.lang.Number value)
Updates an existing value, or adds a new value to the collection.- Parameters:
key- the key (nullnot permitted).value- the value (nullpermitted).
-
insertValue
public void insertValue(int position, K key, double value)Inserts a new value at the specified position in the dataset or, if there is an existing item with the specified key, updates the value for that item and moves it to the specified position.- Parameters:
position- the position (in the range 0 to getItemCount()).key- the key (nullnot permitted).value- the value.
-
insertValue
public void insertValue(int position, K key, java.lang.Number value)Inserts a new value at the specified position in the dataset or, if there is an existing item with the specified key, updates the value for that item and moves it to the specified position.- Parameters:
position- the position (in the range 0 to getItemCount()).key- the key (nullnot permitted).value- the value (nullpermitted).
-
rebuildIndex
private void rebuildIndex()
Rebuilds the key to indexed-position mapping after an positioned insert or a remove operation.
-
removeValue
public void removeValue(int index)
Removes a value from the collection.- Parameters:
index- the index of the item to remove (in the range0togetItemCount() -1).- Throws:
java.lang.IndexOutOfBoundsException- ifindexis not within the specified range.
-
removeValue
public void removeValue(K key)
Removes a value from the collection.- Parameters:
key- the item key (nullnot permitted).- Throws:
java.lang.IllegalArgumentException- ifkeyisnull.UnknownKeyException- ifkeyis not recognised.
-
clear
public void clear()
Clears all values from the collection.
-
sortByKeys
public void sortByKeys(SortOrder order)
Sorts the items in the list by key.- Parameters:
order- the sort order (nullnot permitted).
-
sortByValues
public void sortByValues(SortOrder order)
Sorts the items in the list by value. If the list containsnullvalues, they will sort to the end of the list, irrespective of the sort order.- Parameters:
order- the sort order (nullnot permitted).
-
equals
public boolean equals(java.lang.Object obj)
Tests if this object is equal to another.- Overrides:
equalsin classjava.lang.Object- Parameters:
obj- the object (nullpermitted).- Returns:
- A boolean.
-
hashCode
public int hashCode()
Returns a hash code.- Overrides:
hashCodein classjava.lang.Object- Returns:
- A hash code.
-
clone
public java.lang.Object clone() throws java.lang.CloneNotSupportedExceptionReturns a clone.- Specified by:
clonein interfacePublicCloneable- Overrides:
clonein classjava.lang.Object- Returns:
- A clone.
- Throws:
java.lang.CloneNotSupportedException- this class will not throw this exception, but subclasses might.
-
-