summaryrefslogtreecommitdiffstats
path: root/java/com/android/dialer/common/Assert.java
blob: 189d209c80628fd60cb634a626728c190dc23d61 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/*
 * Copyright (C) 2016 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License
 */

package com.android.dialer.common;

import android.os.Looper;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import javax.annotation.CheckReturnValue;

/** Assertions which will result in program termination unless disabled by flags. */
public class Assert {

  private static boolean areThreadAssertsEnabled = true;

  public static void setAreThreadAssertsEnabled(boolean areThreadAssertsEnabled) {
    Assert.areThreadAssertsEnabled = areThreadAssertsEnabled;
  }

  public static boolean areThreadAssertsEnabled() {
    return areThreadAssertsEnabled;
  }

  /**
   * Called when a truly exceptional case occurs.
   *
   * @throws AssertionError
   * @deprecated Use throw Assert.create*FailException() instead.
   */
  @Deprecated
  public static void fail() {
    throw new AssertionError("Fail");
  }

  /**
   * Called when a truly exceptional case occurs.
   *
   * @param reason the optional reason to supply as the exception message
   * @throws AssertionError
   * @deprecated Use throw Assert.create*FailException() instead.
   */
  @Deprecated
  public static void fail(String reason) {
    throw new AssertionError(reason);
  }

  @CheckReturnValue
  public static AssertionError createAssertionFailException(String msg) {
    return new AssertionError(msg);
  }

  @CheckReturnValue
  public static UnsupportedOperationException createUnsupportedOperationFailException() {
    return new UnsupportedOperationException();
  }

  @CheckReturnValue
  public static UnsupportedOperationException createUnsupportedOperationFailException(String msg) {
    return new UnsupportedOperationException(msg);
  }

  @CheckReturnValue
  public static IllegalStateException createIllegalStateFailException() {
    return new IllegalStateException();
  }

  @CheckReturnValue
  public static IllegalStateException createIllegalStateFailException(String msg) {
    return new IllegalStateException(msg);
  }

  /**
   * Ensures the truth of an expression involving one or more parameters to the calling method.
   *
   * @param expression a boolean expression
   * @throws IllegalArgumentException if {@code expression} is false
   */
  public static void checkArgument(boolean expression) {
    checkArgument(expression, null);
  }

  /**
   * Ensures the truth of an expression involving one or more parameters to the calling method.
   *
   * @param expression a boolean expression
   * @param messageTemplate the message to log, possible with format arguments.
   * @param args optional arguments to be used in the formatted string.
   * @throws IllegalArgumentException if {@code expression} is false
   */
  public static void checkArgument(
      boolean expression, @Nullable String messageTemplate, Object... args) {
    if (!expression) {
      throw new IllegalArgumentException(format(messageTemplate, args));
    }
  }

  /**
   * Ensures the truth of an expression involving the state of the calling instance, but not
   * involving any parameters to the calling method.
   *
   * @param expression a boolean expression
   * @throws IllegalStateException if {@code expression} is false
   */
  public static void checkState(boolean expression) {
    checkState(expression, null);
  }

  /**
   * Ensures the truth of an expression involving the state of the calling instance, but not
   * involving any parameters to the calling method.
   *
   * @param expression a boolean expression
   * @param messageTemplate the message to log, possible with format arguments.
   * @param args optional arguments to be used in the formatted string.
   * @throws IllegalStateException if {@code expression} is false
   */
  public static void checkState(
      boolean expression, @Nullable String messageTemplate, Object... args) {
    if (!expression) {
      throw new IllegalStateException(format(messageTemplate, args));
    }
  }

  /**
   * Ensures that an object reference passed as a parameter to the calling method is not null.
   *
   * @param reference an object reference
   * @return the non-null reference that was validated
   * @throws NullPointerException if {@code reference} is null
   */
  @NonNull
  public static <T> T isNotNull(@Nullable T reference) {
    return isNotNull(reference, null);
  }

  /**
   * Ensures that an object reference passed as a parameter to the calling method is not null.
   *
   * @param reference an object reference
   * @param messageTemplate the message to log, possible with format arguments.
   * @param args optional arguments to be used in the formatted string.
   * @return the non-null reference that was validated
   * @throws NullPointerException if {@code reference} is null
   */
  @NonNull
  public static <T> T isNotNull(
      @Nullable T reference, @Nullable String messageTemplate, Object... args) {
    if (reference == null) {
      throw new NullPointerException(format(messageTemplate, args));
    }
    return reference;
  }

  /**
   * Ensures that the current thread is the main thread.
   *
   * @throws IllegalStateException if called on a background thread
   */
  public static void isMainThread() {
    isMainThread(null);
  }

  /**
   * Ensures that the current thread is the main thread.
   *
   * @param messageTemplate the message to log, possible with format arguments.
   * @param args optional arguments to be used in the formatted string.
   * @throws IllegalStateException if called on a background thread
   */
  public static void isMainThread(@Nullable String messageTemplate, Object... args) {
    if (!areThreadAssertsEnabled) {
      return;
    }
    checkState(Looper.getMainLooper().equals(Looper.myLooper()), messageTemplate, args);
  }

  /**
   * Ensures that the current thread is a worker thread.
   *
   * @throws IllegalStateException if called on the main thread
   */
  public static void isWorkerThread() {
    isWorkerThread(null);
  }

  /**
   * Ensures that the current thread is a worker thread.
   *
   * @param messageTemplate the message to log, possible with format arguments.
   * @param args optional arguments to be used in the formatted string.
   * @throws IllegalStateException if called on the main thread
   */
  public static void isWorkerThread(@Nullable String messageTemplate, Object... args) {
    if (!areThreadAssertsEnabled) {
      return;
    }
    checkState(!Looper.getMainLooper().equals(Looper.myLooper()), messageTemplate, args);
  }

  private static String format(@Nullable String messageTemplate, Object... args) {
    if (messageTemplate == null) {
      return null;
    }
    return String.format(messageTemplate, args);
  }
}