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 * IntervalMarker.java 029 * ------------------- 030 * (C) Copyright 2002-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.BasicStroke; 040import java.awt.Color; 041import java.awt.Paint; 042import java.awt.Stroke; 043import java.io.Serializable; 044import java.util.Objects; 045 046import org.jfree.chart.event.MarkerChangeEvent; 047import org.jfree.chart.ui.GradientPaintTransformer; 048import org.jfree.chart.ui.LengthAdjustmentType; 049 050/** 051 * Represents an interval to be highlighted in some way. 052 */ 053public class IntervalMarker extends Marker implements Cloneable, Serializable { 054 055 /** For serialization. */ 056 private static final long serialVersionUID = -1762344775267627916L; 057 058 /** The start value. */ 059 private double startValue; 060 061 /** The end value. */ 062 private double endValue; 063 064 /** The gradient paint transformer (optional). */ 065 private GradientPaintTransformer gradientPaintTransformer; 066 067 /** 068 * Constructs an interval marker. 069 * 070 * @param start the start of the interval. 071 * @param end the end of the interval. 072 */ 073 public IntervalMarker(double start, double end) { 074 this(start, end, Color.GRAY, new BasicStroke(0.5f), Color.GRAY, 075 new BasicStroke(0.5f), 0.8f); 076 } 077 078 /** 079 * Creates a new interval marker with the specified range and fill paint. 080 * The outline paint and stroke default to {@code null}. 081 * 082 * @param start the lower bound of the interval. 083 * @param end the upper bound of the interval. 084 * @param paint the fill paint ({@code null} not permitted). 085 */ 086 public IntervalMarker(double start, double end, Paint paint) { 087 this(start, end, paint, new BasicStroke(0.5f), null, null, 0.8f); 088 } 089 090 /** 091 * Constructs an interval marker. 092 * 093 * @param start the start of the interval. 094 * @param end the end of the interval. 095 * @param paint the paint ({@code null} not permitted). 096 * @param stroke the stroke ({@code null} not permitted). 097 * @param outlinePaint the outline paint. 098 * @param outlineStroke the outline stroke. 099 * @param alpha the alpha transparency. 100 */ 101 public IntervalMarker(double start, double end, 102 Paint paint, Stroke stroke, 103 Paint outlinePaint, Stroke outlineStroke, 104 float alpha) { 105 106 super(paint, stroke, outlinePaint, outlineStroke, alpha); 107 this.startValue = start; 108 this.endValue = end; 109 this.gradientPaintTransformer = null; 110 setLabelOffsetType(LengthAdjustmentType.CONTRACT); 111 112 } 113 114 /** 115 * Returns the start value for the interval. 116 * 117 * @return The start value. 118 */ 119 public double getStartValue() { 120 return this.startValue; 121 } 122 123 /** 124 * Sets the start value for the marker and sends a 125 * {@link MarkerChangeEvent} to all registered listeners. 126 * 127 * @param value the value. 128 */ 129 public void setStartValue(double value) { 130 this.startValue = value; 131 notifyListeners(new MarkerChangeEvent(this)); 132 } 133 134 /** 135 * Returns the end value for the interval. 136 * 137 * @return The end value. 138 */ 139 public double getEndValue() { 140 return this.endValue; 141 } 142 143 /** 144 * Sets the end value for the marker and sends a 145 * {@link MarkerChangeEvent} to all registered listeners. 146 * 147 * @param value the value. 148 */ 149 public void setEndValue(double value) { 150 this.endValue = value; 151 notifyListeners(new MarkerChangeEvent(this)); 152 } 153 154 /** 155 * Returns the gradient paint transformer. 156 * 157 * @return The gradient paint transformer (possibly {@code null}). 158 */ 159 public GradientPaintTransformer getGradientPaintTransformer() { 160 return this.gradientPaintTransformer; 161 } 162 163 /** 164 * Sets the gradient paint transformer and sends a 165 * {@link MarkerChangeEvent} to all registered listeners. 166 * 167 * @param transformer the transformer ({@code null} permitted). 168 */ 169 public void setGradientPaintTransformer( 170 GradientPaintTransformer transformer) { 171 this.gradientPaintTransformer = transformer; 172 notifyListeners(new MarkerChangeEvent(this)); 173 } 174 175 /** 176 * Tests the marker for equality with an arbitrary object. 177 * 178 * @param obj the object ({@code null} permitted). 179 * 180 * @return A boolean. 181 */ 182 @Override 183 public boolean equals(Object obj) { 184 if (obj == this) { 185 return true; 186 } 187 if (!(obj instanceof IntervalMarker)) { 188 return false; 189 } 190 IntervalMarker that = (IntervalMarker) obj; 191 if (!that.canEqual(this)) { 192 return false; 193 } 194 if (Double.doubleToLongBits(this.startValue) != 195 Double.doubleToLongBits(that.startValue)) { 196 return false; 197 } 198 if (Double.doubleToLongBits(this.endValue) != 199 Double.doubleToLongBits(that.endValue)) { 200 return false; 201 } 202 if (!Objects.equals(this.gradientPaintTransformer, 203 that.gradientPaintTransformer)) { 204 return false; 205 } 206 return super.equals(obj); 207 } 208 209 /** 210 * Ensures symmetry between super/subclass implementations of equals. For 211 * more detail, see http://jqno.nl/equalsverifier/manual/inheritance. 212 * 213 * @param other Object 214 * 215 * @return true ONLY if the parameter is THIS class type 216 */ 217 @Override 218 public boolean canEqual(Object other) { 219 // Solves Problem: equals not symmetric 220 return (other instanceof IntervalMarker); 221 } 222 223 @Override 224 public int hashCode() { 225 int hash = super.hashCode(); // equals calls superclass, hashCode must also 226 hash = 47 * hash + 227 (int) (Double.doubleToLongBits(this.startValue) ^ 228 (Double.doubleToLongBits(this.startValue) >>> 32)); 229 hash = 47 * hash + 230 (int) (Double.doubleToLongBits(this.endValue) ^ 231 (Double.doubleToLongBits(this.endValue) >>> 32)); 232 hash = 47 * hash + Objects.hashCode(this.gradientPaintTransformer); 233 return hash; 234 } 235 236 /** 237 * Returns a clone of the marker. 238 * 239 * @return A clone. 240 * 241 * @throws CloneNotSupportedException Not thrown by this class, but the 242 * exception is declared for the use of subclasses. 243 */ 244 @Override 245 public Object clone() throws CloneNotSupportedException { 246 return super.clone(); 247 } 248 249}