1 /*
2  * Copyright (C) 2016 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 com.android.dialer.common;
18 
19 import android.os.Looper;
20 import android.support.annotation.NonNull;
21 import android.support.annotation.Nullable;
22 import javax.annotation.CheckReturnValue;
23 
24 /** Assertions which will result in program termination unless disabled by flags. */
25 public class Assert {
26 
27   private static boolean areThreadAssertsEnabled = true;
28 
setAreThreadAssertsEnabled(boolean areThreadAssertsEnabled)29   public static void setAreThreadAssertsEnabled(boolean areThreadAssertsEnabled) {
30     Assert.areThreadAssertsEnabled = areThreadAssertsEnabled;
31   }
32 
33   /**
34    * Called when a truly exceptional case occurs.
35    *
36    * @throws AssertionError
37    * @deprecated Use throw Assert.create*FailException() instead.
38    */
39   @Deprecated
fail()40   public static void fail() {
41     throw new AssertionError("Fail");
42   }
43 
44   /**
45    * Called when a truly exceptional case occurs.
46    *
47    * @param reason the optional reason to supply as the exception message
48    * @throws AssertionError
49    * @deprecated Use throw Assert.create*FailException() instead.
50    */
51   @Deprecated
fail(String reason)52   public static void fail(String reason) {
53     throw new AssertionError(reason);
54   }
55 
56   @CheckReturnValue
createAssertionFailException(String msg)57   public static AssertionError createAssertionFailException(String msg) {
58     return new AssertionError(msg);
59   }
60 
61   @CheckReturnValue
createAssertionFailException(String msg, Throwable reason)62   public static AssertionError createAssertionFailException(String msg, Throwable reason) {
63     return new AssertionError(msg, reason);
64   }
65 
66   @CheckReturnValue
createUnsupportedOperationFailException()67   public static UnsupportedOperationException createUnsupportedOperationFailException() {
68     return new UnsupportedOperationException();
69   }
70 
71   @CheckReturnValue
createUnsupportedOperationFailException(String msg)72   public static UnsupportedOperationException createUnsupportedOperationFailException(String msg) {
73     return new UnsupportedOperationException(msg);
74   }
75 
76   @CheckReturnValue
createIllegalStateFailException()77   public static IllegalStateException createIllegalStateFailException() {
78     return new IllegalStateException();
79   }
80 
81   @CheckReturnValue
createIllegalStateFailException(String msg)82   public static IllegalStateException createIllegalStateFailException(String msg) {
83     return new IllegalStateException(msg);
84   }
85 
86   /**
87    * Ensures the truth of an expression involving one or more parameters to the calling method.
88    *
89    * @param expression a boolean expression
90    * @throws IllegalArgumentException if {@code expression} is false
91    */
checkArgument(boolean expression)92   public static void checkArgument(boolean expression) {
93     checkArgument(expression, null);
94   }
95 
96   /**
97    * Ensures the truth of an expression involving one or more parameters to the calling method.
98    *
99    * @param expression a boolean expression
100    * @param messageTemplate the message to log, possible with format arguments.
101    * @param args optional arguments to be used in the formatted string.
102    * @throws IllegalArgumentException if {@code expression} is false
103    */
checkArgument( boolean expression, @Nullable String messageTemplate, Object... args)104   public static void checkArgument(
105       boolean expression, @Nullable String messageTemplate, Object... args) {
106     if (!expression) {
107       throw new IllegalArgumentException(format(messageTemplate, args));
108     }
109   }
110 
111   /**
112    * Ensures the truth of an expression involving the state of the calling instance, but not
113    * involving any parameters to the calling method.
114    *
115    * @param expression a boolean expression
116    * @throws IllegalStateException if {@code expression} is false
117    */
checkState(boolean expression)118   public static void checkState(boolean expression) {
119     checkState(expression, null);
120   }
121 
122   /**
123    * Ensures the truth of an expression involving the state of the calling instance, but not
124    * involving any parameters to the calling method.
125    *
126    * @param expression a boolean expression
127    * @param messageTemplate the message to log, possible with format arguments.
128    * @param args optional arguments to be used in the formatted string.
129    * @throws IllegalStateException if {@code expression} is false
130    */
checkState( boolean expression, @Nullable String messageTemplate, Object... args)131   public static void checkState(
132       boolean expression, @Nullable String messageTemplate, Object... args) {
133     if (!expression) {
134       throw new IllegalStateException(format(messageTemplate, args));
135     }
136   }
137 
138   /**
139    * Ensures that an object reference passed as a parameter to the calling method is not null.
140    *
141    * @param reference an object reference
142    * @return the non-null reference that was validated
143    * @throws NullPointerException if {@code reference} is null
144    */
145   @NonNull
isNotNull(@ullable T reference)146   public static <T> T isNotNull(@Nullable T reference) {
147     return isNotNull(reference, null);
148   }
149 
150   /**
151    * Ensures that an object reference passed as a parameter to the calling method is not null.
152    *
153    * @param reference an object reference
154    * @param messageTemplate the message to log, possible with format arguments.
155    * @param args optional arguments to be used in the formatted string.
156    * @return the non-null reference that was validated
157    * @throws NullPointerException if {@code reference} is null
158    */
159   @NonNull
isNotNull( @ullable T reference, @Nullable String messageTemplate, Object... args)160   public static <T> T isNotNull(
161       @Nullable T reference, @Nullable String messageTemplate, Object... args) {
162     if (reference == null) {
163       throw new NullPointerException(format(messageTemplate, args));
164     }
165     return reference;
166   }
167 
168   /**
169    * Ensures that the current thread is the main thread.
170    *
171    * @throws IllegalStateException if called on a background thread
172    */
isMainThread()173   public static void isMainThread() {
174     isMainThread(null);
175   }
176 
177   /**
178    * Ensures that the current thread is the main thread.
179    *
180    * @param messageTemplate the message to log, possible with format arguments.
181    * @param args optional arguments to be used in the formatted string.
182    * @throws IllegalStateException if called on a background thread
183    */
isMainThread(@ullable String messageTemplate, Object... args)184   public static void isMainThread(@Nullable String messageTemplate, Object... args) {
185     if (!areThreadAssertsEnabled) {
186       return;
187     }
188     checkState(Looper.getMainLooper().equals(Looper.myLooper()), messageTemplate, args);
189   }
190 
191   /**
192    * Ensures that the current thread is a worker thread.
193    *
194    * @throws IllegalStateException if called on the main thread
195    */
isWorkerThread()196   public static void isWorkerThread() {
197     isWorkerThread(null);
198   }
199 
200   /**
201    * Ensures that the current thread is a worker thread.
202    *
203    * @param messageTemplate the message to log, possible with format arguments.
204    * @param args optional arguments to be used in the formatted string.
205    * @throws IllegalStateException if called on the main thread
206    */
isWorkerThread(@ullable String messageTemplate, Object... args)207   public static void isWorkerThread(@Nullable String messageTemplate, Object... args) {
208     if (!areThreadAssertsEnabled) {
209       return;
210     }
211     checkState(!Looper.getMainLooper().equals(Looper.myLooper()), messageTemplate, args);
212   }
213 
format(@ullable String messageTemplate, Object... args)214   private static String format(@Nullable String messageTemplate, Object... args) {
215     if (messageTemplate == null) {
216       return null;
217     }
218     return String.format(messageTemplate, args);
219   }
220 }
221