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