1 /* 2 * Copyright (C) 2018 The Android Open Source Project 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 com.google.android.setupcompat.internal; 18 19 /** Commonly used validations and preconditions. */ 20 public final class Validations { 21 22 /** 23 * Asserts that the {@code length} is in the expected range. 24 * 25 * @throws IllegalArgumentException if {@code input}'s length is than {@code minLength} or 26 * greather than {@code maxLength}. 27 */ assertLengthInRange(int length, String name, int minLength, int maxLength)28 public static void assertLengthInRange(int length, String name, int minLength, int maxLength) { 29 Preconditions.checkArgument( 30 length <= maxLength && length >= minLength, 31 String.format("Length of %s should be in the range [%s-%s]", name, minLength, maxLength)); 32 } 33 34 /** 35 * Asserts that the {@code input}'s length is in the expected range. 36 * 37 * @throws NullPointerException if {@code input} is null. 38 * @throws IllegalArgumentException if {@code input}'s length is than {@code minLength} or 39 * greather than {@code maxLength}. 40 */ assertLengthInRange(String input, String name, int minLength, int maxLength)41 public static void assertLengthInRange(String input, String name, int minLength, int maxLength) { 42 Preconditions.checkNotNull(input, String.format("%s cannot be null.", name)); 43 assertLengthInRange(input.length(), name, minLength, maxLength); 44 } 45 Validations()46 private Validations() { 47 throw new AssertionError("Should not be instantiated"); 48 } 49 } 50