public final class

StarRating

extends java.lang.Object

implements Rating

 java.lang.Object

↳androidx.media2.session.StarRating

Gradle dependencies

compile group: 'androidx.media2', name: 'media2-session', version: '1.3.0'

  • groupId: androidx.media2
  • artifactId: media2-session
  • version: 1.3.0

Artifact androidx.media2:media2-session:1.3.0 it located at Google repository (https://maven.google.com/)

Overview

A class for rating expressed as the number of stars.

Summary

Constructors
publicStarRating(int maxStars)

Creates a unrated StarRating instance with maxStars.

publicStarRating(int maxStars, float starRating)

Creates a StarRating instance with maxStars and the given integer or fractional number of stars.

Methods
public booleanequals(java.lang.Object obj)

public intgetMaxStars()

Returns the max stars.

public floatgetStarRating()

Returns the star-based rating value.

public inthashCode()

public booleanisRated()

public java.lang.StringtoString()

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

Constructors

public StarRating(int maxStars)

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

Parameters:

maxStars: a range of this star rating from 0.0f to maxStars

public StarRating(int maxStars, float starRating)

Creates a StarRating instance with maxStars and the given integer or fractional number of stars. Non integer values can for instance be used to represent an average rating value, which might not be an integer number of stars. If maxStars is not a positive integer or starRating has invalid value, it will throw IllegalArgumentException.

Parameters:

maxStars: the maximum number of stars which this rating can have.
starRating: a number ranging from 0.0f to maxStars

Methods

public boolean isRated()

public int hashCode()

public boolean equals(java.lang.Object obj)

public java.lang.String toString()

public int getMaxStars()

Returns the max stars.

Returns:

a max number of stars for this star rating.

public float getStarRating()

Returns the star-based rating value.

Returns:

a rating value greater or equal to 0.0f, or a negative value if it is unrated.

Source

/*
 * Copyright 2019 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.media2.session;

import androidx.annotation.IntRange;
import androidx.core.util.ObjectsCompat;
import androidx.media2.common.Rating;
import androidx.versionedparcelable.ParcelField;
import androidx.versionedparcelable.VersionedParcelize;

/**
 * A class for rating expressed as the number of stars.
 *
 * @deprecated androidx.media2 is deprecated. Please migrate to <a
 *     href="https://developer.android.com/guide/topics/media/media3">androidx.media3</a>.
 */
@Deprecated
@VersionedParcelize
public final class StarRating implements Rating {
    private static final float RATING_NOT_RATED = -1.0f;

    @ParcelField(1)
    int mMaxStars;

    @ParcelField(2)
    float mStarRating;

    // WARNING: Adding a new ParcelField may break old library users (b/152830728)

    /*
     * Used for VersionedParcelable
     */
    StarRating() {
    }

    /**
     * Creates a unrated StarRating instance with {@code maxStars}.
     * If {@code maxStars} is not a positive integer, it will throw IllegalArgumentException.
     *
     * @param maxStars a range of this star rating from 0.0f to {@code maxStars}
     */
    public StarRating(@IntRange(from = 1) int maxStars) {
        if (maxStars <= 0) {
            throw new IllegalArgumentException("maxStars should be a positive integer");
        }
        mMaxStars = maxStars;
        mStarRating = RATING_NOT_RATED;
    }

    /**
     * Creates a StarRating instance with {@code maxStars} and the given integer or fractional
     * number of stars. Non integer values can for instance be used to represent an average rating
     * value, which might not be an integer number of stars.
     * If {@code maxStars} is not a positive integer or {@code starRating} has invalid value,
     * it will throw IllegalArgumentException.
     *
     * @param maxStars the maximum number of stars which this rating can have.
     * @param starRating a number ranging from 0.0f to {@code maxStars}
     */
    public StarRating(@IntRange(from = 1) int maxStars, float starRating) {
        if (maxStars <= 0) {
            throw new IllegalArgumentException("maxStars should be a positive integer");
        } else if (starRating < 0.0f || starRating > maxStars) {
            throw new IllegalArgumentException("starRating is out of range [0, maxStars]");
        }
        mMaxStars = maxStars;
        mStarRating = starRating;
    }

    @Override
    public boolean isRated() {
        return mStarRating >= 0.0f;
    }

    @Override
    public int hashCode() {
        return ObjectsCompat.hash(mMaxStars, mStarRating);
    }

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

    @Override
    public String toString() {
        return "StarRating: maxStars=" + mMaxStars
                + (isRated() ? ", starRating=" + mStarRating : ", unrated");
    }

    /**
     * Returns the max stars.
     *
     * @return a max number of stars for this star rating.
     */
    public int getMaxStars() {
        return mMaxStars;
    }

    /**
     * Returns the star-based rating value.
     *
     * @return a rating value greater or equal to 0.0f, or a negative value if it is unrated.
     */
    public float getStarRating() {
        return mStarRating;
    }
}