All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jfree.chart3d.data.DefaultKeyedValues Maven / Gradle / Ivy

/* ===========================================================
 * Orson Charts : a 3D chart library for the Java(tm) platform
 * ===========================================================
 * 
 * (C)opyright 2013-2020, by Object Refinery Limited.  All rights reserved.
 * 
 * https://github.com/jfree/orson-charts
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see .
 * 
 * [Oracle and Java are registered trademarks of Oracle and/or its affiliates. 
 * Other names may be trademarks of their respective owners.]
 * 
 * If you do not wish to be bound by the terms of the GPL, an alternative
 * commercial license can be purchased.  For details, please see visit the
 * Orson Charts home page:
 * 
 * http://www.object-refinery.com/orsoncharts/index.html
 * 
 */

package org.jfree.chart3d.data;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.jfree.chart3d.internal.Args;

/**
 * A list of {@code (key, value)} pairs.
 * 

* This is the basic structure of the data required for a pie chart. *

* NOTE: This class is serializable, but the serialization format is subject * to change in future releases and should not be relied upon for persisting * instances of this class. * * @param the key type (must implement Comparable). * @param the value type. */ @SuppressWarnings("serial") public final class DefaultKeyedValues, T> implements KeyedValues, Serializable { /** Storage for the data items. */ private List> data; /** * Creates a new (empty) list of keyed values. */ public DefaultKeyedValues() { this(new ArrayList()); } /** * Creates a new instance with the specified keys (each associated with * a {@code null} value). There is usually no need to specify any * keys in advance, so you will normally use the default constructor. This * constructor is provided for the convenience of some internal code. * * @param keys the keys ({@code null} not permitted). */ public DefaultKeyedValues(List keys) { Args.nullNotPermitted(keys, "keys"); this.data = new ArrayList<>(); for (K key : keys) { this.data.add(new DefaultKeyedValue<>(key, null)); } } /** * Clears all the data. */ public void clear() { this.data.clear(); } /** * Adds a value or, if there is an existing value with the same key, updates * an existing value. * * @param key the key ({@code null} not permitted) * @param value the value. */ public void put(K key, T value) { Args.nullNotPermitted(key, "key"); DefaultKeyedValue dkv; int index = getIndex(key); if (index >= 0) { dkv = (DefaultKeyedValue) this.data.get(index); dkv.setValue(value); } else { this.data.add(new DefaultKeyedValue<>(key, value)); } } /** * Removes the item with the specified key, if there is one. * * @param key the key ({@code null} not permitted). */ public void remove(K key) { Args.nullNotPermitted(key, "key"); int index = getIndex(key); if (index >= 0) { remove(index); } } /** * Removes the item with the specified index. * * @param index the index. */ public void remove(int index) { this.data.remove(index); } /** * Returns the key for the item with the specified index. * * @param index the item index. * * @return The key. */ @Override public K getKey(int index) { KeyedValue kv = this.data.get(index); return kv.getKey(); } /** * Returns the index of the item with the specified key, or {@code -1} * if there is no such item. * * @param key the key ({@code null} not permitted). * * @return The item index, or {@code -1}. */ @Override public int getIndex(K key) { Args.nullNotPermitted(key, "key"); for (int i = 0; i < this.data.size(); i++) { KeyedValue kv = this.data.get(i); if (kv.getKey().equals(key)) { return i; } } return -1; } /** * Returns a list of all the keys. Note that the list will be a copy, so * modifying it will not impact this data structure. * * @return A list of keys (possibly empty, but never {@code null}). */ @Override public List getKeys() { List keys = new ArrayList<>(); for (KeyedValue kv : this.data) { keys.add(kv.getKey()); } return keys; } /** * Returns the value with the specified key. * * @param key the key ({@code null} not permitted). * * @return The value (possibly {@code null}). */ @Override public T getValue(K key) { // arg checking is performed by getIndex() int index = getIndex(key); if (index < 0) { return null; } return getValue(index); } /** * Returns the number of items in the list. * * @return The number of items in the list. */ @Override public int getItemCount() { return this.data.size(); } /** * Returns the value for the specified item. * * @param item the item index. * * @return The value (possibly {@code null}). */ @Override public T getValue(int item) { KeyedValue kv = this.data.get(item); return kv.getValue(); } /** * Returns the value for the specified item, as a double primitive, * provided that the data value is an instance of {@code Number}. * * @param item the item index. * * @return The value. */ @Override public double getDoubleValue(int item) { T n = getValue(item); if (n != null && n instanceof Number) { return ((Number) n).doubleValue(); } return Double.NaN; } /** * Tests this instance for equality with an arbitrary object. * * @param obj the object to test against ({@code null} permitted). * * @return A boolean. */ @Override public boolean equals(Object obj) { if (obj == this) { return true; } if (!(obj instanceof DefaultKeyedValues)) { return false; } DefaultKeyedValues that = (DefaultKeyedValues) obj; if (!this.data.equals(that.data)) { return false; } return true; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy