1 package com.terradue.jcatalogue.client.internal.lang; 2 3 import static java.lang.String.format; 4 5 /** 6 * Code partially extracted from Google Collections 7 * 8 * @since 0.8 9 */ 10 public final class Assertions 11 { 12 13 private Assertions() 14 { 15 // do nothing 16 } 17 18 /** 19 * Ensures the truth of an expression involving one or more parameters to the 20 * calling method. 21 * 22 * @param expression a boolean expression 23 * @param errorMessageTemplate a template for the exception message should the 24 * check fail. The message is formed by replacing each {@code %s} 25 * placeholder in the template with an argument. These are matched by 26 * position - the first {@code %s} gets {@code errorMessageArgs[0]}, etc. 27 * Unmatched arguments will be appended to the formatted message in square 28 * braces. Unmatched placeholders will be left as-is. 29 * @param errorMessageArgs the arguments to be substituted into the message 30 * template. Arguments are converted to strings using 31 * {@link String#valueOf(Object)}. 32 * @throws IllegalArgumentException if {@code expression} is false 33 * @throws NullPointerException if the check fails and either {@code 34 * errorMessageTemplate} or {@code errorMessageArgs} is null (don't let 35 * this happen) 36 */ 37 public static void checkArgument( boolean expression, String errorMessageTemplate, Object... errorMessageArgs ) 38 { 39 if ( !expression ) 40 { 41 throw new IllegalArgumentException( format( errorMessageTemplate, errorMessageArgs ) ); 42 } 43 } 44 45 /** 46 * Ensures the truth of an expression involving the state of the calling 47 * instance, but not involving any parameters to the calling method. 48 * 49 * @param expression a boolean expression 50 * @param errorMessageTemplate a template for the exception message should the 51 * check fail. The message is formed by replacing each {@code %s} 52 * placeholder in the template with an argument. These are matched by 53 * position - the first {@code %s} gets {@code errorMessageArgs[0]}, etc. 54 * Unmatched arguments will be appended to the formatted message in square 55 * braces. Unmatched placeholders will be left as-is. 56 * @param errorMessageArgs the arguments to be substituted into the message template. 57 * @throws IllegalStateException if {@code expression} is false 58 * @throws NullPointerException if the check fails and either {@code 59 * errorMessageTemplate} or {@code errorMessageArgs} is null (don't let 60 * this happen) 61 */ 62 public static void checkState( boolean expression, String errorMessageTemplate, Object... errorMessageArgs ) 63 { 64 if ( !expression ) 65 { 66 throw new IllegalStateException( format( errorMessageTemplate, errorMessageArgs ) ); 67 } 68 } 69 70 /** 71 * Ensures that an object reference passed as a parameter to the calling 72 * method is not null. 73 * 74 * @param reference an object reference 75 * @param errorMessageTemplate a template for the exception message should the 76 * check fail. The message is formed by replacing each {@code %s} 77 * placeholder in the template with an argument. These are matched by 78 * position - the first {@code %s} gets {@code errorMessageArgs[0]}, etc. 79 * Unmatched arguments will be appended to the formatted message in square 80 * braces. Unmatched placeholders will be left as-is. 81 * @param errorMessageArgs the arguments to be substituted into the message 82 * template. Arguments are converted to strings using 83 * {@link String#valueOf(Object)}. 84 * @return the non-null reference that was validated 85 * @throws NullPointerException if {@code reference} is null 86 */ 87 public static <T> T checkNotNull( T reference, String errorMessageTemplate, Object... errorMessageArgs ) 88 { 89 if ( reference == null ) 90 { 91 // If either of these parameters is null, the right thing happens anyway 92 throw new NullPointerException( format( errorMessageTemplate, errorMessageArgs ) ); 93 } 94 return reference; 95 } 96 97 }