• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 package android.graphics;
18 
19 import android.annotation.NonNull;
20 import android.annotation.Nullable;
21 import android.os.Parcel;
22 import android.os.Parcelable;
23 
24 /**
25  * An Insets instance holds four integer offsets which describe changes to the four
26  * edges of a Rectangle. By convention, positive values move edges towards the
27  * centre of the rectangle.
28  * <p>
29  * Insets are immutable so may be treated as values.
30  *
31  */
32 public final class Insets implements Parcelable {
33     public static final @NonNull Insets NONE = new Insets(0, 0, 0, 0);
34 
35     public final int left;
36     public final int top;
37     public final int right;
38     public final int bottom;
39 
Insets(int left, int top, int right, int bottom)40     private Insets(int left, int top, int right, int bottom) {
41         this.left = left;
42         this.top = top;
43         this.right = right;
44         this.bottom = bottom;
45     }
46 
47     // Factory methods
48 
49     /**
50      * Return an Insets instance with the appropriate values.
51      *
52      * @param left the left inset
53      * @param top the top inset
54      * @param right the right inset
55      * @param bottom the bottom inset
56      *
57      * @return Insets instance with the appropriate values
58      */
of(int left, int top, int right, int bottom)59     public static @NonNull Insets of(int left, int top, int right, int bottom) {
60         if (left == 0 && top == 0 && right == 0 && bottom == 0) {
61             return NONE;
62         }
63         return new Insets(left, top, right, bottom);
64     }
65 
66     /**
67      * Return an Insets instance with the appropriate values.
68      *
69      * @param r the rectangle from which to take the values
70      *
71      * @return an Insets instance with the appropriate values
72      */
of(@ullable Rect r)73     public static @NonNull Insets of(@Nullable Rect r) {
74         return (r == null) ? NONE : of(r.left, r.top, r.right, r.bottom);
75     }
76 
77     /**
78      * Returns a Rect instance with the appropriate values.
79      *
80      * @hide
81      */
toRect()82     public @NonNull Rect toRect() {
83         return new Rect(left, top, right, bottom);
84     }
85 
86     /**
87      * Add two Insets.
88      *
89      * @param a The first Insets to add.
90      * @param b The second Insets to add.
91      * @return a + b, i. e. all insets on every side are added together.
92      */
add(@onNull Insets a, @NonNull Insets b)93     public static @NonNull Insets add(@NonNull Insets a, @NonNull Insets b) {
94         return Insets.of(a.left + b.left, a.top + b.top, a.right + b.right, a.bottom + b.bottom);
95     }
96 
97     /**
98      * Subtract two Insets.
99      *
100      * @param a The minuend.
101      * @param b The subtrahend.
102      * @return a - b, i. e. all insets on every side are subtracted from each other.
103      */
subtract(@onNull Insets a, @NonNull Insets b)104     public static @NonNull Insets subtract(@NonNull Insets a, @NonNull Insets b) {
105         return Insets.of(a.left - b.left, a.top - b.top, a.right - b.right, a.bottom - b.bottom);
106     }
107 
108     /**
109      * Retrieves the maximum of two Insets.
110      *
111      * @param a The first Insets.
112      * @param b The second Insets.
113      * @return max(a, b), i. e. the larger of every inset on every side is taken for the result.
114      */
max(@onNull Insets a, @NonNull Insets b)115     public static @NonNull Insets max(@NonNull Insets a, @NonNull Insets b) {
116         return Insets.of(Math.max(a.left, b.left), Math.max(a.top, b.top),
117                 Math.max(a.right, b.right), Math.max(a.bottom, b.bottom));
118     }
119 
120     /**
121      * Retrieves the minimum of two Insets.
122      *
123      * @param a The first Insets.
124      * @param b The second Insets.
125      * @return min(a, b), i. e. the smaller of every inset on every side is taken for the result.
126      */
min(@onNull Insets a, @NonNull Insets b)127     public static @NonNull Insets min(@NonNull Insets a, @NonNull Insets b) {
128         return Insets.of(Math.min(a.left, b.left), Math.min(a.top, b.top),
129                 Math.min(a.right, b.right), Math.min(a.bottom, b.bottom));
130     }
131 
132     /**
133      * Two Insets instances are equal iff they belong to the same class and their fields are
134      * pairwise equal.
135      *
136      * @param o the object to compare this instance with.
137      *
138      * @return true iff this object is equal {@code o}
139      */
140     @Override
equals(Object o)141     public boolean equals(Object o) {
142         if (this == o) return true;
143         if (o == null || getClass() != o.getClass()) return false;
144 
145         Insets insets = (Insets) o;
146 
147         if (bottom != insets.bottom) return false;
148         if (left != insets.left) return false;
149         if (right != insets.right) return false;
150         if (top != insets.top) return false;
151 
152         return true;
153     }
154 
155     @Override
hashCode()156     public int hashCode() {
157         int result = left;
158         result = 31 * result + top;
159         result = 31 * result + right;
160         result = 31 * result + bottom;
161         return result;
162     }
163 
164     @Override
toString()165     public String toString() {
166         return "Insets{" +
167                 "left=" + left +
168                 ", top=" + top +
169                 ", right=" + right +
170                 ", bottom=" + bottom +
171                 '}';
172     }
173 
174     @Override
describeContents()175     public int describeContents() {
176         return 0;
177     }
178 
179     @Override
writeToParcel(Parcel out, int flags)180     public void writeToParcel(Parcel out, int flags) {
181         out.writeInt(left);
182         out.writeInt(top);
183         out.writeInt(right);
184         out.writeInt(bottom);
185     }
186 
187     public static final @android.annotation.NonNull Parcelable.Creator<Insets> CREATOR = new Parcelable.Creator<Insets>() {
188         @Override
189         public Insets createFromParcel(Parcel in) {
190             return new Insets(in.readInt(), in.readInt(), in.readInt(), in.readInt());
191         }
192 
193         @Override
194         public Insets[] newArray(int size) {
195             return new Insets[size];
196         }
197     };
198 }
199