Path: blob/master/test/langtools/tools/lib/toolbox/Assert.java
41149 views
/*1* Copyright (c) 2011, 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223package toolbox;2425import java.util.function.Supplier;2627/**28* Simple facility for unconditional assertions.29* The methods in this class are described in terms of equivalent assert30* statements, assuming that assertions have been enabled.31*/32public class Assert {33/** Equivalent to34* assert cond;35*/36public static void check(boolean cond) {37if (!cond)38error();39}4041/** Equivalent to42* assert (o == null);43*/44public static void checkNull(Object o) {45if (o != null)46error();47}4849/** Equivalent to50* assert (t != null); return t;51*/52public static <T> T checkNonNull(T t) {53if (t == null)54error();55return t;56}5758/** Equivalent to59* assert cond : value;60*/61public static void check(boolean cond, int value) {62if (!cond)63error(String.valueOf(value));64}6566/** Equivalent to67* assert cond : value;68*/69public static void check(boolean cond, long value) {70if (!cond)71error(String.valueOf(value));72}7374/** Equivalent to75* assert cond : value;76*/77public static void check(boolean cond, Object value) {78if (!cond)79error(String.valueOf(value));80}8182/** Equivalent to83* assert cond : msg;84*/85public static void check(boolean cond, String msg) {86if (!cond)87error(msg);88}8990/** Equivalent to91* assert cond : msg.get();92* Note: message string is computed lazily.93*/94public static void check(boolean cond, Supplier<String> msg) {95if (!cond)96error(msg.get());97}9899/** Equivalent to100* assert (o == null) : value;101*/102public static void checkNull(Object o, Object value) {103if (o != null)104error(String.valueOf(value));105}106107/** Equivalent to108* assert (o == null) : msg;109*/110public static void checkNull(Object o, String msg) {111if (o != null)112error(msg);113}114115/** Equivalent to116* assert (o == null) : msg.get();117* Note: message string is computed lazily.118*/119public static void checkNull(Object o, Supplier<String> msg) {120if (o != null)121error(msg.get());122}123124/** Equivalent to125* assert (o != null) : msg;126*/127public static <T> T checkNonNull(T t, String msg) {128if (t == null)129error(msg);130return t;131}132133/** Equivalent to134* assert (o != null) : msg.get();135* Note: message string is computed lazily.136*/137public static <T> T checkNonNull(T t, Supplier<String> msg) {138if (t == null)139error(msg.get());140return t;141}142143/** Equivalent to144* assert false;145*/146public static void error() {147throw new AssertionError();148}149150/** Equivalent to151* assert false : msg;152*/153public static void error(String msg) {154throw new AssertionError(msg);155}156157/** Prevent instantiation. */158private Assert() { }159}160161162