/** * 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); } }
/** * Compares two {@link KeyedValue} instances and returns an <code>int</code> that indicates the * relative order of the two objects. * * @param o1 object 1. * @param o2 object 2. * @return An int indicating the relative order of the objects. */ @Override public int compare(Object o1, Object o2) { if (o2 == null) { return -1; } if (o1 == null) { return 1; } int result; KeyedValue kv1 = (KeyedValue) o1; KeyedValue kv2 = (KeyedValue) o2; if (this.type == KeyedValueComparatorType.BY_KEY) { if (this.order.equals(SortOrder.ASCENDING)) { result = kv1.getKey().compareTo(kv2.getKey()); } else if (this.order.equals(SortOrder.DESCENDING)) { result = kv2.getKey().compareTo(kv1.getKey()); } else { throw new IllegalArgumentException("Unrecognised sort order."); } } else if (this.type == KeyedValueComparatorType.BY_VALUE) { Number n1 = kv1.getValue(); Number n2 = kv2.getValue(); if (n2 == null) { return -1; } if (n1 == null) { return 1; } double d1 = n1.doubleValue(); double d2 = n2.doubleValue(); if (this.order.equals(SortOrder.ASCENDING)) { if (d1 > d2) { result = 1; } else if (d1 < d2) { result = -1; } else { result = 0; } } else if (this.order.equals(SortOrder.DESCENDING)) { if (d1 > d2) { result = -1; } else if (d1 < d2) { result = 1; } else { result = 0; } } else { throw new IllegalArgumentException("Unrecognised sort order."); } } else { throw new IllegalArgumentException("Unrecognised type."); } return result; }
/** * @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; }