1 /*
2  * Copyright 2021 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 androidx.window.core
18 
19 import android.graphics.Rect
20 
21 /**
22  * A wrapper for [Rect] to handle compatibility issues with API 15. In API 15, equals and hashCode
23  * operate on the reference as opposed to the attributes. This leads to test failures because the
24  * data matches but the equals check fails.
25  *
26  * Also useful in unit tests since you can instantiate [Bounds] in a JVM test but when you
27  * instantiate [Rect] you are using the class from the mockable jar file. The mockable jar does not
28  * contain any behavior or calculations.
29  */
30 internal class Bounds(
31     val left: Int,
32     val top: Int,
33     val right: Int,
34     val bottom: Int,
35 ) {
36     constructor(rect: Rect) : this(rect.left, rect.top, rect.right, rect.bottom)
37 
38     init {
<lambda>null39         require(left <= right) {
40             "Left must be less than or equal to right, left: $left, right: $right"
41         }
<lambda>null42         require(top <= bottom) {
43             "top must be less than or equal to bottom, top: $top, bottom: $bottom"
44         }
45     }
46 
47     /** Return the [Rect] representation of the bounds */
toRectnull48     fun toRect(): Rect = Rect(left, top, right, bottom)
49 
50     /** The width of the bounds, may be negative. */
51     val width: Int
52         get() = right - left
53 
54     /** The height of the bounds, may be negative. */
55     val height: Int
56         get() = bottom - top
57 
58     /** Determines if the bounds has empty area. */
59     val isEmpty: Boolean
60         get() = height == 0 || width == 0
61 
62     /** Returns if the dimensions of the bounds is 0. */
63     val isZero: Boolean
64         get() = height == 0 && width == 0
65 
66     override fun toString(): String {
67         return "${Bounds::class.java.simpleName} { [$left,$top,$right,$bottom] }"
68     }
69 
equalsnull70     override fun equals(other: Any?): Boolean {
71         if (this === other) return true
72         if (javaClass != other?.javaClass) return false
73 
74         other as Bounds
75 
76         if (left != other.left) return false
77         if (top != other.top) return false
78         if (right != other.right) return false
79         if (bottom != other.bottom) return false
80 
81         return true
82     }
83 
hashCodenull84     override fun hashCode(): Int {
85         var result = left
86         result = 31 * result + top
87         result = 31 * result + right
88         result = 31 * result + bottom
89         return result
90     }
91 
92     companion object {
93         val EMPTY_BOUNDS = Bounds(0, 0, 0, 0)
94     }
95 }
96