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 * ComparableObjectItem.java 029 * ------------------------- 030 * (C) Copyright 2006-2016, by Object Refinery Limited. 031 * 032 * Original Author: David Gilbert (for Object Refinery Limited); 033 * Contributor(s): -; 034 * 035 * Changes 036 * ------- 037 * 19-Oct-2006 : New class, based on XYDataItem (DG); 038 * 03-Jul-2013 : Use ParamChecks (DG); 039 * 040 */ 041 042package org.jfree.data; 043 044import java.io.Serializable; 045import org.jfree.chart.util.ObjectUtils; 046import org.jfree.chart.util.Args; 047 048/** 049 * Represents one (Comparable, Object) data item for use in a 050 * {@link ComparableObjectSeries}. 051 * 052 * @since 1.0.3 053 */ 054public class ComparableObjectItem implements Cloneable, Comparable, 055 Serializable { 056 057 /** For serialization. */ 058 private static final long serialVersionUID = 2751513470325494890L; 059 060 /** The x-value. */ 061 private Comparable x; 062 063 /** The y-value. */ 064 private Object obj; 065 066 /** 067 * Constructs a new data item. 068 * 069 * @param x the x-value ({@code null} NOT permitted). 070 * @param y the y-value ({@code null} permitted). 071 */ 072 public ComparableObjectItem(Comparable x, Object y) { 073 Args.nullNotPermitted(x, "x"); 074 this.x = x; 075 this.obj = y; 076 } 077 078 /** 079 * Returns the x-value. 080 * 081 * @return The x-value (never {@code null}). 082 */ 083 protected Comparable getComparable() { 084 return this.x; 085 } 086 087 /** 088 * Returns the y-value. 089 * 090 * @return The y-value (possibly {@code null}). 091 */ 092 protected Object getObject() { 093 return this.obj; 094 } 095 096 /** 097 * Sets the y-value for this data item. Note that there is no 098 * corresponding method to change the x-value. 099 * 100 * @param y the new y-value ({@code null} permitted). 101 */ 102 protected void setObject(Object y) { 103 this.obj = y; 104 } 105 106 /** 107 * Returns an integer indicating the order of this object relative to 108 * another object. 109 * <P> 110 * For the order we consider only the x-value: 111 * negative == "less-than", zero == "equal", positive == "greater-than". 112 * 113 * @param o1 the object being compared to. 114 * 115 * @return An integer indicating the order of this data pair object 116 * relative to another object. 117 */ 118 @Override 119 public int compareTo(Object o1) { 120 121 int result; 122 123 // CASE 1 : Comparing to another ComparableObjectItem object 124 // --------------------------------------------------------- 125 if (o1 instanceof ComparableObjectItem) { 126 ComparableObjectItem that = (ComparableObjectItem) o1; 127 return this.x.compareTo(that.x); 128 } 129 130 // CASE 2 : Comparing to a general object 131 // --------------------------------------------- 132 else { 133 // consider these to be ordered after general objects 134 result = 1; 135 } 136 137 return result; 138 139 } 140 141 /** 142 * Returns a clone of this object. 143 * 144 * @return A clone. 145 * 146 * @throws CloneNotSupportedException not thrown by this class, but 147 * subclasses may differ. 148 */ 149 @Override 150 public Object clone() throws CloneNotSupportedException { 151 return super.clone(); 152 } 153 154 /** 155 * Tests if this object is equal to another. 156 * 157 * @param obj the object to test against for equality ({@code null} 158 * permitted). 159 * 160 * @return A boolean. 161 */ 162 @Override 163 public boolean equals(Object obj) { 164 if (obj == this) { 165 return true; 166 } 167 if (!(obj instanceof ComparableObjectItem)) { 168 return false; 169 } 170 ComparableObjectItem that = (ComparableObjectItem) obj; 171 if (!this.x.equals(that.x)) { 172 return false; 173 } 174 if (!ObjectUtils.equal(this.obj, that.obj)) { 175 return false; 176 } 177 return true; 178 } 179 180 /** 181 * Returns a hash code. 182 * 183 * @return A hash code. 184 */ 185 @Override 186 public int hashCode() { 187 int result; 188 result = this.x.hashCode(); 189 result = 29 * result + (this.obj != null ? this.obj.hashCode() : 0); 190 return result; 191 } 192 193}