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