Assert.java revision 2840:84849fdb360b
1/* 2 * Copyright (c) 2011, 2014, 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 com.sun.tools.javac.util; 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 * 35 * <p><b>This is NOT part of any supported API. 36 * If you write code that depends on this, you do so at your own risk. 37 * This code and its internal interfaces are subject to change or 38 * deletion without notice.</b> 39 */ 40public class Assert { 41 /** Equivalent to 42 * assert cond; 43 */ 44 public static void check(boolean cond) { 45 if (!cond) 46 error(); 47 } 48 49 /** Equivalent to 50 * assert (o == null); 51 */ 52 public static void checkNull(Object o) { 53 if (o != null) 54 error(); 55 } 56 57 /** Equivalent to 58 * assert (t != null); return t; 59 */ 60 public static <T> T checkNonNull(T t) { 61 if (t == null) 62 error(); 63 return t; 64 } 65 66 /** Equivalent to 67 * assert cond : value; 68 */ 69 public static void check(boolean cond, int value) { 70 if (!cond) 71 error(String.valueOf(value)); 72 } 73 74 /** Equivalent to 75 * assert cond : value; 76 */ 77 public static void check(boolean cond, long value) { 78 if (!cond) 79 error(String.valueOf(value)); 80 } 81 82 /** Equivalent to 83 * assert cond : value; 84 */ 85 public static void check(boolean cond, Object value) { 86 if (!cond) 87 error(String.valueOf(value)); 88 } 89 90 /** Equivalent to 91 * assert cond : msg; 92 */ 93 public static void check(boolean cond, String msg) { 94 if (!cond) 95 error(msg); 96 } 97 98 /** Equivalent to 99 * assert cond : msg.get(); 100 * Note: message string is computed lazily. 101 */ 102 public static void check(boolean cond, Supplier<String> msg) { 103 if (!cond) 104 error(msg.get()); 105 } 106 107 /** Equivalent to 108 * assert (o == null) : value; 109 */ 110 public static void checkNull(Object o, Object value) { 111 if (o != null) 112 error(String.valueOf(value)); 113 } 114 115 /** Equivalent to 116 * assert (o == null) : msg; 117 */ 118 public static void checkNull(Object o, String msg) { 119 if (o != null) 120 error(msg); 121 } 122 123 /** Equivalent to 124 * assert (o == null) : msg.get(); 125 * Note: message string is computed lazily. 126 */ 127 public static void checkNull(Object o, Supplier<String> msg) { 128 if (o != null) 129 error(msg.get()); 130 } 131 132 /** Equivalent to 133 * assert (o != null) : msg; 134 */ 135 public static <T> T checkNonNull(T t, String msg) { 136 if (t == null) 137 error(msg); 138 return t; 139 } 140 141 /** Equivalent to 142 * assert (o != null) : msg.get(); 143 * Note: message string is computed lazily. 144 */ 145 public static <T> T checkNonNull(T t, Supplier<String> msg) { 146 if (t == null) 147 error(msg.get()); 148 return t; 149 } 150 151 /** Equivalent to 152 * assert false; 153 */ 154 public static void error() { 155 throw new AssertionError(); 156 } 157 158 /** Equivalent to 159 * assert false : msg; 160 */ 161 public static void error(String msg) { 162 throw new AssertionError(msg); 163 } 164 165 /** Prevent instantiation. */ 166 private Assert() { } 167} 168