Migrate code to Java 17 style.

Use var instead of explicit local types where applicable. Use pattern variable instead instanceof and cast. Prefer loops and nullable types over Stream and Optional. Convert classes to records where applicable.

See #2465
This commit is contained in:
Mark Paluch
2021-10-05 15:21:12 +02:00
committed by Jens Schauder
parent d4036ec0a9
commit c735b58607
463 changed files with 3670 additions and 4014 deletions

View File

@@ -97,7 +97,7 @@ public class Box implements Shape {
@Override
public int hashCode() {
int result = 31;
var result = 31;
result += 17 * first.hashCode();
result += 17 * second.hashCode();
@@ -116,12 +116,10 @@ public class Box implements Shape {
return true;
}
if (!(obj instanceof Box)) {
if (!(obj instanceof Box that)) {
return false;
}
Box that = (Box) obj;
return this.first.equals(that.first) && this.second.equals(that.second);
}
}

View File

@@ -102,12 +102,10 @@ public class Circle implements Shape {
return true;
}
if (!(o instanceof Circle)) {
if (!(o instanceof Circle circle)) {
return false;
}
Circle circle = (Circle) o;
if (!ObjectUtils.nullSafeEquals(center, circle.center)) {
return false;
}
@@ -121,7 +119,7 @@ public class Circle implements Shape {
*/
@Override
public int hashCode() {
int result = ObjectUtils.nullSafeHashCode(center);
var result = ObjectUtils.nullSafeHashCode(center);
result = 31 * result + ObjectUtils.nullSafeHashCode(radius);
return result;
}

View File

@@ -121,7 +121,7 @@ public final class Distance implements Serializable, Comparable<Distance> {
Assert.notNull(other, "Distance to add must not be null!");
double newNormalizedValue = getNormalizedValue() + other.getNormalizedValue();
var newNormalizedValue = getNormalizedValue() + other.getNormalizedValue();
return new Distance(newNormalizedValue * metric.getMultiplier(), metric);
}
@@ -138,8 +138,8 @@ public final class Distance implements Serializable, Comparable<Distance> {
Assert.notNull(other, "Distance to must not be null!");
Assert.notNull(metric, "Result metric must not be null!");
double newLeft = getNormalizedValue() * metric.getMultiplier();
double newRight = other.getNormalizedValue() * metric.getMultiplier();
var newLeft = getNormalizedValue() * metric.getMultiplier();
var newRight = other.getNormalizedValue() * metric.getMultiplier();
return new Distance(newLeft + newRight, metric);
}
@@ -169,7 +169,7 @@ public final class Distance implements Serializable, Comparable<Distance> {
return 1;
}
double difference = this.getNormalizedValue() - that.getNormalizedValue();
var difference = this.getNormalizedValue() - that.getNormalizedValue();
return difference == 0 ? 0 : difference > 0 ? 1 : -1;
}
@@ -181,7 +181,7 @@ public final class Distance implements Serializable, Comparable<Distance> {
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
var builder = new StringBuilder();
builder.append(value);
if (metric != Metrics.NEUTRAL) {
@@ -210,12 +210,10 @@ public final class Distance implements Serializable, Comparable<Distance> {
return true;
}
if (!(o instanceof Distance)) {
if (!(o instanceof Distance distance)) {
return false;
}
Distance distance = (Distance) o;
if (value != distance.value) {
return false;
}

View File

@@ -75,12 +75,10 @@ public class GeoPage<T> extends PageImpl<GeoResult<T>> {
return true;
}
if (!(obj instanceof GeoPage)) {
if (!(obj instanceof GeoPage<?> that)) {
return false;
}
GeoPage<?> that = (GeoPage<?>) obj;
return super.equals(obj) && ObjectUtils.nullSafeEquals(this.averageDistance, that.averageDistance);
}

View File

@@ -62,12 +62,10 @@ public final class GeoResult<T> implements Serializable {
return true;
}
if (!(o instanceof GeoResult)) {
if (!(o instanceof GeoResult<?> geoResult)) {
return false;
}
GeoResult<?> geoResult = (GeoResult<?>) o;
if (!ObjectUtils.nullSafeEquals(content, geoResult.content)) {
return false;
}
@@ -81,7 +79,7 @@ public final class GeoResult<T> implements Serializable {
*/
@Override
public int hashCode() {
int result = ObjectUtils.nullSafeHashCode(content);
var result = ObjectUtils.nullSafeHashCode(content);
result = 31 * result + ObjectUtils.nullSafeHashCode(distance);
return result;
}

View File

@@ -115,12 +115,10 @@ public class GeoResults<T> implements Iterable<GeoResult<T>>, Serializable {
return true;
}
if (!(o instanceof GeoResults)) {
if (!(o instanceof GeoResults<?> that)) {
return false;
}
GeoResults<?> that = (GeoResults<?>) o;
if (!ObjectUtils.nullSafeEquals(results, that.results)) {
return false;
}
@@ -134,7 +132,7 @@ public class GeoResults<T> implements Iterable<GeoResult<T>>, Serializable {
*/
@Override
public int hashCode() {
int result = ObjectUtils.nullSafeHashCode(results);
var result = ObjectUtils.nullSafeHashCode(results);
result = 31 * result + ObjectUtils.nullSafeHashCode(averageDistance);
return result;
}
@@ -158,7 +156,7 @@ public class GeoResults<T> implements Iterable<GeoResult<T>>, Serializable {
return new Distance(0, metric);
}
double averageDistance = results.stream()//
var averageDistance = results.stream()//
.mapToDouble(it -> it.getDistance().getValue())//
.average().orElse(0);

View File

@@ -87,9 +87,9 @@ public class Point implements Serializable {
@Override
public int hashCode() {
int result = 1;
var result = 1;
long temp = Double.doubleToLongBits(x);
var temp = Double.doubleToLongBits(x);
result = 31 * result + (int) (temp ^ temp >>> 32);
temp = Double.doubleToLongBits(y);
@@ -109,12 +109,10 @@ public class Point implements Serializable {
return true;
}
if (!(obj instanceof Point)) {
if (!(obj instanceof Point other)) {
return false;
}
Point other = (Point) obj;
if (Double.doubleToLongBits(x) != Double.doubleToLongBits(other.x)) {
return false;
}

View File

@@ -111,11 +111,10 @@ public class Polygon implements Iterable<Point>, Shape {
return true;
}
if (!(o instanceof Polygon)) {
if (!(o instanceof Polygon that)) {
return false;
}
Polygon that = (Polygon) o;
return ObjectUtils.nullSafeEquals(points, that.points);
}

View File

@@ -94,7 +94,7 @@ public enum DistanceFormatter implements Converter<String, Distance>, Formatter<
*/
private static Distance doConvert(String source) {
for (Entry<String, Metric> metric : SUPPORTED_METRICS.entrySet()) {
for (var metric : SUPPORTED_METRICS.entrySet()) {
if (source.endsWith(metric.getKey())) {
return fromString(source, metric);
}
@@ -117,7 +117,7 @@ public enum DistanceFormatter implements Converter<String, Distance>, Formatter<
*/
private static Distance fromString(String source, Entry<String, Metric> metric) {
String amountString = source.substring(0, source.indexOf(metric.getKey()));
var amountString = source.substring(0, source.indexOf(metric.getKey()));
try {
return new Distance(Double.parseDouble(amountString), metric.getValue());

View File

@@ -45,7 +45,7 @@ public enum PointFormatter implements Converter<String, Point>, Formatter<Point>
@Override
public Point convert(String source) {
String[] parts = source.split(",");
var parts = source.split(",");
if (parts.length != 2) {
throw new IllegalArgumentException(String.format(INVALID_FORMAT, source));
@@ -53,8 +53,8 @@ public enum PointFormatter implements Converter<String, Point>, Formatter<Point>
try {
double latitude = Double.parseDouble(parts[0]);
double longitude = Double.parseDouble(parts[1]);
var latitude = Double.parseDouble(parts[0]);
var longitude = Double.parseDouble(parts[1]);
return new Point(longitude, latitude);