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 * ItemLabelPosition.java
029 * ----------------------
030 * (C) Copyright 2003-2022, by David Gilbert and Contributors.
031 *
032 * Original Author:  David Gilbert;
033 * Contributor(s):   -;
034 *
035 */
036
037package org.jfree.chart.labels;
038
039import java.io.Serializable;
040import java.util.Objects;
041import org.jfree.chart.text.TextAnchor;
042import org.jfree.chart.internal.Args;
043
044/**
045 * The attributes that control the position of the label for each data item on
046 * a chart.  Instances of this class are immutable.
047 */
048public class ItemLabelPosition implements Serializable {
049
050    /** For serialization. */
051    private static final long serialVersionUID = 5845390630157034499L;
052
053    /** The item label anchor point. */
054    private final ItemLabelAnchor itemLabelAnchor;
055
056    /** The text anchor. */
057    private final TextAnchor textAnchor;
058
059    /** The rotation anchor. */
060    private final TextAnchor rotationAnchor;
061
062    /** The rotation angle. */
063    private final double angle;
064
065    /**
066     * Creates a new position record with default settings.
067     */
068    public ItemLabelPosition() {
069        this(ItemLabelAnchor.OUTSIDE12, TextAnchor.BOTTOM_CENTER,
070                TextAnchor.CENTER, 0.0);
071    }
072
073    /**
074     * Creates a new position record (with zero rotation).
075     *
076     * @param itemLabelAnchor  the item label anchor ({@code null} not
077     *                         permitted).
078     * @param textAnchor  the text anchor ({@code null} not permitted).
079     */
080    public ItemLabelPosition(ItemLabelAnchor itemLabelAnchor,
081                             TextAnchor textAnchor) {
082        this(itemLabelAnchor, textAnchor, TextAnchor.CENTER, 0.0);
083    }
084
085    /**
086     * Creates a new position record.  The item label anchor is a point
087     * relative to the data item (dot, bar or other visual item) on a chart.
088     * The item label is aligned by aligning the text anchor with the
089     * item label anchor.
090     *
091     * @param itemLabelAnchor  the item label anchor ({@code null} not
092     *                         permitted).
093     * @param textAnchor  the text anchor ({@code null} not permitted).
094     * @param rotationAnchor  the rotation anchor ({@code null} not
095     *                        permitted).
096     * @param angle  the rotation angle (in radians).
097     */
098    public ItemLabelPosition(ItemLabelAnchor itemLabelAnchor, 
099            TextAnchor textAnchor, TextAnchor rotationAnchor, double angle) {
100
101        Args.nullNotPermitted(itemLabelAnchor, "itemLabelAnchor");
102        Args.nullNotPermitted(textAnchor, "textAnchor");
103        Args.nullNotPermitted(rotationAnchor, "rotationAnchor");
104        this.itemLabelAnchor = itemLabelAnchor;
105        this.textAnchor = textAnchor;
106        this.rotationAnchor = rotationAnchor;
107        this.angle = angle;
108    }
109
110    /**
111     * Returns the item label anchor.
112     *
113     * @return The item label anchor (never {@code null}).
114     */
115    public ItemLabelAnchor getItemLabelAnchor() {
116        return this.itemLabelAnchor;
117    }
118
119    /**
120     * Returns the text anchor.
121     *
122     * @return The text anchor (never {@code null}).
123     */
124    public TextAnchor getTextAnchor() {
125        return this.textAnchor;
126    }
127
128    /**
129     * Returns the rotation anchor point.
130     *
131     * @return The rotation anchor point (never {@code null}).
132     */
133    public TextAnchor getRotationAnchor() {
134        return this.rotationAnchor;
135    }
136
137    /**
138     * Returns the angle of rotation for the label.
139     *
140     * @return The angle (in radians).
141     */
142    public double getAngle() {
143        return this.angle;
144    }
145
146    /**
147     * Tests this object for equality with an arbitrary object.
148     *
149     * @param obj  the object ({@code null} permitted).
150     *
151     * @return A boolean.
152     */
153    @Override
154    public boolean equals(Object obj) {
155        if (obj == this) {
156            return true;
157        }
158        if (!(obj instanceof ItemLabelPosition)) {
159            return false;
160        }
161        ItemLabelPosition that = (ItemLabelPosition) obj;
162        if (!this.itemLabelAnchor.equals(that.itemLabelAnchor)) {
163            return false;
164        }
165        if (!this.textAnchor.equals(that.textAnchor)) {
166            return false;
167        }
168        if (!this.rotationAnchor.equals(that.rotationAnchor)) {
169            return false;
170        }
171        if (this.angle != that.angle) {
172            return false;
173        }
174        return true;
175    }
176
177    @Override
178    public int hashCode(){
179        int hash = 5;
180        hash = 83 * hash + Objects.hashCode(this.itemLabelAnchor);
181        hash = 83 * hash + Objects.hashCode(this.textAnchor);
182        hash = 83 * hash + Objects.hashCode(this.rotationAnchor);
183        hash = 83 * hash +
184                (int) (Double.doubleToLongBits(this.angle) ^
185                (Double.doubleToLongBits(this.angle) >>> 32));
186        return hash;
187    }
188
189}