001/* 002 * Copyright 2023 the original author or authors. 003 * <p> 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * <p> 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * <p> 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package de.cuioss.tools.base; 017 018import static de.cuioss.tools.string.MoreStrings.lenientFormat; 019 020import lombok.experimental.UtilityClass; 021 022/** 023 * Inspired by com.google.common.base.Preconditions. Defines a subset of the 024 * corresponding Preconditions 025 * 026 * @author com.google.common.base.Preconditions 027 * @author Oliver Wolff 028 * 029 */ 030@UtilityClass 031public class Preconditions { 032 033 /** 034 * Ensures the truth of an expression involving one or more parameters to the 035 * calling method. 036 * 037 * @param expression a boolean expression 038 * @throws IllegalArgumentException if {@code expression} is false 039 * @author com.google.common.base.Preconditions 040 */ 041 public static void checkArgument(boolean expression) { 042 if (!expression) { 043 throw new IllegalArgumentException(); 044 } 045 } 046 047 /** 048 * Ensures the truth of an expression involving one or more parameters to the 049 * calling method. 050 * 051 * @param expression a boolean expression 052 * @param message to be put into the create {@link IllegalArgumentException} 053 * @throws IllegalArgumentException if {@code expression} is false 054 * @author com.google.common.base.Preconditions 055 */ 056 public static void checkArgument(boolean expression, String message) { 057 if (!expression) { 058 throw new IllegalArgumentException(message); 059 } 060 } 061 062 /** 063 * Ensures the truth of an expression involving one or more parameters to the 064 * calling method. 065 * 066 * @param expression a boolean expression 067 * @param errorMessageTemplate a template for the exception message should the 068 * check fail. The message is formed by replacing 069 * each {@code %s} placeholder in the template with 070 * an argument. These are matched by position - the 071 * first {@code %s} gets {@code 072 * errorMessageArgs[0]} , etc. Unmatched arguments will be appended to 073 * the formatted message in square braces. Unmatched 074 * placeholders will be left as-is. 075 * @param errorMessageArgs the arguments to be substituted into the message 076 * template. Arguments are converted to strings 077 * using {@link String#valueOf(Object)}. 078 * @throws IllegalArgumentException if {@code expression} is false 079 * @author com.google.common.base.Preconditions 080 */ 081 public static void checkArgument(boolean expression, String errorMessageTemplate, Object... errorMessageArgs) { 082 if (!expression) { 083 throw new IllegalArgumentException(lenientFormat(errorMessageTemplate, errorMessageArgs)); 084 } 085 } 086 087 /** 088 * Ensures the truth of an expression involving the state of the calling 089 * instance, but not involving any parameters to the calling method. 090 * 091 * @param expression a boolean expression 092 * @throws IllegalStateException if {@code expression} is false 093 * @author com.google.common.base.Preconditions 094 */ 095 public static void checkState(boolean expression) { 096 if (!expression) { 097 throw new IllegalStateException(); 098 } 099 } 100 101 /** 102 * Ensures the truth of an expression involving the state of the calling 103 * instance, but not involving any parameters to the calling method. 104 * 105 * @param expression a boolean expression 106 * @param message to be put into the create {@link IllegalStateException} 107 * @throws IllegalStateException if {@code expression} is false 108 * @author com.google.common.base.Preconditions 109 */ 110 public static void checkState(boolean expression, String message) { 111 if (!expression) { 112 throw new IllegalStateException(message); 113 } 114 } 115 116 /** 117 * Ensures the truth of an expression involving one or more parameters to the 118 * calling method. 119 * 120 * @param expression a boolean expression 121 * @param errorMessageTemplate a template for the exception message should the 122 * check fail. The message is formed by replacing 123 * each {@code %s} placeholder in the template with 124 * an argument. These are matched by position - the 125 * first {@code %s} gets {@code 126 * errorMessageArgs[0]} , etc. Unmatched arguments will be appended to 127 * the formatted message in square braces. Unmatched 128 * placeholders will be left as-is. 129 * @param errorMessageArgs the arguments to be substituted into the message 130 * template. Arguments are converted to strings 131 * using {@link String#valueOf(Object)}. 132 * @throws IllegalStateException if {@code expression} is false 133 * @author com.google.common.base.Preconditions 134 */ 135 public static void checkState(boolean expression, String errorMessageTemplate, Object... errorMessageArgs) { 136 if (!expression) { 137 throw new IllegalStateException(lenientFormat(errorMessageTemplate, errorMessageArgs)); 138 } 139 } 140}