1/* 2 * Copyright (C) 2016 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 */ 16package com.android.car.apps.common.util; 17 18import android.os.Looper; 19import android.util.Log; 20 21/** 22 * A helper class which checks a given runtime assumption. If the assumption fails this class will 23 * log a error and if the current BuildConfig is set to debug the failed assertion will throw an 24 * exception. 25 */ 26public class Assert { 27 private static final String TAG = "Em.MediaAssert"; 28 29 private Assert() {} 30 31 /** 32 * Checks that the current thread is the main thread and throws an {@link AssertionError} if it 33 * is not. 34 */ 35 public static void isMainThread() { 36 if (Looper.myLooper() != Looper.getMainLooper()) { 37 fail("Expected to run on main thread."); 38 } 39 } 40 41 /** 42 * Throws an {@link AssertionError} with the given message. Also will write the message to 43 * {@link Log}. 44 */ 45 public static void fail(final String message) { 46 Log.e(TAG, "Assert.fail() called: " + message); 47 throw new AssertionError(message); 48 } 49}