public class

ContentMetadataMutations

extends java.lang.Object

 java.lang.Object

↳androidx.media3.datasource.cache.ContentMetadataMutations

Gradle dependencies

compile group: 'androidx.media3', name: 'media3-datasource', version: '1.0.0-alpha03'

  • groupId: androidx.media3
  • artifactId: media3-datasource
  • version: 1.0.0-alpha03

Artifact androidx.media3:media3-datasource:1.0.0-alpha03 it located at Google repository (https://maven.google.com/)

Overview

Defines multiple mutations on metadata value which are applied atomically. This class isn't thread safe.

Summary

Constructors
publicContentMetadataMutations()

Constructs a DefaultMetadataMutations.

Methods
public java.util.Map<java.lang.String, java.lang.Object>getEditedValues()

Returns a map of metadata name, value pairs to be set.

public java.util.List<java.lang.String>getRemovedValues()

Returns a list of names of metadata values to be removed.

public ContentMetadataMutationsremove(java.lang.String name)

Adds a mutation to remove a metadata value.

public ContentMetadataMutationsset(java.lang.String name, byte[] value[])

Adds a mutation to set a metadata value.

public ContentMetadataMutationsset(java.lang.String name, long value)

Adds a mutation to set a metadata value.

public ContentMetadataMutationsset(java.lang.String name, java.lang.String value)

Adds a mutation to set a metadata value.

public static ContentMetadataMutationssetContentLength(ContentMetadataMutations mutations, long length)

Adds a mutation to set the ContentMetadata.KEY_CONTENT_LENGTH value, or to remove any existing value if C.LENGTH_UNSET is passed.

public static ContentMetadataMutationssetRedirectedUri(ContentMetadataMutations mutations, Uri uri)

Adds a mutation to set the ContentMetadata.KEY_REDIRECTED_URI value, or to remove any existing entry if null is passed.

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

Constructors

public ContentMetadataMutations()

Constructs a DefaultMetadataMutations.

Methods

public static ContentMetadataMutations setContentLength(ContentMetadataMutations mutations, long length)

Adds a mutation to set the ContentMetadata.KEY_CONTENT_LENGTH value, or to remove any existing value if C.LENGTH_UNSET is passed.

Parameters:

mutations: The mutations to modify.
length: The length value, or C.LENGTH_UNSET to remove any existing entry.

Returns:

The mutations instance, for convenience.

public static ContentMetadataMutations setRedirectedUri(ContentMetadataMutations mutations, Uri uri)

Adds a mutation to set the ContentMetadata.KEY_REDIRECTED_URI value, or to remove any existing entry if null is passed.

Parameters:

mutations: The mutations to modify.
uri: The value, or null to remove any existing entry.

Returns:

The mutations instance, for convenience.

public ContentMetadataMutations set(java.lang.String name, java.lang.String value)

Adds a mutation to set a metadata value.

Parameters:

name: The name of the metadata value.
value: The value to be set.

Returns:

This instance, for convenience.

public ContentMetadataMutations set(java.lang.String name, long value)

Adds a mutation to set a metadata value.

Parameters:

name: The name of the metadata value.
value: The value to be set.

Returns:

This instance, for convenience.

public ContentMetadataMutations set(java.lang.String name, byte[] value[])

Adds a mutation to set a metadata value.

Parameters:

name: The name of the metadata value.
value: The value to be set.

Returns:

This instance, for convenience.

public ContentMetadataMutations remove(java.lang.String name)

Adds a mutation to remove a metadata value.

Parameters:

name: The name of the metadata value.

Returns:

This instance, for convenience.

public java.util.List<java.lang.String> getRemovedValues()

Returns a list of names of metadata values to be removed.

public java.util.Map<java.lang.String, java.lang.Object> getEditedValues()

Returns a map of metadata name, value pairs to be set. Values are copied.

Source

/*
 * Copyright (C) 2018 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.datasource.cache;

import android.net.Uri;
import androidx.annotation.Nullable;
import androidx.media3.common.C;
import androidx.media3.common.util.Assertions;
import androidx.media3.common.util.UnstableApi;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

/**
 * Defines multiple mutations on metadata value which are applied atomically. This class isn't
 * thread safe.
 */
@UnstableApi
public class ContentMetadataMutations {

  /**
   * Adds a mutation to set the {@link ContentMetadata#KEY_CONTENT_LENGTH} value, or to remove any
   * existing value if {@link C#LENGTH_UNSET} is passed.
   *
   * @param mutations The mutations to modify.
   * @param length The length value, or {@link C#LENGTH_UNSET} to remove any existing entry.
   * @return The mutations instance, for convenience.
   */
  public static ContentMetadataMutations setContentLength(
      ContentMetadataMutations mutations, long length) {
    return mutations.set(ContentMetadata.KEY_CONTENT_LENGTH, length);
  }

  /**
   * Adds a mutation to set the {@link ContentMetadata#KEY_REDIRECTED_URI} value, or to remove any
   * existing entry if {@code null} is passed.
   *
   * @param mutations The mutations to modify.
   * @param uri The {@link Uri} value, or {@code null} to remove any existing entry.
   * @return The mutations instance, for convenience.
   */
  public static ContentMetadataMutations setRedirectedUri(
      ContentMetadataMutations mutations, @Nullable Uri uri) {
    if (uri == null) {
      return mutations.remove(ContentMetadata.KEY_REDIRECTED_URI);
    } else {
      return mutations.set(ContentMetadata.KEY_REDIRECTED_URI, uri.toString());
    }
  }

  private final Map<String, Object> editedValues;
  private final List<String> removedValues;

  /** Constructs a DefaultMetadataMutations. */
  public ContentMetadataMutations() {
    editedValues = new HashMap<>();
    removedValues = new ArrayList<>();
  }

  /**
   * Adds a mutation to set a metadata value.
   *
   * @param name The name of the metadata value.
   * @param value The value to be set.
   * @return This instance, for convenience.
   */
  public ContentMetadataMutations set(String name, String value) {
    return checkAndSet(name, value);
  }

  /**
   * Adds a mutation to set a metadata value.
   *
   * @param name The name of the metadata value.
   * @param value The value to be set.
   * @return This instance, for convenience.
   */
  public ContentMetadataMutations set(String name, long value) {
    return checkAndSet(name, value);
  }

  /**
   * Adds a mutation to set a metadata value.
   *
   * @param name The name of the metadata value.
   * @param value The value to be set.
   * @return This instance, for convenience.
   */
  public ContentMetadataMutations set(String name, byte[] value) {
    return checkAndSet(name, Arrays.copyOf(value, value.length));
  }

  /**
   * Adds a mutation to remove a metadata value.
   *
   * @param name The name of the metadata value.
   * @return This instance, for convenience.
   */
  public ContentMetadataMutations remove(String name) {
    removedValues.add(name);
    editedValues.remove(name);
    return this;
  }

  /** Returns a list of names of metadata values to be removed. */
  public List<String> getRemovedValues() {
    return Collections.unmodifiableList(new ArrayList<>(removedValues));
  }

  /** Returns a map of metadata name, value pairs to be set. Values are copied. */
  public Map<String, Object> getEditedValues() {
    HashMap<String, Object> hashMap = new HashMap<>(editedValues);
    for (Entry<String, Object> entry : hashMap.entrySet()) {
      Object value = entry.getValue();
      if (value instanceof byte[]) {
        byte[] bytes = (byte[]) value;
        entry.setValue(Arrays.copyOf(bytes, bytes.length));
      }
    }
    return Collections.unmodifiableMap(hashMap);
  }

  private ContentMetadataMutations checkAndSet(String name, Object value) {
    editedValues.put(Assertions.checkNotNull(name), Assertions.checkNotNull(value));
    removedValues.remove(name);
    return this;
  }
}