public final class

PercentageRating

extends java.lang.Object

implements Rating

 java.lang.Object

↳androidx.media2.session.PercentageRating

Gradle dependencies

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

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

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

Overview

A class for rating expressed as a percentage.

Summary

Constructors
publicPercentageRating()

Creates a unrated PercentageRating instance.

publicPercentageRating(float percent)

Creates a PercentageRating instance with the given percentage.

Methods
public booleanequals(java.lang.Object obj)

public floatgetPercentRating()

Returns the percentage-based rating value.

public inthashCode()

public booleanisRated()

public java.lang.StringtoString()

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

Constructors

public PercentageRating()

Creates a unrated PercentageRating instance.

public PercentageRating(float percent)

Creates a PercentageRating instance with the given percentage. If percent is less than 0f or greater than 100f, it will throw IllegalArgumentException.

Parameters:

percent: the value of the rating

Methods

public boolean isRated()

public int hashCode()

public boolean equals(java.lang.Object obj)

public java.lang.String toString()

public float getPercentRating()

Returns the percentage-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.core.util.ObjectsCompat;
import androidx.media2.common.Rating;
import androidx.versionedparcelable.ParcelField;
import androidx.versionedparcelable.VersionedParcelize;

/**
 * A class for rating expressed as a percentage.
 */
@VersionedParcelize
public final class PercentageRating implements Rating {
    private static final float RATING_NOT_RATED = -1.0f;

    @ParcelField(1)
    float mPercent;

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

    /**
     * Creates a unrated PercentageRating instance.
     */
    public PercentageRating() {
        mPercent = RATING_NOT_RATED;
    }

    /**
     * Creates a PercentageRating instance with the given percentage.
     * If {@code percent} is less than 0f or greater than 100f, it will throw
     * IllegalArgumentException.
     *
     * @param percent the value of the rating
     */
    public PercentageRating(float percent) {
        if (percent < 0.0f || percent > 100.0f) {
            throw new IllegalArgumentException("percent should be in the rage of [0, 100]");
        }
        mPercent = percent;
    }

    @Override
    public boolean isRated() {
        return mPercent != RATING_NOT_RATED;
    }

    @Override
    public int hashCode() {
        return ObjectsCompat.hash(mPercent);
    }

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

    @Override
    public String toString() {
        return "PercentageRating: " + (isRated() ? "percentage=" + mPercent : "unrated");
    }

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