mirror of
https://github.com/apple/pkl.git
synced 2026-03-21 00:29:07 +01:00
Leverage basic Java 17 features (#451)
- Refactor code to use the following basic Java 17 features: - pattern matching for instanceof - @Serial annotation - switch expressions - enhanced switch statements - StringBuilder.isEmpty() - Replace two switch statements with simpler if statements. - Rename a few local variables.
This commit is contained in:
@@ -38,11 +38,11 @@ abstract class AbstractConfig implements Config {
|
||||
public Config get(String propertyName) {
|
||||
var childValue = getRawChildValue(propertyName);
|
||||
var childName = qualifiedName.isEmpty() ? propertyName : qualifiedName + '.' + propertyName;
|
||||
if (childValue instanceof Composite) {
|
||||
return new CompositeConfig(childName, mapper, (Composite) childValue);
|
||||
if (childValue instanceof Composite composite) {
|
||||
return new CompositeConfig(childName, mapper, composite);
|
||||
}
|
||||
if (childValue instanceof Map) {
|
||||
return new MapConfig(childName, mapper, (Map<?, ?>) childValue);
|
||||
if (childValue instanceof Map<?, ?> map) {
|
||||
return new MapConfig(childName, mapper, map);
|
||||
}
|
||||
return new LeafConfig(childName, mapper, childValue);
|
||||
}
|
||||
|
||||
@@ -49,10 +49,10 @@ public class JavaType<T> {
|
||||
|
||||
protected JavaType() {
|
||||
var superclass = getClass().getGenericSuperclass();
|
||||
if (superclass instanceof Class) {
|
||||
if (!(superclass instanceof ParameterizedType parameterizedType)) {
|
||||
throw new IllegalStateException("JavaType token must be parameterized.");
|
||||
}
|
||||
type = ((ParameterizedType) superclass).getActualTypeArguments()[0];
|
||||
type = parameterizedType.getActualTypeArguments()[0];
|
||||
}
|
||||
|
||||
private JavaType(Type type) {
|
||||
@@ -161,9 +161,7 @@ public class JavaType<T> {
|
||||
@Override
|
||||
public boolean equals(@Nullable Object obj) {
|
||||
if (this == obj) return true;
|
||||
if (!(obj instanceof JavaType)) return false;
|
||||
|
||||
var other = (JavaType<?>) obj;
|
||||
if (!(obj instanceof JavaType<?> other)) return false;
|
||||
return type.equals(other.type);
|
||||
}
|
||||
|
||||
|
||||
@@ -23,7 +23,7 @@ final class PNullToAny implements ConverterFactory {
|
||||
@Override
|
||||
public Optional<Converter<?, ?>> create(PClassInfo<?> sourceType, Type targetType) {
|
||||
if (sourceType != PClassInfo.Null
|
||||
|| (targetType instanceof Class && ((Class<?>) targetType).isPrimitive())) {
|
||||
|| (targetType instanceof Class<?> clazz && clazz.isPrimitive())) {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
|
||||
@@ -46,8 +46,7 @@ public final class Reflection {
|
||||
}
|
||||
|
||||
public static boolean isMissingTypeArguments(Type type) {
|
||||
if (type instanceof WildcardType) {
|
||||
var wildcardType = (WildcardType) type;
|
||||
if (type instanceof WildcardType wildcardType) {
|
||||
var baseType =
|
||||
wildcardType.getLowerBounds().length > 0
|
||||
? wildcardType.getLowerBounds()[0]
|
||||
@@ -62,11 +61,10 @@ public final class Reflection {
|
||||
* instantiable (not an interface or abstract class).
|
||||
*/
|
||||
public static Type normalize(Type type) {
|
||||
if (type instanceof WildcardType) {
|
||||
var wcType = (WildcardType) type;
|
||||
var bounds = wcType.getLowerBounds();
|
||||
if (type instanceof WildcardType wildcardType) {
|
||||
var bounds = wildcardType.getLowerBounds();
|
||||
if (bounds.length > 0) return bounds[0];
|
||||
bounds = wcType.getUpperBounds();
|
||||
bounds = wildcardType.getUpperBounds();
|
||||
if (bounds.length > 0) return bounds[0];
|
||||
}
|
||||
return getExactSupertype(type, toRawType(type));
|
||||
@@ -154,8 +152,8 @@ public final class Reflection {
|
||||
* eliminate them.
|
||||
*/
|
||||
private static Type uncapture(Type type) {
|
||||
if (type instanceof CaptureType) {
|
||||
return ((CaptureType) type).getWildcardType();
|
||||
if (type instanceof CaptureType captureType) {
|
||||
return captureType.getWildcardType();
|
||||
}
|
||||
return type;
|
||||
}
|
||||
|
||||
@@ -35,10 +35,7 @@ final class Tuple2<S, T> {
|
||||
@Override
|
||||
public boolean equals(@Nullable Object obj) {
|
||||
if (this == obj) return true;
|
||||
if (!(obj instanceof Tuple2)) return false;
|
||||
|
||||
var other = (Tuple2<?, ?>) obj;
|
||||
|
||||
if (!(obj instanceof Tuple2<?, ?> other)) return false;
|
||||
return first.equals(other.first) && second.equals(other.second);
|
||||
}
|
||||
|
||||
|
||||
@@ -59,9 +59,7 @@ public final class TypeMapping<S, T extends S> {
|
||||
@Override
|
||||
public boolean equals(@Nullable Object obj) {
|
||||
if (this == obj) return true;
|
||||
if (!(obj instanceof TypeMapping)) return false;
|
||||
|
||||
var other = (TypeMapping<?, ?>) obj;
|
||||
if (!(obj instanceof TypeMapping<?, ?> other)) return false;
|
||||
return requestedType == other.requestedType && implementationType == other.implementationType;
|
||||
}
|
||||
|
||||
|
||||
@@ -44,8 +44,7 @@ public final class Types {
|
||||
typeParamsCount, rawType.getTypeName(), typeArguments.length));
|
||||
}
|
||||
for (Type arg : typeArguments) {
|
||||
if (arg instanceof Class) {
|
||||
var clazz = (Class<?>) arg;
|
||||
if (arg instanceof Class<?> clazz) {
|
||||
if (clazz.isPrimitive()) {
|
||||
throw new IllegalArgumentException(
|
||||
String.format(
|
||||
|
||||
@@ -64,8 +64,7 @@ public final class ValueMapperBuilder {
|
||||
@SuppressWarnings({"unchecked", "rawtypes"})
|
||||
public ValueMapperBuilder addConversion(Conversion<?, ?> conversion) {
|
||||
conversions.add(conversion);
|
||||
if (conversion.targetType instanceof Class) {
|
||||
var clazz = (Class<?>) conversion.targetType;
|
||||
if (conversion.targetType instanceof Class<?> clazz) {
|
||||
if (clazz.isPrimitive()) {
|
||||
conversions.add(
|
||||
Conversion.of(
|
||||
|
||||
@@ -91,9 +91,7 @@ public class PObjectToDataObjectJavaxInjectTest {
|
||||
@Override
|
||||
public boolean equals(@Nullable Object obj) {
|
||||
if (this == obj) return true;
|
||||
if (!(obj instanceof Person)) return false;
|
||||
|
||||
var other = (Person) obj;
|
||||
if (!(obj instanceof Person other)) return false;
|
||||
return name.equals(other.name)
|
||||
&& age == other.age
|
||||
&& hobbies.equals(other.hobbies)
|
||||
@@ -118,9 +116,7 @@ public class PObjectToDataObjectJavaxInjectTest {
|
||||
@Override
|
||||
public boolean equals(@Nullable Object obj) {
|
||||
if (this == obj) return true;
|
||||
if (!(obj instanceof Address)) return false;
|
||||
|
||||
var other = (Address) obj;
|
||||
if (!(obj instanceof Address other)) return false;
|
||||
return street.equals(other.street) && zip == other.zip;
|
||||
}
|
||||
|
||||
@@ -148,9 +144,7 @@ public class PObjectToDataObjectJavaxInjectTest {
|
||||
@Override
|
||||
public boolean equals(@Nullable Object obj) {
|
||||
if (this == obj) return true;
|
||||
if (!(obj instanceof Pair)) return false;
|
||||
|
||||
var other = (Pair<?, ?>) obj;
|
||||
if (!(obj instanceof Pair<?, ?> other)) return false;
|
||||
return first.equals(other.first) && second.equals(other.second);
|
||||
}
|
||||
|
||||
|
||||
@@ -136,9 +136,7 @@ public class PObjectToDataObjectTest {
|
||||
@Override
|
||||
public boolean equals(@Nullable Object obj) {
|
||||
if (this == obj) return true;
|
||||
if (!(obj instanceof Person)) return false;
|
||||
|
||||
var other = (Person) obj;
|
||||
if (!(obj instanceof Person other)) return false;
|
||||
return name.equals(other.name)
|
||||
&& age == other.age
|
||||
&& hobbies.equals(other.hobbies)
|
||||
@@ -168,9 +166,7 @@ public class PObjectToDataObjectTest {
|
||||
@Override
|
||||
public boolean equals(@Nullable Object obj) {
|
||||
if (this == obj) return true;
|
||||
if (!(obj instanceof PersonConstructoProperties)) return false;
|
||||
|
||||
var other = (PersonConstructoProperties) obj;
|
||||
if (!(obj instanceof PersonConstructoProperties other)) return false;
|
||||
return name.equals(other.name)
|
||||
&& age == other.age
|
||||
&& hobbies.equals(other.hobbies)
|
||||
@@ -195,9 +191,7 @@ public class PObjectToDataObjectTest {
|
||||
@Override
|
||||
public boolean equals(@Nullable Object obj) {
|
||||
if (this == obj) return true;
|
||||
if (!(obj instanceof Address)) return false;
|
||||
|
||||
var other = (Address) obj;
|
||||
if (!(obj instanceof Address other)) return false;
|
||||
return street.equals(other.street) && zip == other.zip;
|
||||
}
|
||||
|
||||
@@ -225,9 +219,7 @@ public class PObjectToDataObjectTest {
|
||||
@Override
|
||||
public boolean equals(@Nullable Object obj) {
|
||||
if (this == obj) return true;
|
||||
if (!(obj instanceof Pair)) return false;
|
||||
|
||||
var other = (Pair<?, ?>) obj;
|
||||
if (!(obj instanceof Pair<?, ?> other)) return false;
|
||||
return first.equals(other.first) && second.equals(other.second);
|
||||
}
|
||||
|
||||
|
||||
@@ -29,9 +29,7 @@ public class Person {
|
||||
@Override
|
||||
public boolean equals(@Nullable Object obj) {
|
||||
if (this == obj) return true;
|
||||
if (!(obj instanceof Person)) return false;
|
||||
|
||||
var other = (Person) obj;
|
||||
if (!(obj instanceof Person other)) return false;
|
||||
return name.equals(other.name) && age == other.age;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user