Spec-Zone .ru
спецификации, руководства, описания, API
001/*
002 * Copyright (c) 2008, 2013, Oracle and/or its affiliates. All rights reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation.  Oracle designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Oracle in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
022 * or visit www.oracle.com if you need additional information or have any
023 * questions.
024 */
025
026 package javafx.geometry;
027
028/**
029 * A set of inside offsets for the 4 side of a rectangular area
030 */
031public class Insets {
032    /**
033     * Empty insets. An {@code Insets} instance with all offsets equal to zero.
034     */
035    public static final Insets EMPTY = new Insets(0, 0, 0, 0);
036
037    /**
038     * The inset on the top side
039     */
040    public final double getTop() { return top; }
041    private double top;
042
043    /**
044     * The inset on the right side
045     */
046    public final double getRight() { return right; }
047    private double right;
048
049    /**
050     * The inset on the bottom side
051     */
052    public final double getBottom() { return bottom; }
053    private double bottom;
054
055    /**
056     * The inset on the left side
057     */
058    public final double getLeft() { return left; }
059    private double left;
060
061    /**
062     * The cached hash code, used to improve performance in situations where
063     * we cache gradients, such as in the CSS routines.
064     */
065    private int hash = 0;
066
067    /**
068     * Constructs a new Insets instance with four different offsets.
069     *
070     * @param top the top offset
071     * @param right the right offset
072     * @param bottom the bottom offset
073     * @param left the left offset
074     */
075    public Insets(double top, double right, double bottom, double left) {
076        this.top = top;
077        this.right = right;
078        this.bottom = bottom;
079        this.left = left;
080    }
081
082    /**
083     * Constructs a new Insets instance with same value for all four offsets.
084     * 
085     * @param topRightBottomLeft the value used for top, bottom, right and left 
086     * offset
087     */
088    public Insets(double topRightBottomLeft) {
089        this.top = topRightBottomLeft;
090        this.right = topRightBottomLeft;
091        this.bottom = topRightBottomLeft;
092        this.left = topRightBottomLeft;
093    }
094
095    /**
096     * Indicates whether some other object is "equal to" this one.
097     *
098     * @param obj the reference object with which to compare
099     * @return true if this object is the same as the obj argument; false otherwise
100     */
101    @Override public boolean equals(Object obj) {
102        if (obj == this) return true;
103        if (obj instanceof Insets) {
104            Insets other = (Insets) obj;
105            return top == other.top
106              && right == other.right
107              && bottom == other.bottom
108              && left == other.left;
109        } else return false;
110
111    }
112
113    /**
114     * Returns a hash code value for the insets.
115     * @return a hash code value for the insets.
116     */
117    @Override public int hashCode() {
118        if (hash == 0) {
119            long bits = 17L;
120            bits = 37L * bits + Double.doubleToLongBits(top);
121            bits = 37L * bits + Double.doubleToLongBits(right);
122            bits = 37L * bits + Double.doubleToLongBits(bottom);
123            bits = 37L * bits + Double.doubleToLongBits(left);
124            hash = (int) (bits ^ (bits >> 32));
125        }
126        return hash;
127    }
128
129    /**
130     * Returns a string representation for the insets.
131     * @return a string representation for the insets.
132     */
133    @Override public String toString() {
134        return "Insets [top=" + top + ", right=" + right + ", bottom="
135                + bottom + ", left=" + left + "]";
136    }
137}