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 * KeyedValues2DItemKey.java
029 * -------------------------
030 * (C) Copyright 2014-present, by David Gilbert.
031 *
032 * Original Author:  David Gilbert;
033 * Contributor(s):   -;
034 *
035 */
036
037package org.jfree.data;
038
039import java.io.Serializable;
040import org.jfree.chart.util.ObjectUtils;
041import org.jfree.chart.util.Args;
042
043/**
044 * An object that references one data item in a {@link KeyedValues2D} data
045 * structure.  Instances of this class are immutable (subject to the caller
046 * using series, row and column keys that are immutable).
047 * 
048 * @param <R> the row key type.
049 * @param <C> the column key type.
050 */
051public class KeyedValues2DItemKey<R extends Comparable<R>, 
052        C extends Comparable<C>> implements ItemKey, 
053        Comparable<KeyedValues2DItemKey<R, C>>, Serializable {
054    
055    /** The row key. */
056    R rowKey;
057    
058    /** The column key. */
059    C columnKey;
060    
061    /**
062     * Creates a new instance.
063     * 
064     * @param rowKey  the row key ({@code null} not permitted).
065     * @param columnKey  the column key ({@code null} not permitted).
066     */
067    public KeyedValues2DItemKey(R rowKey, C columnKey) {
068        Args.nullNotPermitted(rowKey, "rowKey");
069        Args.nullNotPermitted(columnKey, "columnKey");
070        this.rowKey = rowKey;
071        this.columnKey = columnKey;
072    }
073    
074    /**
075     * Returns the row key.
076     * 
077     * @return The row key (never {@code null}).
078     */
079    public R getRowKey() {
080        return this.rowKey;
081    }
082    
083    /**
084     * Returns the column key.
085     * 
086     * @return The column key (never {@code null}).
087     */
088    public C getColumnKey() {
089        return this.columnKey;
090    }
091    
092    @Override
093    public int compareTo(KeyedValues2DItemKey<R, C> key) {
094        int result = this.rowKey.compareTo(key.rowKey);
095        if (result == 0) {
096            result = this.columnKey.compareTo(key.columnKey);
097        }
098        return result;
099    }
100    
101    /**
102     * Tests this key for equality with an arbitrary object.
103     * 
104     * @param obj  the object ({@code null} permitted).
105     * 
106     * @return A boolean. 
107     */
108    @Override
109    public boolean equals(Object obj) {
110        if (obj == this) {
111            return true;
112        }
113        if (!(obj instanceof KeyedValues2DItemKey)) {
114            return false;
115        }
116        KeyedValues2DItemKey that = (KeyedValues2DItemKey) obj;
117        if (!this.rowKey.equals(that.rowKey)) {
118            return false;
119        }
120        if (!this.columnKey.equals(that.columnKey)) {
121            return false;
122        }
123        return true;
124    }
125
126    @Override
127    public int hashCode() {
128        int hash = 3;
129        hash = 17 * hash + ObjectUtils.hashCode(this.rowKey);
130        hash = 17 * hash + ObjectUtils.hashCode(this.columnKey);
131        return hash;
132    }
133
134    @Override
135    public String toJSONString() {
136        StringBuilder sb = new StringBuilder();
137        sb.append("{\"rowKey\": \"").append(this.rowKey.toString());
138        sb.append("\", ");
139        sb.append("\"columnKey\": \"").append(this.columnKey.toString());
140        sb.append("\"}");
141        return sb.toString();
142    }
143    
144    @Override
145    public String toString() {
146        StringBuilder sb = new StringBuilder();
147        sb.append("Values2DItemKey[row=");
148        sb.append(rowKey.toString()).append(",column=");
149        sb.append(columnKey.toString());
150        sb.append("]");
151        return sb.toString();
152    }
153
154}