001/* =========================================================== 002 * JFreeChart : a free chart library for the Java(tm) platform 003 * =========================================================== 004 * 005 * (C) Copyright 2000-present, 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 * ValueMarker.java 029 * ---------------- 030 * (C) Copyright 2004-present, by David Gilbert. 031 * 032 * Original Author: David Gilbert; 033 * Contributor(s): Tracy Hiltbrand (equals/hashCode comply with EqualsVerifier); 034 * 035 */ 036 037package org.jfree.chart.plot; 038 039import java.awt.Paint; 040import java.awt.Stroke; 041 042import org.jfree.chart.event.MarkerChangeEvent; 043 044/** 045 * A marker that represents a single value. Markers can be added to plots to 046 * highlight specific values. 047 */ 048public class ValueMarker extends Marker { 049 050 /** The value. */ 051 private double value; 052 053 /** 054 * Creates a new marker. 055 * 056 * @param value the value. 057 */ 058 public ValueMarker(double value) { 059 super(); 060 this.value = value; 061 } 062 063 /** 064 * Creates a new marker. 065 * 066 * @param value the value. 067 * @param paint the paint ({@code null} not permitted). 068 * @param stroke the stroke ({@code null} not permitted). 069 */ 070 public ValueMarker(double value, Paint paint, Stroke stroke) { 071 this(value, paint, stroke, paint, stroke, 1.0f); 072 } 073 074 /** 075 * Creates a new value marker. 076 * 077 * @param value the value. 078 * @param paint the paint ({@code null} not permitted). 079 * @param stroke the stroke ({@code null} not permitted). 080 * @param outlinePaint the outline paint ({@code null} permitted). 081 * @param outlineStroke the outline stroke ({@code null} permitted). 082 * @param alpha the alpha transparency (in the range 0.0f to 1.0f). 083 */ 084 public ValueMarker(double value, Paint paint, Stroke stroke, 085 Paint outlinePaint, Stroke outlineStroke, float alpha) { 086 super(paint, stroke, outlinePaint, outlineStroke, alpha); 087 this.value = value; 088 } 089 090 /** 091 * Returns the value. 092 * 093 * @return The value. 094 * 095 * @see #setValue(double) 096 */ 097 public double getValue() { 098 return this.value; 099 } 100 101 /** 102 * Sets the value for the marker and sends a {@link MarkerChangeEvent} to 103 * all registered listeners. 104 * 105 * @param value the value. 106 * 107 * @see #getValue() 108 */ 109 public void setValue(double value) { 110 this.value = value; 111 notifyListeners(new MarkerChangeEvent(this)); 112 } 113 114 /** 115 * Tests this marker for equality with an arbitrary object. This method 116 * returns {@code true} if: 117 * 118 * <ul> 119 * <li>{@code obj} is not {@code null};</li> 120 * <li>{@code obj} is an instance of {@code ValueMarker};</li> 121 * <li>{@code obj} has the same value as this marker;</li> 122 * <li>{@code super.equals(obj)} returns {@code true}.</li> 123 * </ul> 124 * 125 * @param obj the object ({@code null} permitted). 126 * 127 * @return A boolean. 128 */ 129 @Override 130 public boolean equals(Object obj) { 131 if (obj == this) { 132 return true; 133 } 134 if (!(obj instanceof ValueMarker)) { 135 return false; 136 } 137 ValueMarker that = (ValueMarker) obj; 138 if (!that.canEqual(this)) { 139 return false; 140 } 141 if (Double.doubleToLongBits(this.value) != 142 Double.doubleToLongBits(that.value)) { 143 return false; 144 } 145 return super.equals(obj); 146 } 147 148 /** 149 * Ensures symmetry between super/subclass implementations of equals. For 150 * more detail, see http://jqno.nl/equalsverifier/manual/inheritance. 151 * 152 * @param other Object 153 * 154 * @return true ONLY if the parameter is THIS class type 155 */ 156 @Override 157 public boolean canEqual(Object other) 158 { 159 // Solves Problem: equals not symmetric 160 return (other instanceof ValueMarker); 161 } 162 163 @Override 164 public int hashCode() { 165 int hash = super.hashCode(); // equals calls superclass, hashCode must also 166 hash = 47 * hash + 167 (int) (Double.doubleToLongBits(this.value) ^ 168 (Double.doubleToLongBits(this.value) >>> 32)); 169 return hash; 170 } 171}