001/* ===========================================================
002 * Orson Charts : a 3D chart library for the Java(tm) platform
003 * ===========================================================
004 * 
005 * (C)opyright 2013-2016, by Object Refinery Limited.  All rights reserved.
006 * 
007 * http://www.object-refinery.com/orsoncharts/index.html
008 * 
009 * This program is free software: you can redistribute it and/or modify
010 * it under the terms of the GNU General Public License as published by
011 * the Free Software Foundation, either version 3 of the License, or
012 * (at your option) any later version.
013 *
014 * This program is distributed in the hope that it will be useful,
015 * but WITHOUT ANY WARRANTY; without even the implied warranty of
016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
017 * GNU General Public License for more details.
018 *
019 * You should have received a copy of the GNU General Public License
020 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
021 * 
022 * [Oracle and Java are registered trademarks of Oracle and/or its affiliates. 
023 * Other names may be trademarks of their respective owners.]
024 * 
025 * If you do not wish to be bound by the terms of the GPL, an alternative
026 * commercial license can be purchased.  For details, please see visit the
027 * Orson Charts home page:
028 * 
029 * http://www.object-refinery.com/orsoncharts/index.html
030 * 
031 */
032
033package com.orsoncharts.data;
034
035import java.io.Serializable;
036
037import com.orsoncharts.util.ArgChecks;
038import com.orsoncharts.util.ObjectUtils;
039
040/**
041 * A data item where a key is associated with a value (typically numerical).
042 * <br><br>
043 * NOTE: This class is serializable, but the serialization format is subject 
044 * to change in future releases and should not be relied upon for persisting 
045 * instances of this class. 
046 */
047@SuppressWarnings("serial")
048public final class DefaultKeyedValue<T> implements KeyedValue<T>, Serializable {
049
050    /** The key. */
051    private Comparable<?> key;
052
053    /** The value. */
054    private T value;
055
056    /**
057     * Creates a new instance.
058     * 
059     * @param key  the key ({@code null} not permitted).
060     * @param value  the value.
061     */
062    public DefaultKeyedValue(Comparable<?> key, T value) {
063        ArgChecks.nullNotPermitted(key, "key");
064        this.key = key;
065        this.value = value;
066    }
067
068    /**
069     * Returns the key.
070     * 
071     * @return The key (never {@code null}). 
072     */
073    @Override
074    public Comparable<?> getKey() {
075        return this.key;
076    }
077
078    /**
079     * Returns the value.
080     * 
081     * @return The value (possibly {@code null}). 
082     */
083    @Override
084    public T getValue() {
085        return this.value;
086    }
087  
088    /**
089     * Sets the value.
090     * 
091     * @param value  the value ({@code null} permitted).
092     */
093    public void setValue(T value) {
094        this.value = value;
095    }
096    
097    /**
098     * Tests this instance for equality with an arbitrary object.
099     * 
100     * @param obj  the object to test ({@code null} permitted).
101     * 
102     * @return A boolean.
103     */
104    @Override
105    public boolean equals(Object obj) {
106        if (obj == this) {
107            return true;
108        }
109        if (!(obj instanceof DefaultKeyedValue)) {
110            return false;
111        }
112        DefaultKeyedValue<?> that = (DefaultKeyedValue<?>) obj;
113        if (!this.key.equals(that.key)) {
114            return false;
115        }
116        if (!ObjectUtils.equals(this.value, that.value)) {
117            return false;
118        }
119        return true;
120    }
121  
122    @Override
123    public String toString() {
124        return "(" + this.key.toString() + ", " + value + ")";
125    }
126}