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