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