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