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