1 /* 2 * Copyright (C) 2017 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 package android.app.timezone; 17 18 /** 19 * Shared code for android.app.timezone classes. 20 */ 21 final class Utils { Utils()22 private Utils() {} 23 validateVersion(String type, int version)24 static int validateVersion(String type, int version) { 25 if (version < 0 || version > 999) { 26 throw new IllegalArgumentException("Invalid " + type + " version=" + version); 27 } 28 return version; 29 } 30 validateRulesVersion(String type, String rulesVersion)31 static String validateRulesVersion(String type, String rulesVersion) { 32 validateNotNull(type, rulesVersion); 33 34 if (rulesVersion.isEmpty()) { 35 throw new IllegalArgumentException(type + " must not be empty"); 36 } 37 return rulesVersion; 38 } 39 40 /** Validates that {@code object} is not null. Always returns {@code object}. */ validateNotNull(String type, T object)41 static <T> T validateNotNull(String type, T object) { 42 if (object == null) { 43 throw new NullPointerException(type + " == null"); 44 } 45 return object; 46 } 47 48 /** 49 * If {@code requireNotNull} is {@code true} calls {@link #validateNotNull(String, Object)}, 50 * and {@link #validateNull(String, Object)} otherwise. Returns {@code object}. 51 */ validateConditionalNull(boolean requireNotNull, String type, T object)52 static <T> T validateConditionalNull(boolean requireNotNull, String type, T object) { 53 if (requireNotNull) { 54 return validateNotNull(type, object); 55 } else { 56 return validateNull(type, object); 57 } 58 } 59 60 /** Validates that {@code object} is null. Always returns null. */ validateNull(String type, T object)61 static <T> T validateNull(String type, T object) { 62 if (object != null) { 63 throw new IllegalArgumentException(type + " != null"); 64 } 65 return null; 66 } 67 } 68