public final class

PercentageRating

extends Rating

 java.lang.Object

androidx.media3.common.Rating

↳androidx.media3.common.PercentageRating

Gradle dependencies

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

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

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

Overview

A rating expressed as a percentage.

Summary

Fields
public static final Bundleable.Creator<PercentageRating>CREATOR

Object that can restore a PercentageRating from a .

from RatingRATING_UNSET
Constructors
publicPercentageRating()

Creates a unrated instance.

publicPercentageRating(float percent)

Creates a rated instance with the given percentage.

Methods
public booleanequals(java.lang.Object obj)

public floatgetPercent()

Returns the percent value of this rating.

public inthashCode()

public abstract booleanisRated()

Whether the rating exists or not.

public BundletoBundle()

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

Fields

public static final Bundleable.Creator<PercentageRating> CREATOR

Object that can restore a PercentageRating from a .

Constructors

public PercentageRating()

Creates a unrated instance.

public PercentageRating(float percent)

Creates a rated instance with the given percentage.

Parameters:

percent: The percentage value of the rating.

Methods

public abstract boolean isRated()

Whether the rating exists or not.

public float getPercent()

Returns the percent value of this rating. Will be within the range [0f, 100f], or Rating.RATING_UNSET if unrated.

public int hashCode()

public boolean equals(java.lang.Object obj)

public Bundle toBundle()

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;

import static androidx.media3.common.util.Assertions.checkArgument;
import static java.lang.annotation.ElementType.TYPE_USE;

import android.os.Bundle;
import androidx.annotation.FloatRange;
import androidx.annotation.IntDef;
import androidx.annotation.Nullable;
import androidx.media3.common.util.UnstableApi;
import com.google.common.base.Objects;
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/** A rating expressed as a percentage. */
public final class PercentageRating extends Rating {

  private final float percent;

  /** Creates a unrated instance. */
  public PercentageRating() {
    percent = RATING_UNSET;
  }

  /**
   * Creates a rated instance with the given percentage.
   *
   * @param percent The percentage value of the rating.
   */
  public PercentageRating(@FloatRange(from = 0, to = 100) float percent) {
    checkArgument(percent >= 0.0f && percent <= 100.0f, "percent must be in the range of [0, 100]");
    this.percent = percent;
  }

  @Override
  public boolean isRated() {
    return percent != RATING_UNSET;
  }

  /**
   * Returns the percent value of this rating. Will be within the range {@code [0f, 100f]}, or
   * {@link #RATING_UNSET} if unrated.
   */
  public float getPercent() {
    return percent;
  }

  @Override
  public int hashCode() {
    return Objects.hashCode(percent);
  }

  @Override
  public boolean equals(@Nullable Object obj) {
    if (!(obj instanceof PercentageRating)) {
      return false;
    }
    return percent == ((PercentageRating) obj).percent;
  }

  // Bundleable implementation.

  private static final @RatingType int TYPE = RATING_TYPE_PERCENTAGE;

  @Documented
  @Retention(RetentionPolicy.SOURCE)
  @Target(TYPE_USE)
  @IntDef({FIELD_RATING_TYPE, FIELD_PERCENT})
  private @interface FieldNumber {}

  private static final int FIELD_PERCENT = 1;

  @UnstableApi
  @Override
  public Bundle toBundle() {
    Bundle bundle = new Bundle();
    bundle.putInt(keyForField(FIELD_RATING_TYPE), TYPE);
    bundle.putFloat(keyForField(FIELD_PERCENT), percent);
    return bundle;
  }

  /** Object that can restore a {@link PercentageRating} from a {@link Bundle}. */
  @UnstableApi public static final Creator<PercentageRating> CREATOR = PercentageRating::fromBundle;

  private static PercentageRating fromBundle(Bundle bundle) {
    checkArgument(
        bundle.getInt(keyForField(FIELD_RATING_TYPE), /* defaultValue= */ RATING_TYPE_UNSET)
            == TYPE);
    float percent = bundle.getFloat(keyForField(FIELD_PERCENT), /* defaultValue= */ RATING_UNSET);
    return percent == RATING_UNSET ? new PercentageRating() : new PercentageRating(percent);
  }

  private static String keyForField(@FieldNumber int field) {
    return Integer.toString(field, Character.MAX_RADIX);
  }
}