Assert.java revision 3573:c4a18ee691c4
1/*
2 * Copyright (c) 2011, 2016, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26package toolbox;
27
28import java.util.function.Supplier;
29
30/**
31 * Simple facility for unconditional assertions.
32 * The methods in this class are described in terms of equivalent assert
33 * statements, assuming that assertions have been enabled.
34 */
35public class Assert {
36    /** Equivalent to
37     *   assert cond;
38     */
39    public static void check(boolean cond) {
40        if (!cond)
41            error();
42    }
43
44    /** Equivalent to
45     *   assert (o == null);
46     */
47    public static void checkNull(Object o) {
48        if (o != null)
49            error();
50    }
51
52    /** Equivalent to
53     *   assert (t != null); return t;
54     */
55    public static <T> T checkNonNull(T t) {
56        if (t == null)
57            error();
58        return t;
59    }
60
61    /** Equivalent to
62     *   assert cond : value;
63     */
64    public static void check(boolean cond, int value) {
65        if (!cond)
66            error(String.valueOf(value));
67    }
68
69    /** Equivalent to
70     *   assert cond : value;
71     */
72    public static void check(boolean cond, long value) {
73        if (!cond)
74            error(String.valueOf(value));
75    }
76
77    /** Equivalent to
78     *   assert cond : value;
79     */
80    public static void check(boolean cond, Object value) {
81        if (!cond)
82            error(String.valueOf(value));
83    }
84
85    /** Equivalent to
86     *   assert cond : msg;
87     */
88    public static void check(boolean cond, String msg) {
89        if (!cond)
90            error(msg);
91    }
92
93    /** Equivalent to
94     *   assert cond : msg.get();
95     *  Note: message string is computed lazily.
96     */
97    public static void check(boolean cond, Supplier<String> msg) {
98        if (!cond)
99            error(msg.get());
100    }
101
102    /** Equivalent to
103     *   assert (o == null) : value;
104     */
105    public static void checkNull(Object o, Object value) {
106        if (o != null)
107            error(String.valueOf(value));
108    }
109
110    /** Equivalent to
111     *   assert (o == null) : msg;
112     */
113    public static void checkNull(Object o, String msg) {
114        if (o != null)
115            error(msg);
116    }
117
118    /** Equivalent to
119     *   assert (o == null) : msg.get();
120     *  Note: message string is computed lazily.
121     */
122    public static void checkNull(Object o, Supplier<String> msg) {
123        if (o != null)
124            error(msg.get());
125    }
126
127    /** Equivalent to
128     *   assert (o != null) : msg;
129     */
130    public static <T> T checkNonNull(T t, String msg) {
131        if (t == null)
132            error(msg);
133        return t;
134    }
135
136    /** Equivalent to
137     *   assert (o != null) : msg.get();
138     *  Note: message string is computed lazily.
139     */
140    public static <T> T checkNonNull(T t, Supplier<String> msg) {
141        if (t == null)
142            error(msg.get());
143        return t;
144    }
145
146    /** Equivalent to
147     *   assert false;
148     */
149    public static void error() {
150        throw new AssertionError();
151    }
152
153    /** Equivalent to
154     *   assert false : msg;
155     */
156    public static void error(String msg) {
157        throw new AssertionError(msg);
158    }
159
160    /** Prevent instantiation. */
161    private Assert() { }
162}
163