mirror of
https://github.com/androidx/media.git
synced 2025-05-04 06:00:37 +08:00
Use getters in Rating subclasses rather than direct field access.
#minor-release PiperOrigin-RevId: 372368685
This commit is contained in:
parent
3be50b26a0
commit
c97956ea29
@ -32,9 +32,7 @@ import java.lang.annotation.RetentionPolicy;
|
|||||||
public final class HeartRating extends Rating {
|
public final class HeartRating extends Rating {
|
||||||
|
|
||||||
private final boolean rated;
|
private final boolean rated;
|
||||||
|
private final boolean isHeart;
|
||||||
/** Whether the rating is "heart". */
|
|
||||||
public final boolean isHeart;
|
|
||||||
|
|
||||||
/** Creates a unrated instance. */
|
/** Creates a unrated instance. */
|
||||||
public HeartRating() {
|
public HeartRating() {
|
||||||
@ -57,6 +55,11 @@ public final class HeartRating extends Rating {
|
|||||||
return rated;
|
return rated;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Returns whether the rating is "heart". */
|
||||||
|
public boolean isHeart() {
|
||||||
|
return isHeart;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int hashCode() {
|
public int hashCode() {
|
||||||
return Objects.hashCode(rated, isHeart);
|
return Objects.hashCode(rated, isHeart);
|
||||||
|
@ -29,11 +29,7 @@ import java.lang.annotation.RetentionPolicy;
|
|||||||
/** A rating expressed as a percentage. */
|
/** A rating expressed as a percentage. */
|
||||||
public final class PercentageRating extends Rating {
|
public final class PercentageRating extends Rating {
|
||||||
|
|
||||||
/**
|
private final float percent;
|
||||||
* The percent value of this rating. Will be within the range {@code [0f, 100f]}, or {@link
|
|
||||||
* #RATING_UNSET} if unrated.
|
|
||||||
*/
|
|
||||||
public final float percent;
|
|
||||||
|
|
||||||
/** Creates a unrated instance. */
|
/** Creates a unrated instance. */
|
||||||
public PercentageRating() {
|
public PercentageRating() {
|
||||||
@ -55,6 +51,14 @@ public final class PercentageRating extends Rating {
|
|||||||
return percent != RATING_UNSET;
|
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
|
@Override
|
||||||
public int hashCode() {
|
public int hashCode() {
|
||||||
return Objects.hashCode(percent);
|
return Objects.hashCode(percent);
|
||||||
|
@ -30,15 +30,10 @@ import java.lang.annotation.RetentionPolicy;
|
|||||||
/** A rating expressed as a fractional number of stars. */
|
/** A rating expressed as a fractional number of stars. */
|
||||||
public final class StarRating extends Rating {
|
public final class StarRating extends Rating {
|
||||||
|
|
||||||
/** The maximum number of stars. Must be a positive number. */
|
|
||||||
@IntRange(from = 1)
|
@IntRange(from = 1)
|
||||||
public final int maxStars;
|
private final int maxStars;
|
||||||
|
|
||||||
/**
|
private final float starRating;
|
||||||
* The fractional number of stars of this rating. Will range from {@code 0f} to {@link #maxStars},
|
|
||||||
* or {@link #RATING_UNSET} if unrated.
|
|
||||||
*/
|
|
||||||
public final float starRating;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a unrated instance with {@code maxStars}. If {@code maxStars} is not a positive
|
* Creates a unrated instance with {@code maxStars}. If {@code maxStars} is not a positive
|
||||||
@ -75,6 +70,20 @@ public final class StarRating extends Rating {
|
|||||||
return starRating != RATING_UNSET;
|
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
|
@Override
|
||||||
public int hashCode() {
|
public int hashCode() {
|
||||||
return Objects.hashCode(maxStars, starRating);
|
return Objects.hashCode(maxStars, starRating);
|
||||||
|
@ -29,9 +29,7 @@ import java.lang.annotation.RetentionPolicy;
|
|||||||
public final class ThumbRating extends Rating {
|
public final class ThumbRating extends Rating {
|
||||||
|
|
||||||
private final boolean rated;
|
private final boolean rated;
|
||||||
|
private final boolean isThumbsUp;
|
||||||
/** Whether the rating is "thumbs up". */
|
|
||||||
public final boolean isThumbsUp;
|
|
||||||
|
|
||||||
/** Creates a unrated instance. */
|
/** Creates a unrated instance. */
|
||||||
public ThumbRating() {
|
public ThumbRating() {
|
||||||
@ -54,6 +52,11 @@ public final class ThumbRating extends Rating {
|
|||||||
return rated;
|
return rated;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Returns whether the rating is "thumbs up". */
|
||||||
|
public boolean isThumbsUp() {
|
||||||
|
return isThumbsUp;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int hashCode() {
|
public int hashCode() {
|
||||||
return Objects.hashCode(rated, isThumbsUp);
|
return Objects.hashCode(rated, isThumbsUp);
|
||||||
|
@ -39,7 +39,7 @@ public class RatingTest {
|
|||||||
boolean hasHeart = true;
|
boolean hasHeart = true;
|
||||||
HeartRating rating = new HeartRating(hasHeart);
|
HeartRating rating = new HeartRating(hasHeart);
|
||||||
assertThat(rating.isRated()).isTrue();
|
assertThat(rating.isRated()).isTrue();
|
||||||
assertThat(rating.isHeart).isEqualTo(hasHeart);
|
assertThat(rating.isHeart()).isEqualTo(hasHeart);
|
||||||
assertThat(roundTripViaBundle(rating)).isEqualTo(rating);
|
assertThat(roundTripViaBundle(rating)).isEqualTo(rating);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -55,7 +55,7 @@ public class RatingTest {
|
|||||||
float percentage = 20.5f;
|
float percentage = 20.5f;
|
||||||
PercentageRating rating = new PercentageRating(percentage);
|
PercentageRating rating = new PercentageRating(percentage);
|
||||||
assertThat(rating.isRated()).isTrue();
|
assertThat(rating.isRated()).isTrue();
|
||||||
assertThat(rating.percent).isEqualTo(percentage);
|
assertThat(rating.getPercent()).isEqualTo(percentage);
|
||||||
assertThat(roundTripViaBundle(rating)).isEqualTo(rating);
|
assertThat(roundTripViaBundle(rating)).isEqualTo(rating);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -71,7 +71,7 @@ public class RatingTest {
|
|||||||
boolean isThumbUp = true;
|
boolean isThumbUp = true;
|
||||||
ThumbRating rating = new ThumbRating(isThumbUp);
|
ThumbRating rating = new ThumbRating(isThumbUp);
|
||||||
assertThat(rating.isRated()).isTrue();
|
assertThat(rating.isRated()).isTrue();
|
||||||
assertThat(rating.isThumbsUp).isEqualTo(isThumbUp);
|
assertThat(rating.isThumbsUp()).isEqualTo(isThumbUp);
|
||||||
assertThat(roundTripViaBundle(rating)).isEqualTo(rating);
|
assertThat(roundTripViaBundle(rating)).isEqualTo(rating);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -80,7 +80,7 @@ public class RatingTest {
|
|||||||
int maxStars = 5;
|
int maxStars = 5;
|
||||||
StarRating rating = new StarRating(maxStars);
|
StarRating rating = new StarRating(maxStars);
|
||||||
assertThat(rating.isRated()).isFalse();
|
assertThat(rating.isRated()).isFalse();
|
||||||
assertThat(rating.maxStars).isEqualTo(maxStars);
|
assertThat(rating.getMaxStars()).isEqualTo(maxStars);
|
||||||
assertThat(roundTripViaBundle(rating)).isEqualTo(rating);
|
assertThat(roundTripViaBundle(rating)).isEqualTo(rating);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -90,8 +90,8 @@ public class RatingTest {
|
|||||||
float starRating = 3.1f;
|
float starRating = 3.1f;
|
||||||
StarRating rating = new StarRating(maxStars, starRating);
|
StarRating rating = new StarRating(maxStars, starRating);
|
||||||
assertThat(rating.isRated()).isTrue();
|
assertThat(rating.isRated()).isTrue();
|
||||||
assertThat(rating.maxStars).isEqualTo(maxStars);
|
assertThat(rating.getMaxStars()).isEqualTo(maxStars);
|
||||||
assertThat(rating.starRating).isEqualTo(starRating);
|
assertThat(rating.getStarRating()).isEqualTo(starRating);
|
||||||
assertThat(roundTripViaBundle(rating)).isEqualTo(rating);
|
assertThat(roundTripViaBundle(rating)).isEqualTo(rating);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user