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