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