public final class

Assertions

extends java.lang.Object

 java.lang.Object

↳androidx.media3.common.util.Assertions

Gradle dependencies

compile group: 'androidx.media3', name: 'media3-common', version: '1.5.0-alpha01'

  • groupId: androidx.media3
  • artifactId: media3-common
  • version: 1.5.0-alpha01

Artifact androidx.media3:media3-common:1.5.0-alpha01 it located at Google repository (https://maven.google.com/)

Overview

Provides methods for asserting the truth of expressions and properties.

Summary

Methods
public static voidcheckArgument(boolean expression)

Throws java.lang.IllegalArgumentException if expression evaluates to false.

public static voidcheckArgument(boolean expression, java.lang.Object errorMessage)

Throws java.lang.IllegalArgumentException if expression evaluates to false.

public static intcheckIndex(int index, int start, int limit)

Throws java.lang.IndexOutOfBoundsException if index falls outside the specified bounds.

public static voidcheckMainThread()

Throws java.lang.IllegalStateException if the calling thread is not the application's main thread.

public static java.lang.StringcheckNotEmpty(java.lang.String string)

Throws java.lang.IllegalArgumentException if string is null or zero length.

public static java.lang.StringcheckNotEmpty(java.lang.String string, java.lang.Object errorMessage)

Throws java.lang.IllegalArgumentException if string is null or zero length.

public static java.lang.ObjectcheckNotNull(java.lang.Object reference)

Throws java.lang.NullPointerException if reference is null.

public static java.lang.ObjectcheckNotNull(java.lang.Object reference, java.lang.Object errorMessage)

Throws java.lang.NullPointerException if reference is null.

public static voidcheckState(boolean expression)

Throws java.lang.IllegalStateException if expression evaluates to false.

public static voidcheckState(boolean expression, java.lang.Object errorMessage)

Throws java.lang.IllegalStateException if expression evaluates to false.

public static java.lang.ObjectcheckStateNotNull(java.lang.Object reference)

Throws java.lang.IllegalStateException if reference is null.

public static java.lang.ObjectcheckStateNotNull(java.lang.Object reference, java.lang.Object errorMessage)

Throws java.lang.IllegalStateException if reference is null.

from java.lang.Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

Methods

public static void checkArgument(boolean expression)

Throws java.lang.IllegalArgumentException if expression evaluates to false.

Parameters:

expression: The expression to evaluate.

public static void checkArgument(boolean expression, java.lang.Object errorMessage)

Throws java.lang.IllegalArgumentException if expression evaluates to false.

Parameters:

expression: The expression to evaluate.
errorMessage: The exception message if an exception is thrown. The message is converted to a java.lang.String using valueOf.

public static int checkIndex(int index, int start, int limit)

Throws java.lang.IndexOutOfBoundsException if index falls outside the specified bounds.

Parameters:

index: The index to test.
start: The start of the allowed range (inclusive).
limit: The end of the allowed range (exclusive).

Returns:

The index that was validated.

public static void checkState(boolean expression)

Throws java.lang.IllegalStateException if expression evaluates to false.

Parameters:

expression: The expression to evaluate.

public static void checkState(boolean expression, java.lang.Object errorMessage)

Throws java.lang.IllegalStateException if expression evaluates to false.

Parameters:

expression: The expression to evaluate.
errorMessage: The exception message if an exception is thrown. The message is converted to a java.lang.String using valueOf.

public static java.lang.Object checkStateNotNull(java.lang.Object reference)

Throws java.lang.IllegalStateException if reference is null.

Parameters:

reference: The reference.

Returns:

The non-null reference that was validated.

public static java.lang.Object checkStateNotNull(java.lang.Object reference, java.lang.Object errorMessage)

Throws java.lang.IllegalStateException if reference is null.

Parameters:

reference: The reference.
errorMessage: The exception message to use if the check fails. The message is converted to a string using valueOf.

Returns:

The non-null reference that was validated.

public static java.lang.Object checkNotNull(java.lang.Object reference)

Throws java.lang.NullPointerException if reference is null.

Parameters:

reference: The reference.

Returns:

The non-null reference that was validated.

public static java.lang.Object checkNotNull(java.lang.Object reference, java.lang.Object errorMessage)

Throws java.lang.NullPointerException if reference is null.

Parameters:

reference: The reference.
errorMessage: The exception message to use if the check fails. The message is converted to a string using valueOf.

Returns:

The non-null reference that was validated.

public static java.lang.String checkNotEmpty(java.lang.String string)

Throws java.lang.IllegalArgumentException if string is null or zero length.

Parameters:

string: The string to check.

Returns:

The non-null, non-empty string that was validated.

public static java.lang.String checkNotEmpty(java.lang.String string, java.lang.Object errorMessage)

Throws java.lang.IllegalArgumentException if string is null or zero length.

Parameters:

string: The string to check.
errorMessage: The exception message to use if the check fails. The message is converted to a string using valueOf.

Returns:

The non-null, non-empty string that was validated.

public static void checkMainThread()

Throws java.lang.IllegalStateException if the calling thread is not the application's main thread.

Source

/*
 * Copyright (C) 2016 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package androidx.media3.common.util;

import android.os.Looper;
import android.text.TextUtils;
import androidx.annotation.Nullable;
import androidx.media3.common.MediaLibraryInfo;
import org.checkerframework.checker.nullness.qual.EnsuresNonNull;
import org.checkerframework.dataflow.qual.Pure;

/** Provides methods for asserting the truth of expressions and properties. */
@UnstableApi
public final class Assertions {

  private Assertions() {}

  /**
   * Throws {@link IllegalArgumentException} if {@code expression} evaluates to false.
   *
   * @param expression The expression to evaluate.
   * @throws IllegalArgumentException If {@code expression} is false.
   */
  @Pure
  public static void checkArgument(boolean expression) {
    if (MediaLibraryInfo.ASSERTIONS_ENABLED && !expression) {
      throw new IllegalArgumentException();
    }
  }

  /**
   * Throws {@link IllegalArgumentException} if {@code expression} evaluates to false.
   *
   * @param expression The expression to evaluate.
   * @param errorMessage The exception message if an exception is thrown. The message is converted
   *     to a {@link String} using {@link String#valueOf(Object)}.
   * @throws IllegalArgumentException If {@code expression} is false.
   */
  @Pure
  public static void checkArgument(boolean expression, Object errorMessage) {
    if (MediaLibraryInfo.ASSERTIONS_ENABLED && !expression) {
      throw new IllegalArgumentException(String.valueOf(errorMessage));
    }
  }

  /**
   * Throws {@link IndexOutOfBoundsException} if {@code index} falls outside the specified bounds.
   *
   * @param index The index to test.
   * @param start The start of the allowed range (inclusive).
   * @param limit The end of the allowed range (exclusive).
   * @return The {@code index} that was validated.
   * @throws IndexOutOfBoundsException If {@code index} falls outside the specified bounds.
   */
  @Pure
  public static int checkIndex(int index, int start, int limit) {
    if (index < start || index >= limit) {
      throw new IndexOutOfBoundsException();
    }
    return index;
  }

  /**
   * Throws {@link IllegalStateException} if {@code expression} evaluates to false.
   *
   * @param expression The expression to evaluate.
   * @throws IllegalStateException If {@code expression} is false.
   */
  @Pure
  public static void checkState(boolean expression) {
    if (MediaLibraryInfo.ASSERTIONS_ENABLED && !expression) {
      throw new IllegalStateException();
    }
  }

  /**
   * Throws {@link IllegalStateException} if {@code expression} evaluates to false.
   *
   * @param expression The expression to evaluate.
   * @param errorMessage The exception message if an exception is thrown. The message is converted
   *     to a {@link String} using {@link String#valueOf(Object)}.
   * @throws IllegalStateException If {@code expression} is false.
   */
  @Pure
  public static void checkState(boolean expression, Object errorMessage) {
    if (MediaLibraryInfo.ASSERTIONS_ENABLED && !expression) {
      throw new IllegalStateException(String.valueOf(errorMessage));
    }
  }

  /**
   * Throws {@link IllegalStateException} if {@code reference} is null.
   *
   * @param <T> The type of the reference.
   * @param reference The reference.
   * @return The non-null reference that was validated.
   * @throws IllegalStateException If {@code reference} is null.
   */
  @SuppressWarnings({"nullness:contracts.postcondition", "nullness:return"})
  @EnsuresNonNull({"#1"})
  @Pure
  public static <T> T checkStateNotNull(@Nullable T reference) {
    if (MediaLibraryInfo.ASSERTIONS_ENABLED && reference == null) {
      throw new IllegalStateException();
    }
    return reference;
  }

  /**
   * Throws {@link IllegalStateException} if {@code reference} is null.
   *
   * @param <T> The type of the reference.
   * @param reference The reference.
   * @param errorMessage The exception message to use if the check fails. The message is converted
   *     to a string using {@link String#valueOf(Object)}.
   * @return The non-null reference that was validated.
   * @throws IllegalStateException If {@code reference} is null.
   */
  @SuppressWarnings({"nullness:contracts.postcondition", "nullness:return"})
  @EnsuresNonNull({"#1"})
  @Pure
  public static <T> T checkStateNotNull(@Nullable T reference, Object errorMessage) {
    if (MediaLibraryInfo.ASSERTIONS_ENABLED && reference == null) {
      throw new IllegalStateException(String.valueOf(errorMessage));
    }
    return reference;
  }

  /**
   * Throws {@link NullPointerException} if {@code reference} is null.
   *
   * @param <T> The type of the reference.
   * @param reference The reference.
   * @return The non-null reference that was validated.
   * @throws NullPointerException If {@code reference} is null.
   */
  @SuppressWarnings({"nullness:contracts.postcondition", "nullness:return"})
  @EnsuresNonNull({"#1"})
  @Pure
  public static <T> T checkNotNull(@Nullable T reference) {
    if (MediaLibraryInfo.ASSERTIONS_ENABLED && reference == null) {
      throw new NullPointerException();
    }
    return reference;
  }

  /**
   * Throws {@link NullPointerException} if {@code reference} is null.
   *
   * @param <T> The type of the reference.
   * @param reference The reference.
   * @param errorMessage The exception message to use if the check fails. The message is converted
   *     to a string using {@link String#valueOf(Object)}.
   * @return The non-null reference that was validated.
   * @throws NullPointerException If {@code reference} is null.
   */
  @SuppressWarnings({"nullness:contracts.postcondition", "nullness:return"})
  @EnsuresNonNull({"#1"})
  @Pure
  public static <T> T checkNotNull(@Nullable T reference, Object errorMessage) {
    if (MediaLibraryInfo.ASSERTIONS_ENABLED && reference == null) {
      throw new NullPointerException(String.valueOf(errorMessage));
    }
    return reference;
  }

  /**
   * Throws {@link IllegalArgumentException} if {@code string} is null or zero length.
   *
   * @param string The string to check.
   * @return The non-null, non-empty string that was validated.
   * @throws IllegalArgumentException If {@code string} is null or 0-length.
   */
  @SuppressWarnings({"nullness:contracts.postcondition", "nullness:return"})
  @EnsuresNonNull({"#1"})
  @Pure
  public static String checkNotEmpty(@Nullable String string) {
    if (MediaLibraryInfo.ASSERTIONS_ENABLED && TextUtils.isEmpty(string)) {
      throw new IllegalArgumentException();
    }
    return string;
  }

  /**
   * Throws {@link IllegalArgumentException} if {@code string} is null or zero length.
   *
   * @param string The string to check.
   * @param errorMessage The exception message to use if the check fails. The message is converted
   *     to a string using {@link String#valueOf(Object)}.
   * @return The non-null, non-empty string that was validated.
   * @throws IllegalArgumentException If {@code string} is null or 0-length.
   */
  @SuppressWarnings({"nullness:contracts.postcondition", "nullness:return"})
  @EnsuresNonNull({"#1"})
  @Pure
  public static String checkNotEmpty(@Nullable String string, Object errorMessage) {
    if (MediaLibraryInfo.ASSERTIONS_ENABLED && TextUtils.isEmpty(string)) {
      throw new IllegalArgumentException(String.valueOf(errorMessage));
    }
    return string;
  }

  /**
   * Throws {@link IllegalStateException} if the calling thread is not the application's main
   * thread.
   *
   * @throws IllegalStateException If the calling thread is not the application's main thread.
   */
  @Pure
  public static void checkMainThread() {
    if (MediaLibraryInfo.ASSERTIONS_ENABLED && Looper.myLooper() != Looper.getMainLooper()) {
      throw new IllegalStateException("Not in applications main thread");
    }
  }
}