OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.chromoting; | 5 package org.chromium.chromoting; |
6 | 6 |
7 /** | 7 /** |
8 * A set of helper functions to write assertions. These functions are always bei
ng executed, and | 8 * A set of helper functions to write assertions. These functions are always bei
ng executed, and |
9 * will not ignored in release build. | 9 * will not ignored in release build. |
10 */ | 10 */ |
11 | 11 |
12 public final class Preconditions { | 12 public final class Preconditions { |
13 // This class contains only static functions, so it should not be instantiat
ed. | 13 // This class contains only static functions, so it should not be instantiat
ed. |
14 private Preconditions() {} | 14 private Preconditions() {} |
15 | 15 |
16 /** | 16 /** |
17 * Checks whether input |value| is true, and returns its value. Throws | 17 * Checks whether input |value| is true, and returns its value. Throws |
18 * {@link IllegalStateException} if |value| is false. | 18 * {@link IllegalStateException} if |value| is false. |
19 */ | 19 */ |
20 public static final boolean isTrue(boolean value) { | 20 public static final boolean isTrue(boolean value) { |
21 if (!value) { | 21 if (!value) { |
22 throw new IllegalStateException(); | 22 throw new IllegalStateException(); |
23 } | 23 } |
24 return value; | 24 return value; |
25 } | 25 } |
26 | 26 |
27 /** | 27 /** |
28 * Checks whether input |ref| is not a null reference, and return its value.
Throws | 28 * Checks whether input |ref| is not a null reference, and returns its value
. Throws |
29 * {@link NullPointerException} if |ref| is null. | 29 * {@link NullPointerException} if |ref| is null. |
30 */ | 30 */ |
31 public static final <T> T notNull(T ref) { | 31 public static final <T> T notNull(T ref) { |
32 if (ref == null) { | 32 if (ref == null) { |
33 throw new NullPointerException(); | 33 throw new NullPointerException(); |
34 } | 34 } |
35 return ref; | 35 return ref; |
36 } | 36 } |
| 37 |
| 38 /** |
| 39 * Checks whether input |ref| is a null reference, and returns its value. Th
rows |
| 40 * {@link IllegalArgumentException} if |ref| is not null. |
| 41 */ |
| 42 public static final <T> T isNull(T ref) { |
| 43 if (ref != null) { |
| 44 throw new IllegalArgumentException(); |
| 45 } |
| 46 return ref; |
| 47 } |
37 } | 48 } |
OLD | NEW |