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