001/* ===========================================================
002 * JFreeChart : a free chart library for the Java(tm) platform
003 * ===========================================================
004 *
005 * (C) Copyright 2000-2020, by Object Refinery Limited 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 * AxisSpace.java
029 * --------------
030 * (C) Copyright 2003-2016, by Object Refinery Limited and Contributors.
031 *
032 * Original Author:  David Gilbert (for Object Refinery Limited);
033 * Contributor(s):   -;
034 *
035 * Changes
036 * -------
037 * 03-Jul-2003 : Version 1 (DG);
038 * 14-Aug-2003 : Implemented Cloneable (DG);
039 * 18-Aug-2003 : Implemented Serializable (DG);
040 * 17-Mar-2004 : Added a toString() method for debugging (DG);
041 * 07-Jan-2005 : Updated equals() method (DG);
042 * 11-Jan-2005 : Removed deprecated methods in preparation for 1.0.0
043 *               release (DG);
044 * 02-Jul-2013 : Use ParamChecks (DG);
045 *
046 */
047
048package org.jfree.chart.axis;
049
050import java.awt.geom.Rectangle2D;
051import java.io.Serializable;
052import org.jfree.chart.ui.RectangleEdge;
053import org.jfree.chart.util.Args;
054import org.jfree.chart.util.PublicCloneable;
055
056/**
057 * A record that contains the space required at each edge of a plot.
058 */
059public class AxisSpace implements Cloneable, PublicCloneable, Serializable {
060
061    /** For serialization. */
062    private static final long serialVersionUID = -2490732595134766305L;
063
064    /** The top space. */
065    private double top;
066
067    /** The bottom space. */
068    private double bottom;
069
070    /** The left space. */
071    private double left;
072
073    /** The right space. */
074    private double right;
075
076    /**
077     * Creates a new axis space record.
078     */
079    public AxisSpace() {
080        this.top = 0.0;
081        this.bottom = 0.0;
082        this.left = 0.0;
083        this.right = 0.0;
084    }
085
086    /**
087     * Returns the space reserved for axes at the top of the plot area.
088     *
089     * @return The space (in Java2D units).
090     */
091    public double getTop() {
092        return this.top;
093    }
094
095    /**
096     * Sets the space reserved for axes at the top of the plot area.
097     *
098     * @param space  the space (in Java2D units).
099     */
100    public void setTop(double space) {
101        this.top = space;
102    }
103
104    /**
105     * Returns the space reserved for axes at the bottom of the plot area.
106     *
107     * @return The space (in Java2D units).
108     */
109    public double getBottom() {
110        return this.bottom;
111    }
112
113    /**
114     * Sets the space reserved for axes at the bottom of the plot area.
115     *
116     * @param space  the space (in Java2D units).
117     */
118    public void setBottom(double space) {
119        this.bottom = space;
120    }
121
122    /**
123     * Returns the space reserved for axes at the left of the plot area.
124     *
125     * @return The space (in Java2D units).
126     */
127    public double getLeft() {
128        return this.left;
129    }
130
131    /**
132     * Sets the space reserved for axes at the left of the plot area.
133     *
134     * @param space  the space (in Java2D units).
135     */
136    public void setLeft(double space) {
137        this.left = space;
138    }
139
140    /**
141     * Returns the space reserved for axes at the right of the plot area.
142     *
143     * @return The space (in Java2D units).
144     */
145    public double getRight() {
146        return this.right;
147    }
148
149    /**
150     * Sets the space reserved for axes at the right of the plot area.
151     *
152     * @param space  the space (in Java2D units).
153     */
154    public void setRight(double space) {
155        this.right = space;
156    }
157
158    /**
159     * Adds space to the top, bottom, left or right edge of the plot area.
160     *
161     * @param space  the space (in Java2D units).
162     * @param edge  the edge ({@code null} not permitted).
163     */
164    public void add(double space, RectangleEdge edge) {
165        Args.nullNotPermitted(edge, "edge");
166        if (edge == RectangleEdge.TOP) {
167            this.top += space;
168        }
169        else if (edge == RectangleEdge.BOTTOM) {
170            this.bottom += space;
171        }
172        else if (edge == RectangleEdge.LEFT) {
173            this.left += space;
174        }
175        else if (edge == RectangleEdge.RIGHT) {
176            this.right += space;
177        }
178        else {
179            throw new IllegalStateException("Unrecognised 'edge' argument.");
180        }
181    }
182
183    /**
184     * Ensures that this object reserves at least as much space as another.
185     *
186     * @param space  the other space.
187     */
188    public void ensureAtLeast(AxisSpace space) {
189        this.top = Math.max(this.top, space.top);
190        this.bottom = Math.max(this.bottom, space.bottom);
191        this.left = Math.max(this.left, space.left);
192        this.right = Math.max(this.right, space.right);
193    }
194
195    /**
196     * Ensures there is a minimum amount of space at the edge corresponding to
197     * the specified axis location.
198     *
199     * @param space  the space.
200     * @param edge  the location.
201     */
202    public void ensureAtLeast(double space, RectangleEdge edge) {
203        if (edge == RectangleEdge.TOP) {
204            if (this.top < space) {
205                this.top = space;
206            }
207        }
208        else if (edge == RectangleEdge.BOTTOM) {
209            if (this.bottom < space) {
210                this.bottom = space;
211            }
212        }
213        else if (edge == RectangleEdge.LEFT) {
214            if (this.left < space) {
215                this.left = space;
216            }
217        }
218        else if (edge == RectangleEdge.RIGHT) {
219            if (this.right < space) {
220                this.right = space;
221            }
222        }
223        else {
224            throw new IllegalStateException(
225                "AxisSpace.ensureAtLeast(): unrecognised AxisLocation."
226            );
227        }
228    }
229
230    /**
231     * Shrinks an area by the space attributes.
232     *
233     * @param area  the area to shrink.
234     * @param result  an optional carrier for the result.
235     *
236     * @return The result.
237     */
238    public Rectangle2D shrink(Rectangle2D area, Rectangle2D result) {
239        if (result == null) {
240            result = new Rectangle2D.Double();
241        }
242        result.setRect(
243            area.getX() + this.left,
244            area.getY() + this.top,
245            area.getWidth() - this.left - this.right,
246            area.getHeight() - this.top - this.bottom
247        );
248        return result;
249    }
250
251    /**
252     * Expands an area by the amount of space represented by this object.
253     *
254     * @param area  the area to expand.
255     * @param result  an optional carrier for the result.
256     *
257     * @return The result.
258     */
259    public Rectangle2D expand(Rectangle2D area, Rectangle2D result) {
260        if (result == null) {
261            result = new Rectangle2D.Double();
262        }
263        result.setRect(
264            area.getX() - this.left,
265            area.getY() - this.top,
266            area.getWidth() + this.left + this.right,
267            area.getHeight() + this.top + this.bottom
268        );
269        return result;
270    }
271
272    /**
273     * Calculates the reserved area.
274     *
275     * @param area  the area.
276     * @param edge  the edge.
277     *
278     * @return The reserved area.
279     */
280    public Rectangle2D reserved(Rectangle2D area, RectangleEdge edge) {
281        Rectangle2D result = null;
282        if (edge == RectangleEdge.TOP) {
283            result = new Rectangle2D.Double(
284                area.getX(), area.getY(), area.getWidth(), this.top
285            );
286        }
287        else if (edge == RectangleEdge.BOTTOM) {
288            result = new Rectangle2D.Double(
289                area.getX(), area.getMaxY() - this.top,
290                area.getWidth(), this.bottom
291            );
292        }
293        else if (edge == RectangleEdge.LEFT) {
294            result = new Rectangle2D.Double(
295                area.getX(), area.getY(), this.left, area.getHeight()
296            );
297        }
298        else if (edge == RectangleEdge.RIGHT) {
299            result = new Rectangle2D.Double(
300                area.getMaxX() - this.right, area.getY(),
301                this.right, area.getHeight()
302            );
303        }
304        return result;
305    }
306
307    /**
308     * Returns a clone of the object.
309     *
310     * @return A clone.
311     *
312     * @throws CloneNotSupportedException This class won't throw this exception,
313     *         but subclasses (if any) might.
314     */
315    @Override
316    public Object clone() throws CloneNotSupportedException {
317        return super.clone();
318    }
319
320    /**
321     * Tests this object for equality with another object.
322     *
323     * @param obj  the object to compare against.
324     *
325     * @return {@code true} or {@code false}.
326     */
327    @Override
328    public boolean equals(Object obj) {
329        if (obj == this) {
330            return true;
331        }
332        if (!(obj instanceof AxisSpace)) {
333            return false;
334        }
335        AxisSpace that = (AxisSpace) obj;
336        if (this.top != that.top) {
337            return false;
338        }
339        if (this.bottom != that.bottom) {
340            return false;
341        }
342        if (this.left != that.left) {
343            return false;
344        }
345        if (this.right != that.right) {
346            return false;
347        }
348        return true;
349    }
350
351    /**
352     * Returns a hash code for this object.
353     *
354     * @return A hash code.
355     */
356    @Override
357    public int hashCode() {
358        int result = 23;
359        long l = Double.doubleToLongBits(this.top);
360        result = 37 * result + (int) (l ^ (l >>> 32));
361        l = Double.doubleToLongBits(this.bottom);
362        result = 37 * result + (int) (l ^ (l >>> 32));
363        l = Double.doubleToLongBits(this.left);
364        result = 37 * result + (int) (l ^ (l >>> 32));
365        l = Double.doubleToLongBits(this.right);
366        result = 37 * result + (int) (l ^ (l >>> 32));
367        return result;
368    }
369
370    /**
371     * Returns a string representing the object (for debugging purposes).
372     *
373     * @return A string.
374     */
375    @Override
376    public String toString() {
377        return super.toString() + "[left=" + this.left + ",right=" + this.right
378                    + ",top=" + this.top + ",bottom=" + this.bottom + "]";
379    }
380
381}