1 /* 2 * Copyright (C) 2020 The Dagger Authors. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package dagger.hilt.internal; 18 19 /** 20 * A partial copy of Guava's {@code com.google.common.base.Preconditions} meant to be used by 21 * generated code. TODO(danysantiago): Consolidate with dagger.internal.Preconditions 22 */ 23 public final class Preconditions { 24 25 /** 26 * Ensures that an object reference passed as a parameter to the calling method is not null. 27 * 28 * @param reference an object reference 29 * @return the non-null reference that was validated 30 * @throws NullPointerException if {@code reference} is null 31 */ checkNotNull(T reference)32 public static <T> T checkNotNull(T reference) { 33 if (reference == null) { 34 throw new NullPointerException(); 35 } 36 return reference; 37 } 38 39 /** 40 * Ensures that an object reference passed as a parameter to the calling method is not null. 41 * 42 * @param reference an object reference 43 * @param errorMessage the exception message to use if the check fails 44 * @return the non-null reference that was validated 45 * @throws NullPointerException if {@code reference} is null 46 */ checkNotNull(T reference, String errorMessage)47 public static <T> T checkNotNull(T reference, String errorMessage) { 48 if (reference == null) { 49 throw new NullPointerException(errorMessage); 50 } 51 return reference; 52 } 53 54 /** 55 * Ensures the truth of an expression involving one or more parameters to the calling method. 56 * 57 * @param expression a boolean expression 58 * @param errorMessageTemplate a template for the exception message should the check fail. The 59 * message is formed by replacing each occurrence of {@code "%s"} with the corresponding 60 * argument value from {@code args}. 61 * @param args the arguments to be substituted into the message template. 62 * @throws IllegalArgumentException if {@code expression} is false 63 */ checkArgument( boolean expression, String errorMessageTemplate, Object... args)64 public static void checkArgument( 65 boolean expression, String errorMessageTemplate, Object... args) { 66 if (!expression) { 67 throw new IllegalArgumentException(String.format(errorMessageTemplate, args)); 68 } 69 } 70 71 /** 72 * Ensures the truth of an expression involving one or more parameters to the calling method. 73 * 74 * @param expression a boolean expression 75 * @param errorMessageTemplate a template for the exception message should the check fail. The 76 * message is formed by replacing each occurrence of {@code "%s"} with the corresponding 77 * argument value from {@code args}. 78 * @param args the arguments to be substituted into the message template. 79 * @throws IllegalStateException if {@code expression} is false 80 */ checkState(boolean expression, String errorMessageTemplate, Object... args)81 public static void checkState(boolean expression, String errorMessageTemplate, Object... args) { 82 if (!expression) { 83 throw new IllegalStateException(String.format(errorMessageTemplate, args)); 84 } 85 } 86 Preconditions()87 private Preconditions() {} 88 } 89