001// Generated by delombok at Sat Feb 25 17:58:34 UTC 2023 002package io.cui.tools.base; 003 004import static io.cui.tools.string.MoreStrings.lenientFormat; 005 006/** 007 * Inspired by com.google.common.base.Preconditions. Defines a subset of the corresponding 008 * Preconditions 009 * 010 * @author com.google.common.base.Preconditions 011 * @author Oliver Wolff 012 */ 013public final class Preconditions { 014 /** 015 * Ensures the truth of an expression involving one or more parameters to the calling method. 016 * 017 * @param expression a boolean expression 018 * @throws IllegalArgumentException if {@code expression} is false 019 * @author com.google.common.base.Preconditions 020 */ 021 public static void checkArgument(boolean expression) { 022 if (!expression) { 023 throw new IllegalArgumentException(); 024 } 025 } 026 027 /** 028 * Ensures the truth of an expression involving one or more parameters to the calling method. 029 * 030 * @param expression a boolean expression 031 * @param message to be put into the create {@link IllegalArgumentException} 032 * @throws IllegalArgumentException if {@code expression} is false 033 * @author com.google.common.base.Preconditions 034 */ 035 public static void checkArgument(boolean expression, String message) { 036 if (!expression) { 037 throw new IllegalArgumentException(message); 038 } 039 } 040 041 /** 042 * Ensures the truth of an expression involving one or more parameters to the calling method. 043 * 044 * @param expression a boolean expression 045 * @param errorMessageTemplate a template for the exception message should the check fail. The 046 * message is formed by replacing each {@code %s} placeholder in the template with an 047 * argument. These are matched by position - the first {@code %s} gets {@code 048 * errorMessageArgs[0]}, etc. Unmatched arguments will be appended to the formatted message 049 * in square braces. Unmatched placeholders will be left as-is. 050 * @param errorMessageArgs the arguments to be substituted into the message template. Arguments 051 * are converted to strings using {@link String#valueOf(Object)}. 052 * @throws IllegalArgumentException if {@code expression} is false 053 * @author com.google.common.base.Preconditions 054 */ 055 public static void checkArgument(boolean expression, String errorMessageTemplate, Object... errorMessageArgs) { 056 if (!expression) { 057 throw new IllegalArgumentException(lenientFormat(errorMessageTemplate, errorMessageArgs)); 058 } 059 } 060 061 /** 062 * Ensures the truth of an expression involving the state of the calling instance, but not 063 * involving any parameters to the calling method. 064 * 065 * @param expression a boolean expression 066 * @throws IllegalStateException if {@code expression} is false 067 * @author com.google.common.base.Preconditions 068 */ 069 public static void checkState(boolean expression) { 070 if (!expression) { 071 throw new IllegalStateException(); 072 } 073 } 074 075 /** 076 * Ensures the truth of an expression involving the state of the calling instance, but not 077 * involving any parameters to the calling method. 078 * 079 * @param expression a boolean expression 080 * @param message to be put into the create {@link IllegalStateException} 081 * @throws IllegalStateException if {@code expression} is false 082 * @author com.google.common.base.Preconditions 083 */ 084 public static void checkState(boolean expression, String message) { 085 if (!expression) { 086 throw new IllegalStateException(message); 087 } 088 } 089 090 /** 091 * Ensures the truth of an expression involving one or more parameters to the calling method. 092 * 093 * @param expression a boolean expression 094 * @param errorMessageTemplate a template for the exception message should the check fail. The 095 * message is formed by replacing each {@code %s} placeholder in the template with an 096 * argument. These are matched by position - the first {@code %s} gets {@code 097 * errorMessageArgs[0]}, etc. Unmatched arguments will be appended to the formatted message 098 * in square braces. Unmatched placeholders will be left as-is. 099 * @param errorMessageArgs the arguments to be substituted into the message template. Arguments 100 * are converted to strings using {@link String#valueOf(Object)}. 101 * @throws IllegalStateException if {@code expression} is false 102 * @author com.google.common.base.Preconditions 103 */ 104 public static void checkState(boolean expression, String errorMessageTemplate, Object... errorMessageArgs) { 105 if (!expression) { 106 throw new IllegalStateException(lenientFormat(errorMessageTemplate, errorMessageArgs)); 107 } 108 } 109 110 @java.lang.SuppressWarnings("all") 111 @lombok.Generated 112 private Preconditions() { 113 throw new java.lang.UnsupportedOperationException("This is a utility class and cannot be instantiated"); 114 } 115}