Cleanup: use Java17 switch statements.

AndroidStudio code cleanup.
This commit is contained in:
Dennis Guse
2023-04-13 14:52:20 +02:00
parent 3a8e547e10
commit efddb31391
30 changed files with 355 additions and 640 deletions
@@ -58,16 +58,11 @@ public class Distance {
}
public static Distance one(UnitSystem unitSystem) {
switch (unitSystem) {
case METRIC:
return Distance.ofKilometer(1);
case IMPERIAL:
return Distance.ofMile(1);
case NAUTICAL_IMPERIAL:
return Distance.ofNauticalMile(1);
default:
throw new RuntimeException("Not implemented");
}
return switch (unitSystem) {
case METRIC -> Distance.ofKilometer(1);
case IMPERIAL -> Distance.ofMile(1);
case NAUTICAL_IMPERIAL -> Distance.ofNauticalMile(1);
};
}
private final double distance_m;
@@ -133,28 +128,18 @@ public class Distance {
}
public double toKM_Miles(UnitSystem unitSystem) {
switch (unitSystem) {
case METRIC:
return toKM();
case IMPERIAL:
return toMI();
case NAUTICAL_IMPERIAL:
return toNauticalMiles();
default:
throw new RuntimeException("Not implemented");
}
return switch (unitSystem) {
case METRIC -> toKM();
case IMPERIAL -> toMI();
case NAUTICAL_IMPERIAL -> toNauticalMiles();
};
}
public double toM_FT(UnitSystem unitSystem) {
switch (unitSystem) {
case METRIC:
return toM();
case NAUTICAL_IMPERIAL:
case IMPERIAL:
return toFT();
default:
throw new RuntimeException("Not implemented");
}
return switch (unitSystem) {
case METRIC -> toM();
case NAUTICAL_IMPERIAL, IMPERIAL -> toFT();
};
}
@Override
@@ -46,38 +46,36 @@ public class DistanceFormatter {
public Pair<String, String> getDistanceParts(Distance distance) {
if (distance.isInvalid()) {
String valueUnknown = resources.getString(R.string.value_unknown);
switch (unitSystem) {
case METRIC:
return new Pair<>(valueUnknown, resources.getString(R.string.unit_meter));
case IMPERIAL:
case NAUTICAL_IMPERIAL:
return new Pair<>(valueUnknown, resources.getString(R.string.unit_feet));
default:
throw new RuntimeException("Not implemented");
}
return switch (unitSystem) {
case METRIC -> new Pair<>(valueUnknown, resources.getString(R.string.unit_meter));
case IMPERIAL, NAUTICAL_IMPERIAL ->
new Pair<>(valueUnknown, resources.getString(R.string.unit_feet));
};
}
switch (unitSystem) {
case METRIC:
case METRIC -> {
if (distance.greaterThan(Distance.ofKilometer(threshold))) {
return new Pair<>(StringUtils.formatDecimal(distance.toKM(), decimalCount), resources.getString(R.string.unit_kilometer));
} else {
return new Pair<>(StringUtils.formatDecimal(distance.toM(), decimalCount), resources.getString(R.string.unit_meter));
}
case IMPERIAL:
}
case IMPERIAL -> {
if (distance.greaterThan(Distance.ofMile(threshold))) {
return new Pair<>(StringUtils.formatDecimal(distance.toMI(), decimalCount), resources.getString(R.string.unit_mile));
} else {
return new Pair<>(StringUtils.formatDecimal(distance.toFT(), decimalCount), resources.getString(R.string.unit_feet));
}
case NAUTICAL_IMPERIAL:
}
case NAUTICAL_IMPERIAL -> {
if (distance.greaterThan(Distance.ofNauticalMile(threshold))) {
return new Pair<>(StringUtils.formatDecimal(distance.toNauticalMiles(), decimalCount), resources.getString(R.string.unit_nautical_mile));
} else {
return new Pair<>(StringUtils.formatDecimal(distance.toFT(), decimalCount), resources.getString(R.string.unit_feet));
}
default:
throw new RuntimeException("Not implemented");
}
default -> throw new RuntimeException("Not implemented");
}
}
@@ -115,16 +115,11 @@ public class Speed {
}
public double to(UnitSystem unitSystem) {
switch (unitSystem) {
case METRIC:
return toKMH();
case IMPERIAL:
return toMPH();
case NAUTICAL_IMPERIAL:
return toKnots();
default:
throw new RuntimeException("Not implemented");
}
return switch (unitSystem) {
case METRIC -> toKMH();
case IMPERIAL -> toMPH();
case NAUTICAL_IMPERIAL -> toKnots();
};
}
@Override
@@ -39,20 +39,14 @@ public class SpeedFormatter {
* @return the formatted speed (or null) and it's unit as {@link Pair}
*/
public Pair<String, String> getSpeedParts(Speed speed) {
int unitId;
switch (unitSystem) {
case METRIC:
unitId = reportSpeedOrPace ? R.string.unit_kilometer_per_hour : R.string.unit_minute_per_kilometer;
break;
case IMPERIAL:
unitId = reportSpeedOrPace ? R.string.unit_mile_per_hour : R.string.unit_minute_per_mile;
break;
case NAUTICAL_IMPERIAL:
unitId = reportSpeedOrPace ? R.string.unit_knots : R.string.unit_minute_per_nautical_mile;
break;
default:
throw new RuntimeException("Not implemented");
}
int unitId = switch (unitSystem) {
case METRIC ->
reportSpeedOrPace ? R.string.unit_kilometer_per_hour : R.string.unit_minute_per_kilometer;
case IMPERIAL ->
reportSpeedOrPace ? R.string.unit_mile_per_hour : R.string.unit_minute_per_mile;
case NAUTICAL_IMPERIAL ->
reportSpeedOrPace ? R.string.unit_knots : R.string.unit_minute_per_nautical_mile;
};
String unitString = resources.getString(unitId);