/** * Returns the keys for the values in the collection * * @return The keys (never null) * @author Klaus Meffert * @since 2.3 */ public List getKeys() { final List result = new java.util.ArrayList(); final Iterator iterator = m_data.iterator(); while (iterator.hasNext()) { final KeyedValue kv = (KeyedValue) iterator.next(); result.add(kv.getKey()); } return result; }
/** * @param a_index the item index to retrieve the key for, starting at 0 * @return the row key for item at given index * @author Klaus Meffert * @since 2.3 */ public Comparable getKey(final int a_index) { Comparable result; final KeyedValue item = (KeyedValue) m_data.get(a_index); if (item != null) { result = item.getKey(); } else { result = null; } return result; }
/** * @param a_index the index of the item to return the value for * @return the value at given index * @author Klaus Meffert * @since 2.3 */ public Number getValue(final int a_index) { Number result; final KeyedValue kval = (KeyedValue) m_data.get(a_index); if (kval != null) { result = kval.getValue(); } else { result = null; } return result; }
/** * Updates an existing value, or adds a new value to the collection * * @param a_key the key * @param a_value the value * @author Klaus Meffert * @since 2.3 */ public void setValue(final Comparable a_key, final Number a_value) { final int keyIndex = getIndex(a_key); if (keyIndex >= 0) { final KeyedValue kv = (KeyedValue) m_data.get(keyIndex); kv.setValue(a_value); } else { final KeyedValue kv = new KeyedValue(a_key, a_value); m_data.add(kv); } }
/** * @return clone of the current instance * @author Klaus Meffert * @since 2.3 */ public Object clone() { try { final KeyedValues clone = (KeyedValues) super.clone(); clone.m_data = Collections.synchronizedList(new ArrayList()); final Iterator iterator = m_data.iterator(); while (iterator.hasNext()) { final KeyedValue kv = (KeyedValue) iterator.next(); clone.m_data.add(kv.clone()); } return clone; } catch (CloneNotSupportedException cex) { throw new CloneException(cex); } }
/** * @param a_key the key to search for * @return index for a given key or -1 if the key is not found * @author Klaus Meffert * @since 2.3 */ public int getIndex(final Comparable a_key) { int i = 0; final Iterator iterator = m_data.iterator(); while (iterator.hasNext()) { final KeyedValue kv = (KeyedValue) iterator.next(); if (kv.getKey() != null) { if (kv.getKey().equals(a_key)) { return i; } } else { if (a_key == null) { return i; } } i++; } // key not found return -1; }