1 /* 2 * Copyright (C) 2015 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.android.tv.common; 18 19 import android.content.Context; 20 import android.text.TextUtils; 21 import android.util.Log; 22 23 import com.android.tv.common.BuildConfig; 24 import com.android.tv.common.feature.Feature; 25 26 /** 27 * Simple static methods to be called at the start of your own methods to verify 28 * correct arguments and state. 29 * 30 * <p>{@code checkXXX} methods throw exceptions when {@link BuildConfig#ENG} is true, and 31 * logs a warning when it is false. 32 * 33 * <p>This is based on com.android.internal.util.Preconditions. 34 */ 35 public final class SoftPreconditions { 36 private static final String TAG = "SoftPreconditions"; 37 38 /** 39 * Throws or logs if an expression involving the parameter of the calling 40 * method is not true. 41 * 42 * @param expression a boolean expression 43 * @param tag Used to identify the source of a log message. It usually 44 * identifies the class or activity where the log call occurs. 45 * @param msg The message you would like logged. 46 * @throws IllegalArgumentException if {@code expression} is true 47 */ checkArgument(final boolean expression, String tag, String msg)48 public static void checkArgument(final boolean expression, String tag, String msg) { 49 if (!expression) { 50 warn(tag, "Illegal argument", msg, new IllegalArgumentException(msg)); 51 } 52 } 53 54 /** 55 * Throws or logs if an expression involving the parameter of the calling 56 * method is not true. 57 * 58 * @param expression a boolean expression 59 * @throws IllegalArgumentException if {@code expression} is true 60 */ checkArgument(final boolean expression)61 public static void checkArgument(final boolean expression) { 62 checkArgument(expression, null, null); 63 } 64 65 /** 66 * Throws or logs if an and object is null. 67 * 68 * @param reference an object reference 69 * @param tag Used to identify the source of a log message. It usually 70 * identifies the class or activity where the log call occurs. 71 * @param msg The message you would like logged. 72 * @return true if the object is null 73 * @throws NullPointerException if {@code reference} is null 74 */ checkNotNull(final T reference, String tag, String msg)75 public static <T> T checkNotNull(final T reference, String tag, String msg) { 76 if (reference == null) { 77 warn(tag, "Null Pointer", msg, new NullPointerException(msg)); 78 } 79 return reference; 80 } 81 82 /** 83 * Throws or logs if an and object is null. 84 * 85 * @param reference an object reference 86 * @return true if the object is null 87 * @throws NullPointerException if {@code reference} is null 88 */ checkNotNull(final T reference)89 public static <T> T checkNotNull(final T reference) { 90 return checkNotNull(reference, null, null); 91 } 92 93 /** 94 * Throws or logs if an expression involving the state of the calling 95 * instance, but not involving any parameters to the calling method is not true. 96 * 97 * @param expression a boolean expression 98 * @param tag Used to identify the source of a log message. It usually 99 * identifies the class or activity where the log call occurs. 100 * @param msg The message you would like logged. 101 * @throws IllegalStateException if {@code expression} is true 102 */ checkState(final boolean expression, String tag, String msg)103 public static void checkState(final boolean expression, String tag, String msg) { 104 if (!expression) { 105 warn(tag, "Illegal State", msg, new IllegalStateException(msg)); 106 } 107 } 108 109 /** 110 * Throws or logs if an expression involving the state of the calling 111 * instance, but not involving any parameters to the calling method is not true. 112 * 113 * @param expression a boolean expression 114 * @throws IllegalStateException if {@code expression} is true 115 */ checkState(final boolean expression)116 public static void checkState(final boolean expression) { 117 checkState(expression, null, null); 118 } 119 120 /** 121 * Throws or logs if the Feature is not enabled 122 * 123 * @param context an android context 124 * @param feature the required feature 125 * @param tag used to identify the source of a log message. It usually 126 * identifies the class or activity where the log call occurs 127 * @throws IllegalStateException if {@code feature} is not enabled 128 */ checkFeatureEnabled(Context context, Feature feature, String tag)129 public static void checkFeatureEnabled(Context context, Feature feature, String tag) { 130 checkState(feature.isEnabled(context), tag, feature.toString()); 131 } 132 133 /** 134 * Throws a {@link RuntimeException} if {@link BuildConfig#ENG} is true, else log a warning. 135 * 136 * @param tag Used to identify the source of a log message. It usually 137 * identifies the class or activity where the log call occurs. 138 * @param msg The message you would like logged 139 * @param e The exception to wrap with a RuntimeException when thrown. 140 */ warn(String tag, String prefix, String msg, Exception e)141 public static void warn(String tag, String prefix, String msg, Exception e) 142 throws RuntimeException { 143 if (TextUtils.isEmpty(tag)) { 144 tag = TAG; 145 } 146 String logMessage; 147 if (TextUtils.isEmpty(msg)) { 148 logMessage = prefix; 149 } else if (TextUtils.isEmpty(prefix)) { 150 logMessage = msg; 151 } else { 152 logMessage = prefix + ": " + msg; 153 } 154 155 if (BuildConfig.ENG) { 156 throw new RuntimeException(msg, e); 157 } else { 158 Log.w(tag, logMessage, e); 159 } 160 } 161 SoftPreconditions()162 private SoftPreconditions() { 163 } 164 } 165