From bbeedd5e767d448b7cc6e06cd064620870e2cc6c Mon Sep 17 00:00:00 2001 From: samrobinson Date: Thu, 6 May 2021 18:11:08 +0100 Subject: [PATCH] Use getters in Rating subclasses rather than direct field access. #minor-release PiperOrigin-RevId: 372368685 --- .../android/exoplayer2/HeartRating.java | 9 +++++--- .../android/exoplayer2/PercentageRating.java | 14 +++++++---- .../google/android/exoplayer2/StarRating.java | 23 +++++++++++++------ .../android/exoplayer2/ThumbRating.java | 9 +++++--- .../google/android/exoplayer2/RatingTest.java | 12 +++++----- 5 files changed, 43 insertions(+), 24 deletions(-) diff --git a/library/common/src/main/java/com/google/android/exoplayer2/HeartRating.java b/library/common/src/main/java/com/google/android/exoplayer2/HeartRating.java index ff96ecd38c..b656d0bd35 100644 --- a/library/common/src/main/java/com/google/android/exoplayer2/HeartRating.java +++ b/library/common/src/main/java/com/google/android/exoplayer2/HeartRating.java @@ -32,9 +32,7 @@ import java.lang.annotation.RetentionPolicy; public final class HeartRating extends Rating { private final boolean rated; - - /** Whether the rating is "heart". */ - public final boolean isHeart; + private final boolean isHeart; /** Creates a unrated instance. */ public HeartRating() { @@ -57,6 +55,11 @@ public final class HeartRating extends Rating { return rated; } + /** Returns whether the rating is "heart". */ + public boolean isHeart() { + return isHeart; + } + @Override public int hashCode() { return Objects.hashCode(rated, isHeart); diff --git a/library/common/src/main/java/com/google/android/exoplayer2/PercentageRating.java b/library/common/src/main/java/com/google/android/exoplayer2/PercentageRating.java index 3e078a0b3a..1953e7e1d1 100644 --- a/library/common/src/main/java/com/google/android/exoplayer2/PercentageRating.java +++ b/library/common/src/main/java/com/google/android/exoplayer2/PercentageRating.java @@ -29,11 +29,7 @@ import java.lang.annotation.RetentionPolicy; /** A rating expressed as a percentage. */ public final class PercentageRating extends Rating { - /** - * The percent value of this rating. Will be within the range {@code [0f, 100f]}, or {@link - * #RATING_UNSET} if unrated. - */ - public final float percent; + private final float percent; /** Creates a unrated instance. */ public PercentageRating() { @@ -55,6 +51,14 @@ public final class PercentageRating extends Rating { 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); diff --git a/library/common/src/main/java/com/google/android/exoplayer2/StarRating.java b/library/common/src/main/java/com/google/android/exoplayer2/StarRating.java index 99e56a7d74..543228185e 100644 --- a/library/common/src/main/java/com/google/android/exoplayer2/StarRating.java +++ b/library/common/src/main/java/com/google/android/exoplayer2/StarRating.java @@ -30,15 +30,10 @@ import java.lang.annotation.RetentionPolicy; /** A rating expressed as a fractional number of stars. */ public final class StarRating extends Rating { - /** The maximum number of stars. Must be a positive number. */ @IntRange(from = 1) - public final int maxStars; + private final int maxStars; - /** - * 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; + private final float starRating; /** * 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; } + /** 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); diff --git a/library/common/src/main/java/com/google/android/exoplayer2/ThumbRating.java b/library/common/src/main/java/com/google/android/exoplayer2/ThumbRating.java index bd56959bae..07e0ee38d3 100644 --- a/library/common/src/main/java/com/google/android/exoplayer2/ThumbRating.java +++ b/library/common/src/main/java/com/google/android/exoplayer2/ThumbRating.java @@ -29,9 +29,7 @@ import java.lang.annotation.RetentionPolicy; public final class ThumbRating extends Rating { private final boolean rated; - - /** Whether the rating is "thumbs up". */ - public final boolean isThumbsUp; + private final boolean isThumbsUp; /** Creates a unrated instance. */ public ThumbRating() { @@ -54,6 +52,11 @@ public final class ThumbRating extends Rating { return rated; } + /** Returns whether the rating is "thumbs up". */ + public boolean isThumbsUp() { + return isThumbsUp; + } + @Override public int hashCode() { return Objects.hashCode(rated, isThumbsUp); diff --git a/library/common/src/test/java/com/google/android/exoplayer2/RatingTest.java b/library/common/src/test/java/com/google/android/exoplayer2/RatingTest.java index 4c2d5b78b6..be77acd83b 100644 --- a/library/common/src/test/java/com/google/android/exoplayer2/RatingTest.java +++ b/library/common/src/test/java/com/google/android/exoplayer2/RatingTest.java @@ -39,7 +39,7 @@ public class RatingTest { boolean hasHeart = true; HeartRating rating = new HeartRating(hasHeart); assertThat(rating.isRated()).isTrue(); - assertThat(rating.isHeart).isEqualTo(hasHeart); + assertThat(rating.isHeart()).isEqualTo(hasHeart); assertThat(roundTripViaBundle(rating)).isEqualTo(rating); } @@ -55,7 +55,7 @@ public class RatingTest { float percentage = 20.5f; PercentageRating rating = new PercentageRating(percentage); assertThat(rating.isRated()).isTrue(); - assertThat(rating.percent).isEqualTo(percentage); + assertThat(rating.getPercent()).isEqualTo(percentage); assertThat(roundTripViaBundle(rating)).isEqualTo(rating); } @@ -71,7 +71,7 @@ public class RatingTest { boolean isThumbUp = true; ThumbRating rating = new ThumbRating(isThumbUp); assertThat(rating.isRated()).isTrue(); - assertThat(rating.isThumbsUp).isEqualTo(isThumbUp); + assertThat(rating.isThumbsUp()).isEqualTo(isThumbUp); assertThat(roundTripViaBundle(rating)).isEqualTo(rating); } @@ -80,7 +80,7 @@ public class RatingTest { int maxStars = 5; StarRating rating = new StarRating(maxStars); assertThat(rating.isRated()).isFalse(); - assertThat(rating.maxStars).isEqualTo(maxStars); + assertThat(rating.getMaxStars()).isEqualTo(maxStars); assertThat(roundTripViaBundle(rating)).isEqualTo(rating); } @@ -90,8 +90,8 @@ public class RatingTest { float starRating = 3.1f; StarRating rating = new StarRating(maxStars, starRating); assertThat(rating.isRated()).isTrue(); - assertThat(rating.maxStars).isEqualTo(maxStars); - assertThat(rating.starRating).isEqualTo(starRating); + assertThat(rating.getMaxStars()).isEqualTo(maxStars); + assertThat(rating.getStarRating()).isEqualTo(starRating); assertThat(roundTripViaBundle(rating)).isEqualTo(rating); }