public final class

StarRating

extends Rating

 java.lang.Object

androidx.media3.common.Rating

↳androidx.media3.common.StarRating

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

A rating expressed as a fractional number of stars.

Summary

Constructors
publicStarRating(int maxStars)

Creates a unrated instance with maxStars.

publicStarRating(int maxStars, float starRating)

Creates a rated instance with maxStars and the given fractional number of stars.

Methods
public booleanequals(java.lang.Object obj)

public static StarRatingfromBundle(Bundle bundle)

Restores a StarRating from a .

public intgetMaxStars()

Returns the maximum number of stars.

public floatgetStarRating()

Returns the fractional number of stars of this rating.

public inthashCode()

public abstract booleanisRated()

Whether the rating exists or not.

public abstract BundletoBundle()

Returns a representing the information stored in this rating.

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

Constructors

public StarRating(int maxStars)

Creates a unrated instance with maxStars. If maxStars is not a positive integer, it will throw an java.lang.IllegalArgumentException.

Parameters:

maxStars: The maximum number of stars this rating can have.

public StarRating(int maxStars, float starRating)

Creates a rated instance with maxStars and the given fractional number of stars. Non-integer values may be used to represent an average rating value. If maxStars is not a positive integer or starRating is out of range, it will throw an java.lang.IllegalArgumentException.

Parameters:

maxStars: The maximum number of stars this rating can have.
starRating: A fractional number of stars of this rating from 0f to maxStars.

Methods

public abstract boolean isRated()

Whether the rating exists or not.

public int getMaxStars()

Returns the maximum number of stars. Must be a positive number.

public float getStarRating()

Returns the fractional number of stars of this rating. Will range from 0f to StarRating.maxStars, or Rating.RATING_UNSET if unrated.

public int hashCode()

public boolean equals(java.lang.Object obj)

public abstract Bundle toBundle()

Returns a representing the information stored in this rating.

public static StarRating fromBundle(Bundle bundle)

Restores a StarRating from a .

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 android.os.Bundle;
import androidx.annotation.FloatRange;
import androidx.annotation.IntRange;
import androidx.annotation.Nullable;
import androidx.media3.common.util.UnstableApi;
import androidx.media3.common.util.Util;
import com.google.common.base.Objects;

/** A rating expressed as a fractional number of stars. */
public final class StarRating extends Rating {

  @IntRange(from = 1)
  private final int maxStars;

  private final float starRating;

  /**
   * Creates a unrated instance with {@code maxStars}. If {@code maxStars} is not a positive
   * integer, it will throw an {@link IllegalArgumentException}.
   *
   * @param maxStars The maximum number of stars this rating can have.
   */
  public StarRating(@IntRange(from = 1) int maxStars) {
    checkArgument(maxStars > 0, "maxStars must be a positive integer");
    this.maxStars = maxStars;
    starRating = RATING_UNSET;
  }

  /**
   * Creates a rated instance with {@code maxStars} and the given fractional number of stars.
   * Non-integer values may be used to represent an average rating value. If {@code maxStars} is not
   * a positive integer or {@code starRating} is out of range, it will throw an {@link
   * IllegalArgumentException}.
   *
   * @param maxStars The maximum number of stars this rating can have.
   * @param starRating A fractional number of stars of this rating from {@code 0f} to {@code
   *     maxStars}.
   */
  public StarRating(@IntRange(from = 1) int maxStars, @FloatRange(from = 0.0) float starRating) {
    checkArgument(maxStars > 0, "maxStars must be a positive integer");
    checkArgument(
        starRating >= 0.0f && starRating <= maxStars, "starRating is out of range [0, maxStars]");
    this.maxStars = maxStars;
    this.starRating = starRating;
  }

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

  /** Returns the maximum number of stars. Must be a positive number. */
  @IntRange(from = 1)
  public int getMaxStars() {
    return maxStars;
  }

  /**
   * Returns the fractional number of stars of this rating. Will range from {@code 0f} to {@link
   * #maxStars}, or {@link #RATING_UNSET} if unrated.
   */
  public float getStarRating() {
    return starRating;
  }

  @Override
  public int hashCode() {
    return Objects.hashCode(maxStars, starRating);
  }

  @Override
  public boolean equals(@Nullable Object obj) {
    if (!(obj instanceof StarRating)) {
      return false;
    }
    StarRating other = (StarRating) obj;
    return maxStars == other.maxStars && starRating == other.starRating;
  }

  private static final @RatingType int TYPE = RATING_TYPE_STAR;
  private static final int MAX_STARS_DEFAULT = 5;

  private static final String FIELD_MAX_STARS = Util.intToStringMaxRadix(1);
  private static final String FIELD_STAR_RATING = Util.intToStringMaxRadix(2);

  @UnstableApi
  @Override
  public Bundle toBundle() {
    Bundle bundle = new Bundle();
    bundle.putInt(FIELD_RATING_TYPE, TYPE);
    bundle.putInt(FIELD_MAX_STARS, maxStars);
    bundle.putFloat(FIELD_STAR_RATING, starRating);
    return bundle;
  }

  /** Restores a {@code StarRating} from a {@link Bundle}. */
  @UnstableApi
  public static StarRating fromBundle(Bundle bundle) {
    checkArgument(bundle.getInt(FIELD_RATING_TYPE, /* defaultValue= */ RATING_TYPE_UNSET) == TYPE);
    int maxStars = bundle.getInt(FIELD_MAX_STARS, /* defaultValue= */ MAX_STARS_DEFAULT);
    float starRating = bundle.getFloat(FIELD_STAR_RATING, /* defaultValue= */ RATING_UNSET);
    return starRating == RATING_UNSET
        ? new StarRating(maxStars)
        : new StarRating(maxStars, starRating);
  }
}