001/* ===========================================================
002 * JFreeChart : a free chart library for the Java(tm) platform
003 * ===========================================================
004 *
005 * (C) Copyright 2000-present, by David Gilbert and Contributors.
006 *
007 * Project Info:  http://www.jfree.org/jfreechart/index.html
008 *
009 * This library is free software; you can redistribute it and/or modify it
010 * under the terms of the GNU Lesser General Public License as published by
011 * the Free Software Foundation; either version 2.1 of the License, or
012 * (at your option) any later version.
013 *
014 * This library is distributed in the hope that it will be useful, but
015 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
016 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
017 * License for more details.
018 *
019 * You should have received a copy of the GNU Lesser General Public
020 * License along with this library; if not, write to the Free Software
021 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
022 * USA.
023 *
024 * [Oracle and Java are registered trademarks of Oracle and/or its affiliates. 
025 * Other names may be trademarks of their respective owners.]
026 *
027 * -----------------------
028 * DefaultKeyedValues.java
029 * -----------------------
030 * (C) Copyright 2002-present, by David Gilbert.
031 *
032 * Original Author:  David Gilbert;
033 * Contributor(s):   Thomas Morgner;
034 *                   Tracy Hiltbrand (generics for bug fix to PiePlot);
035 *
036 */
037
038package org.jfree.data;
039
040import java.io.Serializable;
041import java.util.ArrayList;
042import java.util.Arrays;
043import java.util.Comparator;
044import java.util.HashMap;
045import java.util.List;
046import java.util.Map;
047import org.jfree.chart.util.Args;
048import org.jfree.chart.util.PublicCloneable;
049import org.jfree.chart.util.SortOrder;
050
051/**
052 * An ordered list of (key, value) items.  This class provides a default
053 * implementation of the {@link KeyedValues} interface.
054 * 
055 * @param <K> the key type ({@code String} is a good default).
056 */
057public class DefaultKeyedValues<K extends Comparable<K>> 
058        implements KeyedValues<K>, Cloneable, PublicCloneable, Serializable {
059
060    /** For serialization. */
061    private static final long serialVersionUID = 8468154364608194797L;
062
063    /** Storage for the keys. */
064    private List<K> keys;
065
066    /** Storage for the values. */
067    private List<Number> values;
068
069    /**
070     * Contains (key, Integer) mappings, where the Integer is the index for
071     * the key in the list.
072     */
073    private Map<K, Integer> indexMap;
074
075  /**
076     * Creates a new collection (initially empty).
077     */
078    public DefaultKeyedValues() {
079        this.keys = new ArrayList<>();
080        this.values = new ArrayList<>();
081        this.indexMap = new HashMap<>();
082    }
083
084    /**
085     * Returns the number of items (values) in the collection.
086     *
087     * @return The item count.
088     */
089    @Override
090    public int getItemCount() {
091        return this.indexMap.size();
092    }
093
094    /**
095     * Returns a value.
096     *
097     * @param item  the item of interest (zero-based index).
098     *
099     * @return The value (possibly {@code null}).
100     *
101     * @throws IndexOutOfBoundsException if {@code item} is out of bounds.
102     */
103    @Override
104    public Number getValue(int item) {
105        return this.values.get(item);
106    }
107
108    /**
109     * Returns a key.
110     *
111     * @param index  the item index (zero-based).
112     *
113     * @return The row key.
114     *
115     * @throws IndexOutOfBoundsException if {@code item} is out of bounds.
116     */
117    @Override
118    public K getKey(int index) {
119        return this.keys.get(index);
120    }
121
122    /**
123     * Returns the index for a given key.
124     *
125     * @param key  the key ({@code null} not permitted).
126     *
127     * @return The index, or {@code -1} if the key is not recognised.
128     *
129     * @throws IllegalArgumentException if {@code key} is
130     *     {@code null}.
131     */
132    @Override
133    public int getIndex(K key) {
134        Args.nullNotPermitted(key, "key");
135        final Integer i = this.indexMap.get(key);
136        if (i == null) {
137            return -1;  // key not found
138        }
139        return i;
140    }
141
142    /**
143     * Returns the keys for the values in the collection.
144     *
145     * @return The keys (never {@code null}).
146     */
147    @Override
148    public List<K> getKeys() {
149        return new ArrayList<>(this.keys);
150    }
151
152    /**
153     * Returns the value for a given key.
154     *
155     * @param key  the key ({@code null} not permitted).
156     *
157     * @return The value (possibly {@code null}).
158     *
159     * @throws UnknownKeyException if the key is not recognised.
160     *
161     * @see #getValue(int)
162     */
163    @Override
164    public Number getValue(K key) {
165        int index = getIndex(key);
166        if (index < 0) {
167            throw new UnknownKeyException("Key not found: " + key);
168        }
169        return getValue(index);
170    }
171
172    /**
173     * Updates an existing value, or adds a new value to the collection.
174     *
175     * @param key  the key ({@code null} not permitted).
176     * @param value  the value.
177     *
178     * @see #addValue(Comparable, Number)
179     */
180    public void addValue(K key, double value) {
181        addValue(key, Double.valueOf(value));
182    }
183
184    /**
185     * Adds a new value to the collection, or updates an existing value.
186     * This method passes control directly to the
187     * {@link #setValue(Comparable, Number)} method.
188     *
189     * @param key  the key ({@code null} not permitted).
190     * @param value  the value ({@code null} permitted).
191     */
192    public void addValue(K key, Number value) {
193        setValue(key, value);
194    }
195
196    /**
197     * Updates an existing value, or adds a new value to the collection.
198     *
199     * @param key  the key ({@code null} not permitted).
200     * @param value  the value.
201     */
202    public void setValue(K key, double value) {
203        setValue(key, Double.valueOf(value));
204    }
205
206    /**
207     * Updates an existing value, or adds a new value to the collection.
208     *
209     * @param key  the key ({@code null} not permitted).
210     * @param value  the value ({@code null} permitted).
211     */
212    public void setValue(K key, Number value) {
213        Args.nullNotPermitted(key, "key");
214        int keyIndex = getIndex(key);
215        if (keyIndex >= 0) {
216            this.keys.set(keyIndex, key);
217            this.values.set(keyIndex, value);
218        }
219        else {
220            this.keys.add(key);
221            this.values.add(value);
222            this.indexMap.put(key, this.keys.size() - 1);
223        }
224    }
225
226    /**
227     * Inserts a new value at the specified position in the dataset or, if
228     * there is an existing item with the specified key, updates the value
229     * for that item and moves it to the specified position.
230     *
231     * @param position  the position (in the range 0 to getItemCount()).
232     * @param key  the key ({@code null} not permitted).
233     * @param value  the value.
234     */
235    public void insertValue(int position, K key, double value) {
236        insertValue(position, key, Double.valueOf(value));
237    }
238
239    /**
240     * Inserts a new value at the specified position in the dataset or, if
241     * there is an existing item with the specified key, updates the value
242     * for that item and moves it to the specified position.
243     *
244     * @param position  the position (in the range 0 to getItemCount()).
245     * @param key  the key ({@code null} not permitted).
246     * @param value  the value ({@code null} permitted).
247     */
248    public void insertValue(int position, K key, Number value) {
249        if (position < 0 || position > getItemCount()) {
250            throw new IllegalArgumentException("'position' out of bounds.");
251        }
252        Args.nullNotPermitted(key, "key");
253        int pos = getIndex(key);
254        if (pos == position) {
255            this.keys.set(pos, key);
256            this.values.set(pos, value);
257        }
258        else {
259            if (pos >= 0) {
260                this.keys.remove(pos);
261                this.values.remove(pos);
262            }
263
264            this.keys.add(position, key);
265            this.values.add(position, value);
266            rebuildIndex();
267        }
268    }
269
270    /**
271     * Rebuilds the key to indexed-position mapping after an positioned insert
272     * or a remove operation.
273     */
274    private void rebuildIndex () {
275        this.indexMap.clear();
276        for (int i = 0; i < this.keys.size(); i++) {
277            final K key = this.keys.get(i);
278            this.indexMap.put(key, i);
279        }
280    }
281
282    /**
283     * Removes a value from the collection.
284     *
285     * @param index  the index of the item to remove (in the range
286     *     {@code 0} to {@code getItemCount() -1}).
287     *
288     * @throws IndexOutOfBoundsException if {@code index} is not within
289     *     the specified range.
290     */
291    public void removeValue(int index) {
292        this.keys.remove(index);
293        this.values.remove(index);
294        rebuildIndex();
295    }
296
297    /**
298     * Removes a value from the collection.
299     *
300     * @param key  the item key ({@code null} not permitted).
301     *
302     * @throws IllegalArgumentException if {@code key} is
303     *     {@code null}.
304     * @throws UnknownKeyException if {@code key} is not recognised.
305     */
306    public void removeValue(K key) {
307        int index = getIndex(key);
308        if (index < 0) {
309            throw new UnknownKeyException("The key (" + key
310                    + ") is not recognised.");
311        }
312        removeValue(index);
313    }
314
315    /**
316     * Clears all values from the collection.
317     */
318    public void clear() {
319        this.keys.clear();
320        this.values.clear();
321        this.indexMap.clear();
322    }
323
324    /**
325     * Sorts the items in the list by key.
326     *
327     * @param order  the sort order ({@code null} not permitted).
328     */
329    public void sortByKeys(SortOrder order) {
330        final int size = this.keys.size();
331        final DefaultKeyedValue<K>[] data = new DefaultKeyedValue[size];
332
333        for (int i = 0; i < size; i++) {
334            data[i] = new DefaultKeyedValue(this.keys.get(i), this.values.get(i));
335        }
336
337        Comparator comparator = new KeyedValueComparator(
338                KeyedValueComparatorType.BY_KEY, order);
339        Arrays.sort(data, comparator);
340        clear();
341
342        for (int i = 0; i < data.length; i++) {
343            final DefaultKeyedValue<K> value = data[i];
344            addValue(value.getKey(), value.getValue());
345        }
346    }
347
348    /**
349     * Sorts the items in the list by value.  If the list contains
350     * {@code null} values, they will sort to the end of the list,
351     * irrespective of the sort order.
352     *
353     * @param order  the sort order ({@code null} not permitted).
354     */
355    public void sortByValues(SortOrder order) {
356        final int size = this.keys.size();
357        final DefaultKeyedValue[] data = new DefaultKeyedValue[size];
358        for (int i = 0; i < size; i++) {
359            data[i] = new DefaultKeyedValue((Comparable) this.keys.get(i),
360                    (Number) this.values.get(i));
361        }
362
363        Comparator comparator = new KeyedValueComparator(
364                KeyedValueComparatorType.BY_VALUE, order);
365        Arrays.sort(data, comparator);
366
367        clear();
368        for (int i = 0; i < data.length; i++) {
369            final DefaultKeyedValue<K> value = data[i];
370            addValue(value.getKey(), value.getValue());
371        }
372    }
373
374    /**
375     * Tests if this object is equal to another.
376     *
377     * @param obj  the object ({@code null} permitted).
378     *
379     * @return A boolean.
380     */
381    @Override
382    public boolean equals(Object obj) {
383        if (obj == this) {
384            return true;
385        }
386
387        if (!(obj instanceof KeyedValues)) {
388            return false;
389        }
390
391        KeyedValues that = (KeyedValues) obj;
392        int count = getItemCount();
393        if (count != that.getItemCount()) {
394            return false;
395        }
396
397        for (int i = 0; i < count; i++) {
398            Comparable k1 = getKey(i);
399            Comparable k2 = that.getKey(i);
400            if (!k1.equals(k2)) {
401                return false;
402            }
403            Number v1 = getValue(i);
404            Number v2 = that.getValue(i);
405            if (v1 == null) {
406                if (v2 != null) {
407                    return false;
408                }
409            }
410            else {
411                if (!v1.equals(v2)) {
412                    return false;
413                }
414            }
415        }
416        return true;
417    }
418
419    /**
420     * Returns a hash code.
421     *
422     * @return A hash code.
423     */
424    @Override
425    public int hashCode() {
426        return (this.keys != null ? this.keys.hashCode() : 0);
427    }
428
429    /**
430     * Returns a clone.
431     *
432     * @return A clone.
433     *
434     * @throws CloneNotSupportedException  this class will not throw this
435     *         exception, but subclasses might.
436     */
437    @Override
438    public Object clone() throws CloneNotSupportedException {
439        DefaultKeyedValues clone = (DefaultKeyedValues) super.clone();
440        clone.keys = new ArrayList<>(this.keys);
441        clone.values = new ArrayList<>(this.values);
442        clone.indexMap = new HashMap(this.indexMap);
443        return clone;
444    }
445
446}