public final class

BundleCollectionUtil

extends java.lang.Object

 java.lang.Object

↳androidx.media3.common.util.BundleCollectionUtil

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

Utilities for converting collections to and from instances.

Summary

Methods
public static java.util.HashMap<java.lang.String, java.lang.String>bundleToStringHashMap(Bundle bundle)

public static <any>bundleToStringImmutableMap(Bundle bundle)

public static voidensureClassLoader(Bundle bundle)

Sets the application class loader to the given if no class loader is present.

public static <any>fromBundleList(<any> fromBundleFunc, java.util.List<Bundle> bundleList)

Unbundles a list of instances to a list of objects.

public static <any>fromBundleSparseArray(<any> fromBundleFunc, <any> bundleSparseArray)

Unbundles a of instances to a of objects.

public static BundlegetBundleWithDefault(Bundle bundle, java.lang.String field, Bundle defaultValue)

public static java.util.ArrayList<java.lang.Integer>getIntegerArrayListWithDefault(Bundle bundle, java.lang.String field, java.util.ArrayList<java.lang.Integer> defaultValue)

public static BundlestringMapToBundle(java.util.Map<java.lang.String, java.lang.String> map)

public static java.util.ArrayList<Bundle>toBundleArrayList(java.util.Collection<java.lang.Object> items, <any> toBundleFunc)

Bundles a collection of objects to an java.util.ArrayList of instances so that the returned list can be put to using conveniently.

public static <any>toBundleList(java.util.List<java.lang.Object> list, <any> toBundleFunc)

Bundles a list of objects to a list of instances.

public static <any>toBundleSparseArray(<any> items, <any> toBundleFunc)

Bundles a of objects to a of instances so that the returned can be put to using conveniently.

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

Methods

public static <any> toBundleList(java.util.List<java.lang.Object> list, <any> toBundleFunc)

Bundles a list of objects to a list of instances.

Parameters:

list: List of items to be bundled.
toBundleFunc: Function that specifies how to bundle each item.

Returns:

The ImmutableList of bundled items.

public static <any> fromBundleList(<any> fromBundleFunc, java.util.List<Bundle> bundleList)

Unbundles a list of instances to a list of objects.

Parameters:

fromBundleFunc: Function that specified how to unbundle each item.
bundleList: List of instances to be unbundled.

Returns:

The ImmutableList of unbundled items.

public static java.util.ArrayList<Bundle> toBundleArrayList(java.util.Collection<java.lang.Object> items, <any> toBundleFunc)

Bundles a collection of objects to an java.util.ArrayList of instances so that the returned list can be put to using conveniently.

Parameters:

items: Collection of items to be bundled.
toBundleFunc: Function that specifies how to bundle each item.

Returns:

The java.util.ArrayList of bundled items.

public static <any> fromBundleSparseArray(<any> fromBundleFunc, <any> bundleSparseArray)

Unbundles a of instances to a of objects.

Parameters:

fromBundleFunc: Function that specified how to unbundle each item.
bundleSparseArray: of instances to be unbundled.

Returns:

The of unbundled items.

public static <any> toBundleSparseArray(<any> items, <any> toBundleFunc)

Bundles a of objects to a of instances so that the returned can be put to using conveniently.

Parameters:

items: Collection of items to be bundled.
toBundleFunc: Function that specifies how to bundle each item.

Returns:

The of bundled items.

public static Bundle stringMapToBundle(java.util.Map<java.lang.String, java.lang.String> map)

public static java.util.HashMap<java.lang.String, java.lang.String> bundleToStringHashMap(Bundle bundle)

public static <any> bundleToStringImmutableMap(Bundle bundle)

public static Bundle getBundleWithDefault(Bundle bundle, java.lang.String field, Bundle defaultValue)

public static java.util.ArrayList<java.lang.Integer> getIntegerArrayListWithDefault(Bundle bundle, java.lang.String field, java.util.ArrayList<java.lang.Integer> defaultValue)

public static void ensureClassLoader(Bundle bundle)

Sets the application class loader to the given if no class loader is present.

This assumes that all classes unparceled from bundle are sharing the class loader of BundleCollectionUtil.

Source

/*
 * Copyright 2021 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 static androidx.media3.common.util.Assertions.checkNotNull;
import static androidx.media3.common.util.Util.castNonNull;

import android.os.Bundle;
import android.util.SparseArray;
import androidx.annotation.Nullable;
import com.google.common.base.Function;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.checkerframework.checker.nullness.qual.NonNull;

/** Utilities for converting collections to and from {@link Bundle} instances. */
@UnstableApi
public final class BundleCollectionUtil {

  /**
   * Bundles a list of objects to a list of {@link Bundle} instances.
   *
   * @param list List of items to be bundled.
   * @param toBundleFunc Function that specifies how to bundle each item.
   * @return The {@link ImmutableList} of bundled items.
   */
  public static <T extends @NonNull Object> ImmutableList<Bundle> toBundleList(
      List<T> list, Function<T, Bundle> toBundleFunc) {
    ImmutableList.Builder<Bundle> builder = ImmutableList.builder();
    for (int i = 0; i < list.size(); i++) {
      T item = list.get(i);
      builder.add(toBundleFunc.apply(item));
    }
    return builder.build();
  }

  /**
   * Unbundles a list of {@link Bundle} instances to a list of objects.
   *
   * @param fromBundleFunc Function that specified how to unbundle each item.
   * @param bundleList List of {@link Bundle} instances to be unbundled.
   * @return The {@link ImmutableList} of unbundled items.
   */
  public static <T extends @NonNull Object> ImmutableList<T> fromBundleList(
      Function<Bundle, T> fromBundleFunc, List<Bundle> bundleList) {
    ImmutableList.Builder<T> builder = ImmutableList.builder();
    for (int i = 0; i < bundleList.size(); i++) {
      Bundle bundle = checkNotNull(bundleList.get(i)); // Fail fast during parsing.
      T item = fromBundleFunc.apply(bundle);
      builder.add(item);
    }
    return builder.build();
  }

  /**
   * Bundles a collection of objects to an {@link ArrayList} of {@link Bundle} instances so that the
   * returned list can be put to {@link Bundle} using {@link Bundle#putParcelableArrayList}
   * conveniently.
   *
   * @param items Collection of items to be bundled.
   * @param toBundleFunc Function that specifies how to bundle each item.
   * @return The {@link ArrayList} of bundled items.
   */
  @SuppressWarnings("NonApiType") // Intentionally using ArrayList for putParcelableArrayList.
  public static <T extends @NonNull Object> ArrayList<Bundle> toBundleArrayList(
      Collection<T> items, Function<T, Bundle> toBundleFunc) {
    ArrayList<Bundle> arrayList = new ArrayList<>(items.size());
    for (T item : items) {
      arrayList.add(toBundleFunc.apply(item));
    }
    return arrayList;
  }

  /**
   * Unbundles a {@link SparseArray} of {@link Bundle} instances to a {@link SparseArray} of
   * objects.
   *
   * @param fromBundleFunc Function that specified how to unbundle each item.
   * @param bundleSparseArray {@link SparseArray} of {@link Bundle} instances to be unbundled.
   * @return The {@link SparseArray} of unbundled items.
   */
  public static <T extends @NonNull Object> SparseArray<T> fromBundleSparseArray(
      Function<Bundle, T> fromBundleFunc, SparseArray<Bundle> bundleSparseArray) {
    SparseArray<T> result = new SparseArray<>(bundleSparseArray.size());
    for (int i = 0; i < bundleSparseArray.size(); i++) {
      result.put(bundleSparseArray.keyAt(i), fromBundleFunc.apply(bundleSparseArray.valueAt(i)));
    }
    return result;
  }

  /**
   * Bundles a {@link SparseArray} of objects to a {@link SparseArray} of {@link Bundle} instances
   * so that the returned {@link SparseArray} can be put to {@link Bundle} using {@link
   * Bundle#putSparseParcelableArray} conveniently.
   *
   * @param items Collection of items to be bundled.
   * @param toBundleFunc Function that specifies how to bundle each item.
   * @return The {@link SparseArray} of bundled items.
   */
  public static <T extends @NonNull Object> SparseArray<Bundle> toBundleSparseArray(
      SparseArray<T> items, Function<T, Bundle> toBundleFunc) {
    SparseArray<Bundle> sparseArray = new SparseArray<>(items.size());
    for (int i = 0; i < items.size(); i++) {
      sparseArray.put(items.keyAt(i), toBundleFunc.apply(items.valueAt(i)));
    }
    return sparseArray;
  }

  public static Bundle stringMapToBundle(Map<String, String> map) {
    Bundle bundle = new Bundle();
    for (Map.Entry<String, String> entry : map.entrySet()) {
      bundle.putString(entry.getKey(), entry.getValue());
    }
    return bundle;
  }

  public static HashMap<String, String> bundleToStringHashMap(Bundle bundle) {
    HashMap<String, String> map = new HashMap<>();
    if (bundle == Bundle.EMPTY) {
      return map;
    }
    for (String key : bundle.keySet()) {
      @Nullable String value = bundle.getString(key);
      if (value != null) {
        map.put(key, value);
      }
    }
    return map;
  }

  public static ImmutableMap<String, String> bundleToStringImmutableMap(Bundle bundle) {
    if (bundle == Bundle.EMPTY) {
      return ImmutableMap.of();
    }
    HashMap<String, String> map = bundleToStringHashMap(bundle);
    return ImmutableMap.copyOf(map);
  }

  public static Bundle getBundleWithDefault(Bundle bundle, String field, Bundle defaultValue) {
    @Nullable Bundle result = bundle.getBundle(field);
    return result != null ? result : defaultValue;
  }

  public static ArrayList<Integer> getIntegerArrayListWithDefault(
      Bundle bundle, String field, ArrayList<Integer> defaultValue) {
    @Nullable ArrayList<Integer> result = bundle.getIntegerArrayList(field);
    return result != null ? result : defaultValue;
  }

  /**
   * Sets the application class loader to the given {@link Bundle} if no class loader is present.
   *
   * <p>This assumes that all classes unparceled from {@code bundle} are sharing the class loader of
   * {@code BundleCollectionUtil}.
   */
  public static void ensureClassLoader(@Nullable Bundle bundle) {
    if (bundle != null) {
      bundle.setClassLoader(castNonNull(BundleCollectionUtil.class.getClassLoader()));
    }
  }

  private BundleCollectionUtil() {}
}