Insets.java revision bbd51f1e360b22eece1d74bd65c7e6a0b59dee59
1/*
2 * Copyright (C) 2006 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.graphics;
18
19/**
20 * An Insets instance holds four integer offsets which describe changes to the four
21 * edges of a Rectangle. By convention, positive values move edges towards the
22 * centre of the rectangle.
23 * <p>
24 * Insets are immutable so may be treated as values.
25 *
26 * @hide
27 */
28public class Insets {
29    public static final Insets NONE = new Insets(0, 0, 0, 0);
30
31    public final int left;
32    public final int top;
33    public final int right;
34    public final int bottom;
35
36    private Insets(int left, int top, int right, int bottom) {
37        this.left = left;
38        this.top = top;
39        this.right = right;
40        this.bottom = bottom;
41    }
42
43    // Factory methods
44
45    /**
46     * Return an Insets instance with the appropriate values.
47     *
48     * @param left the left inset
49     * @param top the top inset
50     * @param right the right inset
51     * @param bottom the bottom inset
52     *
53     * @return Insets instance with the appropriate values
54     */
55    public static Insets of(int left, int top, int right, int bottom) {
56        if (left == 0 && top == 0 && right == 0 && bottom == 0) {
57            return NONE;
58        }
59        return new Insets(left, top, right, bottom);
60    }
61
62    /**
63     * Return an Insets instance with the appropriate values.
64     *
65     * @param r the rectangle from which to take the values
66     *
67     * @return an Insets instance with the appropriate values
68     */
69    public static Insets of(Rect r) {
70        return (r == null) ? NONE : of(r.left, r.top, r.right, r.bottom);
71    }
72
73    /**
74     * Two Insets instances are equal iff they belong to the same class and their fields are
75     * pairwise equal.
76     *
77     * @param o the object to compare this instance with.
78     *
79     * @return true iff this object is equal {@code o}
80     */
81    @Override
82    public boolean equals(Object o) {
83        if (this == o) return true;
84        if (o == null || getClass() != o.getClass()) return false;
85
86        Insets insets = (Insets) o;
87
88        if (bottom != insets.bottom) return false;
89        if (left != insets.left) return false;
90        if (right != insets.right) return false;
91        if (top != insets.top) return false;
92
93        return true;
94    }
95
96    @Override
97    public int hashCode() {
98        int result = left;
99        result = 31 * result + top;
100        result = 31 * result + right;
101        result = 31 * result + bottom;
102        return result;
103    }
104
105    @Override
106    public String toString() {
107        return "Insets{" +
108                "left=" + left +
109                ", top=" + top +
110                ", right=" + right +
111                ", bottom=" + bottom +
112                '}';
113    }
114}
115