Use getters in Rating subclasses rather than direct field access.

#minor-release

PiperOrigin-RevId: 372368685
This commit is contained in:
samrobinson 2021-05-06 18:11:08 +01:00 committed by bachinger
parent 1408fe0403
commit bbeedd5e76
5 changed files with 43 additions and 24 deletions

View File

@ -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);

View File

@ -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);

View File

@ -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);

View File

@ -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);

View File

@ -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);
}