001/* ===========================================================
002 * JFreeChart : a free chart library for the Java(tm) platform
003 * ===========================================================
004 *
005 * (C) Copyright 2000-2020, by Object Refinery Limited 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-2020, by Object Refinery Limited.
031 *
032 * Original Author:  David Gilbert (for Object Refinery Limited);
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 * @since 1.3
051 */
052public class KeyedValues2DItemKey<R extends Comparable<R>, 
053        C extends Comparable<C>> implements ItemKey, 
054        Comparable<KeyedValues2DItemKey<R, C>>, Serializable {
055    
056    /** The row key. */
057    R rowKey;
058    
059    /** The column key. */
060    C columnKey;
061    
062    /**
063     * Creates a new instance.
064     * 
065     * @param rowKey  the row key ({@code null} not permitted).
066     * @param columnKey  the column key ({@code null} not permitted).
067     */
068    public KeyedValues2DItemKey(R rowKey, C columnKey) {
069        Args.nullNotPermitted(rowKey, "rowKey");
070        Args.nullNotPermitted(columnKey, "columnKey");
071        this.rowKey = rowKey;
072        this.columnKey = columnKey;
073    }
074    
075    /**
076     * Returns the row key.
077     * 
078     * @return The row key (never {@code null}).
079     */
080    public R getRowKey() {
081        return this.rowKey;
082    }
083    
084    /**
085     * Returns the column key.
086     * 
087     * @return The column key (never {@code null}).
088     */
089    public C getColumnKey() {
090        return this.columnKey;
091    }
092    
093    @Override
094    public int compareTo(KeyedValues2DItemKey<R, C> key) {
095        int result = this.rowKey.compareTo(key.rowKey);
096        if (result == 0) {
097            result = this.columnKey.compareTo(key.columnKey);
098        }
099        return result;
100    }
101    
102    /**
103     * Tests this key for equality with an arbitrary object.
104     * 
105     * @param obj  the object ({@code null} permitted).
106     * 
107     * @return A boolean. 
108     */
109    @Override
110    public boolean equals(Object obj) {
111        if (obj == this) {
112            return true;
113        }
114        if (!(obj instanceof KeyedValues2DItemKey)) {
115            return false;
116        }
117        KeyedValues2DItemKey that = (KeyedValues2DItemKey) obj;
118        if (!this.rowKey.equals(that.rowKey)) {
119            return false;
120        }
121        if (!this.columnKey.equals(that.columnKey)) {
122            return false;
123        }
124        return true;
125    }
126
127    @Override
128    public int hashCode() {
129        int hash = 3;
130        hash = 17 * hash + ObjectUtils.hashCode(this.rowKey);
131        hash = 17 * hash + ObjectUtils.hashCode(this.columnKey);
132        return hash;
133    }
134
135    @Override
136    public String toJSONString() {
137        StringBuilder sb = new StringBuilder();
138        sb.append("{\"rowKey\": \"").append(this.rowKey.toString());
139        sb.append("\", ");
140        sb.append("\"columnKey\": \"").append(this.columnKey.toString());
141        sb.append("\"}");
142        return sb.toString();
143    }
144    
145    @Override
146    public String toString() {
147        StringBuilder sb = new StringBuilder();
148        sb.append("Values2DItemKey[row=");
149        sb.append(rowKey.toString()).append(",column=");
150        sb.append(columnKey.toString());
151        sb.append("]");
152        return sb.toString();
153    }
154
155}