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 * CategoryMarker.java
029 * -------------------
030 * (C) Copyright 2005-present, by David Gilbert.
031 *
032 * Original Author:  David Gilbert;
033 * Contributor(s):   Nicolas Brodu;
034 *                   Tracy Hiltbrand (equals/hashCode comply with EqualsVerifier);
035 *
036 */
037
038package org.jfree.chart.plot;
039
040import java.awt.BasicStroke;
041import java.awt.Color;
042import java.awt.Paint;
043import java.awt.Stroke;
044import java.io.Serializable;
045import java.util.Objects;
046
047import org.jfree.chart.event.MarkerChangeEvent;
048import org.jfree.chart.ui.LengthAdjustmentType;
049import org.jfree.chart.util.Args;
050
051/**
052 * A marker for a category.
053 * <br><br>
054 * Note that for serialization to work correctly, the category key must be an
055 * instance of a serializable class.
056 *
057 * @see CategoryPlot#addDomainMarker(CategoryMarker)
058 */
059public class CategoryMarker extends Marker implements Cloneable, Serializable {
060
061    /** The category key. */
062    private Comparable key;
063
064    /**
065     * A hint that the marker should be drawn as a line rather than a region.
066     */
067    private boolean drawAsLine = false;
068
069    /**
070     * Creates a new category marker for the specified category.
071     *
072     * @param key  the category key.
073     */
074    public CategoryMarker(Comparable key) {
075        this(key, Color.GRAY, new BasicStroke(1.0f));
076    }
077
078    /**
079     * Creates a new category marker.
080     *
081     * @param key  the key.
082     * @param paint  the paint ({@code null} not permitted).
083     * @param stroke  the stroke ({@code null} not permitted).
084     */
085    public CategoryMarker(Comparable key, Paint paint, Stroke stroke) {
086        this(key, paint, stroke, paint, stroke, 1.0f);
087    }
088
089    /**
090     * Creates a new category marker.
091     *
092     * @param key  the key.
093     * @param paint  the paint ({@code null} not permitted).
094     * @param stroke  the stroke ({@code null} not permitted).
095     * @param outlinePaint  the outline paint ({@code null} permitted).
096     * @param outlineStroke  the outline stroke ({@code null} permitted).
097     * @param alpha  the alpha transparency.
098     */
099    public CategoryMarker(Comparable key, Paint paint, Stroke stroke,
100                          Paint outlinePaint, Stroke outlineStroke,
101                          float alpha) {
102        super(paint, stroke, outlinePaint, outlineStroke, alpha);
103        this.key = key;
104        setLabelOffsetType(LengthAdjustmentType.EXPAND);
105    }
106
107    /**
108     * Returns the key.
109     *
110     * @return The key.
111     */
112    public Comparable getKey() {
113        return this.key;
114    }
115
116    /**
117     * Sets the key and sends a {@link MarkerChangeEvent} to all registered
118     * listeners.
119     *
120     * @param key  the key ({@code null} not permitted).
121     */
122    public void setKey(Comparable key) {
123        Args.nullNotPermitted(key, "key");
124        this.key = key;
125        notifyListeners(new MarkerChangeEvent(this));
126    }
127
128    /**
129     * Returns the flag that controls whether the marker is drawn as a region
130     * or a line.
131     *
132     * @return A line.
133     */
134    public boolean getDrawAsLine() {
135        return this.drawAsLine;
136    }
137
138    /**
139     * Sets the flag that controls whether the marker is drawn as a region or
140     * as a line, and sends a {@link MarkerChangeEvent} to all registered
141     * listeners.
142     *
143     * @param drawAsLine  the flag.
144     */
145    public void setDrawAsLine(boolean drawAsLine) {
146        this.drawAsLine = drawAsLine;
147        notifyListeners(new MarkerChangeEvent(this));
148    }
149
150    /**
151     * Tests the marker for equality with an arbitrary object.
152     *
153     * @param obj  the object ({@code null} permitted).
154     *
155     * @return A boolean.
156     */
157    @Override
158    public boolean equals(Object obj) {
159        if (obj == null) {
160            return false;
161        }
162        if (!(obj instanceof CategoryMarker)) {
163            return false;
164        }
165        CategoryMarker that = (CategoryMarker) obj;
166        if (!that.canEqual(this)) {
167            return false;
168        }
169        if (!Objects.equals(this.key, that.key)) {
170            return false;
171        }
172        if (this.drawAsLine != that.drawAsLine) {
173            return false;
174        }
175        return super.equals(obj);
176    }
177
178    /**
179     * Ensures symmetry between super/subclass implementations of equals. For
180     * more detail, see http://jqno.nl/equalsverifier/manual/inheritance.
181     *
182     * @param other Object
183     * 
184     * @return true ONLY if the parameter is THIS class type
185     */
186    @Override
187    public boolean canEqual(Object other) {
188        // Solves Problem: equals not symmetric
189        return (other instanceof CategoryMarker);
190    }
191
192    @Override
193    public int hashCode() {
194        int hash = super.hashCode(); // equals calls superclass, hashCode must also
195        hash = 89 * hash + Objects.hashCode(this.key);
196        hash = 89 * hash + (this.drawAsLine ? 1 : 0);
197        return hash;
198    }
199}