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 * MeterInterval.java
029 * ------------------
030 * (C) Copyright 2005-present, by David Gilbert and Contributors.
031 *
032 * Original Author:  David Gilbert;
033 * Contributor(s):   -;
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.IOException;
044import java.io.ObjectInputStream;
045import java.io.ObjectOutputStream;
046import java.io.Serializable;
047import java.util.Objects;
048import org.jfree.chart.util.PaintUtils;
049import org.jfree.chart.util.Args;
050import org.jfree.chart.util.SerialUtils;
051
052import org.jfree.data.Range;
053
054/**
055 * An interval to be highlighted on a {@link MeterPlot}.  Instances of this
056 * class are immutable.
057 */
058public class MeterInterval implements Serializable {
059
060    /** For serialization. */
061    private static final long serialVersionUID = 1530982090622488257L;
062
063    /** The interval label. */
064    private String label;
065
066    /** The interval range. */
067    private Range range;
068
069    /** The outline paint (used for the arc marking the interval). */
070    private transient Paint outlinePaint;
071
072    /** The outline stroke (used for the arc marking the interval). */
073    private transient Stroke outlineStroke;
074
075    /** The background paint for the interval. */
076    private transient Paint backgroundPaint;
077
078    /**
079     * Creates a new interval.
080     *
081     * @param label  the label ({@code null} not permitted).
082     * @param range  the range ({@code null} not permitted).
083     */
084    public MeterInterval(String label, Range range) {
085        this(label, range, Color.YELLOW, new BasicStroke(2.0f), null);
086    }
087
088    /**
089     * Creates a new interval.
090     *
091     * @param label  the label ({@code null} not permitted).
092     * @param range  the range ({@code null} not permitted).
093     * @param outlinePaint  the outline paint ({@code null} permitted).
094     * @param outlineStroke  the outline stroke ({@code null} permitted).
095     * @param backgroundPaint  the background paint ({@code null}
096     *                         permitted).
097     */
098    public MeterInterval(String label, Range range, Paint outlinePaint,
099                         Stroke outlineStroke, Paint backgroundPaint) {
100        Args.nullNotPermitted(label, "label");
101        Args.nullNotPermitted(range, "range");
102        this.label = label;
103        this.range = range;
104        this.outlinePaint = outlinePaint;
105        this.outlineStroke = outlineStroke;
106        this.backgroundPaint = backgroundPaint;
107    }
108
109    /**
110     * Returns the label.
111     *
112     * @return The label (never {@code null}).
113     */
114    public String getLabel() {
115        return this.label;
116    }
117
118    /**
119     * Returns the range.
120     *
121     * @return The range (never {@code null}).
122     */
123    public Range getRange() {
124        return this.range;
125    }
126
127    /**
128     * Returns the background paint.  If {@code null}, the background
129     * should remain unfilled.
130     *
131     * @return The background paint (possibly {@code null}).
132     */
133    public Paint getBackgroundPaint() {
134        return this.backgroundPaint;
135    }
136
137    /**
138     * Returns the outline paint.
139     *
140     * @return The outline paint (possibly {@code null}).
141     */
142    public Paint getOutlinePaint() {
143        return this.outlinePaint;
144    }
145
146    /**
147     * Returns the outline stroke.
148     *
149     * @return The outline stroke (possibly {@code null}).
150     */
151    public Stroke getOutlineStroke() {
152        return this.outlineStroke;
153    }
154
155    /**
156     * Checks this instance for equality with an arbitrary object.
157     *
158     * @param obj  the object ({@code null} permitted).
159     *
160     * @return A boolean.
161     */
162    @Override
163    public boolean equals(Object obj) {
164        if (obj == this) {
165            return true;
166        }
167        if (!(obj instanceof MeterInterval)) {
168            return false;
169        }
170        MeterInterval that = (MeterInterval) obj;
171        if (!this.label.equals(that.label)) {
172            return false;
173        }
174        if (!this.range.equals(that.range)) {
175            return false;
176        }
177        if (!PaintUtils.equal(this.outlinePaint, that.outlinePaint)) {
178            return false;
179        }
180        if (!Objects.equals(this.outlineStroke, that.outlineStroke)) {
181            return false;
182        }
183        if (!PaintUtils.equal(this.backgroundPaint, that.backgroundPaint)) {
184            return false;
185        }
186        return true;
187    }
188
189    /**
190     * Provides serialization support.
191     *
192     * @param stream  the output stream.
193     *
194     * @throws IOException  if there is an I/O error.
195     */
196    private void writeObject(ObjectOutputStream stream) throws IOException {
197        stream.defaultWriteObject();
198        SerialUtils.writePaint(this.outlinePaint, stream);
199        SerialUtils.writeStroke(this.outlineStroke, stream);
200        SerialUtils.writePaint(this.backgroundPaint, stream);
201    }
202
203    /**
204     * Provides serialization support.
205     *
206     * @param stream  the input stream.
207     *
208     * @throws IOException  if there is an I/O error.
209     * @throws ClassNotFoundException  if there is a classpath problem.
210     */
211    private void readObject(ObjectInputStream stream)
212        throws IOException, ClassNotFoundException {
213        stream.defaultReadObject();
214        this.outlinePaint = SerialUtils.readPaint(stream);
215        this.outlineStroke = SerialUtils.readStroke(stream);
216        this.backgroundPaint = SerialUtils.readPaint(stream);
217    }
218
219}