refactor(math): replace min/max with clamp for improved readability (#1440)
This commit is contained in:
@@ -409,12 +409,12 @@ public class ReadingSessionService {
|
|||||||
.mapToDouble(Double::doubleValue).average().orElse(0);
|
.mapToDouble(Double::doubleValue).average().orElse(0);
|
||||||
boolean finishBurst = lastQuarterAvg > firstThreeQuartersAvg;
|
boolean finishBurst = lastQuarterAvg > firstThreeQuartersAvg;
|
||||||
|
|
||||||
double accelScore = Math.min(1.0, Math.max(0.0, (sessionAcceleration + 50) / 100.0));
|
double accelScore = Math.clamp((sessionAcceleration + 50) / 100.0, 0.0, 1.0);
|
||||||
double gapScore = Math.min(1.0, Math.max(0.0, (-gapReduction + 50) / 100.0));
|
double gapScore = Math.clamp((-gapReduction + 50) / 100.0, 0.0, 1.0);
|
||||||
double burstScore = finishBurst ? 1.0 : 0.0;
|
double burstScore = finishBurst ? 1.0 : 0.0;
|
||||||
|
|
||||||
int gripScore = (int) Math.round(
|
int gripScore = (int) Math.round(
|
||||||
Math.min(100, Math.max(0, accelScore * 35 + gapScore * 35 + burstScore * 30)));
|
Math.clamp(accelScore * 35 + gapScore * 35 + burstScore * 30, 0, 100));
|
||||||
|
|
||||||
double avgDuration = durations.stream().mapToDouble(Double::doubleValue).average().orElse(0);
|
double avgDuration = durations.stream().mapToDouble(Double::doubleValue).average().orElse(0);
|
||||||
|
|
||||||
|
|||||||
@@ -114,7 +114,7 @@ public class HardcoverSyncService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
progressPages = Math.round((progressPercent / 100.0f) * hardcoverBook.pages);
|
progressPages = Math.round((progressPercent / 100.0f) * hardcoverBook.pages);
|
||||||
progressPages = Math.max(0, Math.min(hardcoverBook.pages, progressPages));
|
progressPages = Math.clamp(progressPages, 0, hardcoverBook.pages);
|
||||||
|
|
||||||
boolean isFinished = progressPercent >= 99.0f;
|
boolean isFinished = progressPercent >= 99.0f;
|
||||||
|
|
||||||
|
|||||||
@@ -596,7 +596,7 @@ public class CoverImageGenerator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private static Color alpha(Color c, int a) {
|
private static Color alpha(Color c, int a) {
|
||||||
return new Color(c.getRed(), c.getGreen(), c.getBlue(), Math.min(255, Math.max(0, a)));
|
return new Color(c.getRed(), c.getGreen(), c.getBlue(), Math.clamp(a, 0, 255));
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Color darken(Color c, float f) {
|
private static Color darken(Color c, float f) {
|
||||||
|
|||||||
+1
-1
@@ -114,7 +114,7 @@ public class HardcoverBookSearchService {
|
|||||||
return Collections.emptyList();
|
return Collections.emptyList();
|
||||||
}
|
}
|
||||||
|
|
||||||
int sanitizedPerPage = Math.min(Math.max(perPage, 1), 100);
|
int sanitizedPerPage = Math.clamp(perPage, 1, 100);
|
||||||
|
|
||||||
GraphQLRequest body = new GraphQLRequest();
|
GraphQLRequest body = new GraphQLRequest();
|
||||||
body.setQuery("query BookSearch($q: String!, $limit: Int!) { search(query: $q, query_type: \"Book\", per_page: $limit, page: 1) { results } }");
|
body.setQuery("query BookSearch($q: String!, $limit: Int!) { search(query: $q, query_type: \"Book\", per_page: $limit, page: 1) { results } }");
|
||||||
|
|||||||
@@ -290,7 +290,7 @@ public class EpubCfiService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private Float clampPercent(float percent) {
|
private Float clampPercent(float percent) {
|
||||||
return Math.max(0f, Math.min(percent, 100f));
|
return Math.clamp(percent, 0f, 100f);
|
||||||
}
|
}
|
||||||
|
|
||||||
public record CfiLocation(String href, Float contentSourceProgressPercent) {
|
public record CfiLocation(String href, Float contentSourceProgressPercent) {
|
||||||
|
|||||||
Reference in New Issue
Block a user