Flutter Engine
The Flutter Engine
Preconditions.java
Go to the documentation of this file.
1// Copyright 2013 The Flutter Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5package io.flutter.util;
6
7import androidx.annotation.Nullable;
8
9/**
10 * Static convenience methods that help a method or constructor check whether it was invoked
11 * correctly (that is, whether its <i>preconditions</i> were met).
12 */
13public final class Preconditions {
14 private Preconditions() {}
15
16 /**
17 * Ensures that an object reference passed as a parameter to the calling method is not null.
18 *
19 * @param reference an object reference
20 * @return the non-null reference that was validated
21 * @throws NullPointerException if {@code reference} is null
22 */
23 public static <T> T checkNotNull(T reference) {
24 if (reference == null) {
25 throw new NullPointerException();
26 }
27 return reference;
28 }
29
30 /**
31 * Ensures the truth of an expression involving the state of the calling instance.
32 *
33 * @param expression a boolean expression that must be checked to be true
34 * @throws IllegalStateException if {@code expression} is false
35 */
36 public static void checkState(boolean expression) {
37 if (!expression) {
38 throw new IllegalStateException();
39 }
40 }
41
42 /**
43 * Ensures the truth of an expression involving the state of the calling instance.
44 *
45 * @param expression a boolean expression that must be checked to be true
46 * @param errorMessage the exception message to use if the check fails; will be converted to a
47 * string using {@link String#valueOf(Object)}
48 * @throws IllegalStateException if {@code expression} is false
49 */
50 public static void checkState(boolean expression, @Nullable Object errorMessage) {
51 if (!expression) {
52 throw new IllegalStateException(String.valueOf(errorMessage));
53 }
54 }
55}
static void checkState(boolean expression)
static void checkState(boolean expression, @Nullable Object errorMessage)
static< T > T checkNotNull(T reference)
#define T
Definition: precompiler.cc:65