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

@@ -118,8 +118,8 @@ public abstract class AbstractPageRequest implements Pageable, Serializable {
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
final var prime = 31;
var result = 1;
result = prime * result + page;
result = prime * result + size;
@@ -142,7 +142,7 @@ public abstract class AbstractPageRequest implements Pageable, Serializable {
return false;
}
AbstractPageRequest other = (AbstractPageRequest) obj;
var other = (AbstractPageRequest) obj;
return this.page == other.page && this.size == other.size;
}
}

View File

@@ -184,14 +184,12 @@ abstract class Chunk<T> implements Slice<T>, Serializable {
return true;
}
if (!(obj instanceof Chunk<?>)) {
if (!(obj instanceof Chunk<?> that)) {
return false;
}
Chunk<?> that = (Chunk<?>) obj;
boolean contentEqual = this.content.equals(that.content);
boolean pageableEqual = this.pageable.equals(that.pageable);
var contentEqual = this.content.equals(that.content);
var pageableEqual = this.pageable.equals(that.pageable);
return contentEqual && pageableEqual;
}
@@ -203,7 +201,7 @@ abstract class Chunk<T> implements Slice<T>, Serializable {
@Override
public int hashCode() {
int result = 17;
var result = 17;
result += 31 * pageable.hashCode();
result += 31 * content.hashCode();

View File

@@ -124,7 +124,7 @@ public interface ExampleMatcher {
Assert.hasText(propertyPath, "PropertyPath must not be empty!");
Assert.notNull(matcherConfigurer, "MatcherConfigurer must not be empty!");
GenericPropertyMatcher genericPropertyMatcher = new GenericPropertyMatcher();
var genericPropertyMatcher = new GenericPropertyMatcher();
matcherConfigurer.configureMatcher(genericPropertyMatcher);
return withMatcher(propertyPath, genericPropertyMatcher);
@@ -451,12 +451,10 @@ public interface ExampleMatcher {
return true;
}
if (!(o instanceof GenericPropertyMatcher)) {
if (!(o instanceof GenericPropertyMatcher that)) {
return false;
}
GenericPropertyMatcher that = (GenericPropertyMatcher) o;
if (stringMatcher != that.stringMatcher)
return false;
@@ -473,7 +471,7 @@ public interface ExampleMatcher {
*/
@Override
public int hashCode() {
int result = ObjectUtils.nullSafeHashCode(stringMatcher);
var result = ObjectUtils.nullSafeHashCode(stringMatcher);
result = 31 * result + ObjectUtils.nullSafeHashCode(ignoreCase);
result = 31 * result + ObjectUtils.nullSafeHashCode(valueTransformer);
return result;
@@ -751,12 +749,10 @@ public interface ExampleMatcher {
return true;
}
if (!(o instanceof PropertySpecifier)) {
if (!(o instanceof PropertySpecifier that)) {
return false;
}
PropertySpecifier that = (PropertySpecifier) o;
if (!ObjectUtils.nullSafeEquals(path, that.path)) {
return false;
}
@@ -777,7 +773,7 @@ public interface ExampleMatcher {
*/
@Override
public int hashCode() {
int result = ObjectUtils.nullSafeHashCode(path);
var result = ObjectUtils.nullSafeHashCode(path);
result = 31 * result + ObjectUtils.nullSafeHashCode(stringMatcher);
result = 31 * result + ObjectUtils.nullSafeHashCode(ignoreCase);
result = 31 * result + ObjectUtils.nullSafeHashCode(valueTransformer);
@@ -840,11 +836,10 @@ public interface ExampleMatcher {
return true;
}
if (!(o instanceof PropertySpecifiers)) {
if (!(o instanceof PropertySpecifiers that)) {
return false;
}
PropertySpecifiers that = (PropertySpecifiers) o;
return ObjectUtils.nullSafeEquals(propertySpecifiers, that.propertySpecifiers);
}

View File

@@ -113,8 +113,8 @@ public class PageImpl<T> extends Chunk<T> implements Page<T> {
@Override
public String toString() {
String contentType = "UNKNOWN";
List<T> content = getContent();
var contentType = "UNKNOWN";
var content = getContent();
if (!content.isEmpty() && content.get(0) != null) {
contentType = content.get(0).getClass().getName();
@@ -134,12 +134,10 @@ public class PageImpl<T> extends Chunk<T> implements Page<T> {
return true;
}
if (!(obj instanceof PageImpl<?>)) {
if (!(obj instanceof PageImpl<?> that)) {
return false;
}
PageImpl<?> that = (PageImpl<?>) obj;
return this.total == that.total && super.equals(obj);
}
@@ -150,7 +148,7 @@ public class PageImpl<T> extends Chunk<T> implements Page<T> {
@Override
public int hashCode() {
int result = 17;
var result = 17;
result += 31 * (int) (total ^ total >>> 32);
result += 31 * super.hashCode();

View File

@@ -142,12 +142,10 @@ public class PageRequest extends AbstractPageRequest {
return true;
}
if (!(obj instanceof PageRequest)) {
if (!(obj instanceof PageRequest that)) {
return false;
}
PageRequest that = (PageRequest) obj;
return super.equals(that) && this.sort.equals(that.sort);
}

View File

@@ -226,12 +226,10 @@ public final class Range<T extends Comparable<T>> {
return true;
}
if (!(o instanceof Range)) {
if (!(o instanceof Range<?> range)) {
return false;
}
Range<?> range = (Range<?>) o;
if (!ObjectUtils.nullSafeEquals(lowerBound, range.lowerBound)) {
return false;
}
@@ -245,7 +243,7 @@ public final class Range<T extends Comparable<T>> {
*/
@Override
public int hashCode() {
int result = ObjectUtils.nullSafeHashCode(lowerBound);
var result = ObjectUtils.nullSafeHashCode(lowerBound);
result = 31 * result + ObjectUtils.nullSafeHashCode(upperBound);
return result;
}
@@ -436,12 +434,10 @@ public final class Range<T extends Comparable<T>> {
return true;
}
if (!(o instanceof Bound)) {
if (!(o instanceof Bound<?> bound)) {
return false;
}
Bound<?> bound = (Bound<?>) o;
if (inclusive != bound.inclusive)
return false;
@@ -454,7 +450,7 @@ public final class Range<T extends Comparable<T>> {
*/
@Override
public int hashCode() {
int result = ObjectUtils.nullSafeHashCode(value);
var result = ObjectUtils.nullSafeHashCode(value);
result = 31 * result + (inclusive ? 1 : 0);
return result;
}

View File

@@ -83,8 +83,8 @@ public class SliceImpl<T> extends Chunk<T> {
@Override
public String toString() {
String contentType = "UNKNOWN";
List<T> content = getContent();
var contentType = "UNKNOWN";
var content = getContent();
if (content.size() > 0) {
contentType = content.get(0).getClass().getName();
@@ -104,12 +104,10 @@ public class SliceImpl<T> extends Chunk<T> {
return true;
}
if (!(obj instanceof SliceImpl<?>)) {
if (!(obj instanceof SliceImpl<?> that)) {
return false;
}
SliceImpl<?> that = (SliceImpl<?>) obj;
return this.hasNext == that.hasNext && super.equals(obj);
}
@@ -120,7 +118,7 @@ public class SliceImpl<T> extends Chunk<T> {
@Override
public int hashCode() {
int result = 17;
var result = 17;
result += 31 * (hasNext ? 1 : 0);
result += 31 * super.hashCode();

View File

@@ -193,9 +193,9 @@ public class Sort implements Streamable<org.springframework.data.domain.Sort.Ord
Assert.notNull(sort, "Sort must not be null!");
ArrayList<Order> these = new ArrayList<>(this.toList());
var these = new ArrayList<Order>(this.toList());
for (Order order : sort) {
for (var order : sort) {
these.add(order);
}
@@ -211,7 +211,7 @@ public class Sort implements Streamable<org.springframework.data.domain.Sort.Ord
@Nullable
public Order getOrderFor(String property) {
for (Order order : this) {
for (var order : this) {
if (order.getProperty().equals(property)) {
return order;
}
@@ -239,12 +239,10 @@ public class Sort implements Streamable<org.springframework.data.domain.Sort.Ord
return true;
}
if (!(obj instanceof Sort)) {
if (!(obj instanceof Sort that)) {
return false;
}
Sort that = (Sort) obj;
return toList().equals(that.toList());
}
@@ -255,7 +253,7 @@ public class Sort implements Streamable<org.springframework.data.domain.Sort.Ord
@Override
public int hashCode() {
int result = 17;
var result = 17;
result = 31 * result + orders.hashCode();
return result;
}
@@ -607,7 +605,7 @@ public class Sort implements Streamable<org.springframework.data.domain.Sort.Ord
@Override
public int hashCode() {
int result = 17;
var result = 17;
result = 31 * result + direction.hashCode();
result = 31 * result + property.hashCode();
@@ -628,12 +626,10 @@ public class Sort implements Streamable<org.springframework.data.domain.Sort.Ord
return true;
}
if (!(obj instanceof Order)) {
if (!(obj instanceof Order that)) {
return false;
}
Order that = (Order) obj;
return this.direction.equals(that.direction) && this.property.equals(that.property)
&& this.ignoreCase == that.ignoreCase && this.nullHandling.equals(that.nullHandling);
}
@@ -645,7 +641,7 @@ public class Sort implements Streamable<org.springframework.data.domain.Sort.Ord
@Override
public String toString() {
String result = String.format("%s: %s", property, direction);
var result = String.format("%s: %s", property, direction);
if (!NullHandling.NATIVE.equals(nullHandling)) {
result += ", " + nullHandling;

View File

@@ -57,11 +57,10 @@ class TypedExample<T> implements Example<T> {
return true;
}
if (!(o instanceof TypedExample)) {
if (!(o instanceof TypedExample<?> that)) {
return false;
}
TypedExample<?> that = (TypedExample<?>) o;
if (!ObjectUtils.nullSafeEquals(probe, that.probe)) {
return false;
}
@@ -75,7 +74,7 @@ class TypedExample<T> implements Example<T> {
*/
@Override
public int hashCode() {
int result = ObjectUtils.nullSafeHashCode(probe);
var result = ObjectUtils.nullSafeHashCode(probe);
result = 31 * result + ObjectUtils.nullSafeHashCode(matcher);
return result;
}

View File

@@ -105,8 +105,8 @@ class TypedExampleMatcher implements ExampleMatcher {
Assert.hasText(propertyPath, "PropertyPath must not be empty!");
Assert.notNull(genericPropertyMatcher, "GenericPropertyMatcher must not be empty!");
PropertySpecifiers propertySpecifiers = new PropertySpecifiers(this.propertySpecifiers);
PropertySpecifier propertySpecifier = new PropertySpecifier(propertyPath);
var propertySpecifiers = new PropertySpecifiers(this.propertySpecifiers);
var propertySpecifier = new PropertySpecifier(propertyPath);
if (genericPropertyMatcher.ignoreCase != null) {
propertySpecifier = propertySpecifier.withIgnoreCase(genericPropertyMatcher.ignoreCase);
@@ -134,8 +134,8 @@ class TypedExampleMatcher implements ExampleMatcher {
Assert.hasText(propertyPath, "PropertyPath must not be empty!");
Assert.notNull(propertyValueTransformer, "PropertyValueTransformer must not be empty!");
PropertySpecifiers propertySpecifiers = new PropertySpecifiers(this.propertySpecifiers);
PropertySpecifier propertySpecifier = getOrCreatePropertySpecifier(propertyPath, propertySpecifiers);
var propertySpecifiers = new PropertySpecifiers(this.propertySpecifiers);
var propertySpecifier = getOrCreatePropertySpecifier(propertyPath, propertySpecifiers);
propertySpecifiers.add(propertySpecifier.withValueTransformer(propertyValueTransformer));
@@ -153,10 +153,10 @@ class TypedExampleMatcher implements ExampleMatcher {
Assert.notEmpty(propertyPaths, "PropertyPaths must not be empty!");
Assert.noNullElements(propertyPaths, "PropertyPaths must not contain null elements!");
PropertySpecifiers propertySpecifiers = new PropertySpecifiers(this.propertySpecifiers);
var propertySpecifiers = new PropertySpecifiers(this.propertySpecifiers);
for (String propertyPath : propertyPaths) {
PropertySpecifier propertySpecifier = getOrCreatePropertySpecifier(propertyPath, propertySpecifiers);
for (var propertyPath : propertyPaths) {
var propertySpecifier = getOrCreatePropertySpecifier(propertyPath, propertySpecifiers);
propertySpecifiers.add(propertySpecifier.withIgnoreCase(true));
}
@@ -256,12 +256,10 @@ class TypedExampleMatcher implements ExampleMatcher {
return true;
}
if (!(o instanceof TypedExampleMatcher)) {
if (!(o instanceof TypedExampleMatcher that)) {
return false;
}
TypedExampleMatcher that = (TypedExampleMatcher) o;
if (defaultIgnoreCase != that.defaultIgnoreCase) {
return false;
}
@@ -292,7 +290,7 @@ class TypedExampleMatcher implements ExampleMatcher {
*/
@Override
public int hashCode() {
int result = ObjectUtils.nullSafeHashCode(nullHandler);
var result = ObjectUtils.nullSafeHashCode(nullHandler);
result = 31 * result + ObjectUtils.nullSafeHashCode(defaultStringMatcher);
result = 31 * result + ObjectUtils.nullSafeHashCode(propertySpecifiers);
result = 31 * result + ObjectUtils.nullSafeHashCode(ignoredPaths);

View File

@@ -17,7 +17,6 @@ package org.springframework.data.domain.jaxb;
import jakarta.xml.bind.annotation.adapters.XmlAdapter;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.domain.Sort.Order;
import org.springframework.data.domain.jaxb.SpringDataJaxb.OrderDto;
import org.springframework.lang.Nullable;
@@ -43,7 +42,7 @@ public class OrderAdapter extends XmlAdapter<OrderDto, Order> {
return null;
}
OrderDto dto = new OrderDto();
var dto = new OrderDto();
dto.direction = order.getDirection();
dto.property = order.getProperty();
return dto;
@@ -61,8 +60,8 @@ public class OrderAdapter extends XmlAdapter<OrderDto, Order> {
return null;
}
Direction direction = source.direction;
String property = source.property;
var direction = source.direction;
var property = source.property;
if (direction == null || property == null) {
return null;

View File

@@ -44,7 +44,7 @@ public class PageAdapter extends XmlAdapter<PageDto, Page<Object>> {
return null;
}
PageDto dto = new PageDto();
var dto = new PageDto();
dto.content = source.getContent();
dto.add(getLinks(source));

View File

@@ -46,9 +46,9 @@ class PageableAdapter extends XmlAdapter<PageRequestDto, Pageable> {
return null;
}
PageRequestDto dto = new PageRequestDto();
var dto = new PageRequestDto();
SortDto sortDto = SortAdapter.INSTANCE.marshal(request.getSort());
var sortDto = SortAdapter.INSTANCE.marshal(request.getSort());
dto.orders = sortDto == null ? Collections.emptyList() : sortDto.orders;
dto.page = request.getPageNumber();
dto.size = request.getPageSize();
@@ -72,7 +72,7 @@ class PageableAdapter extends XmlAdapter<PageRequestDto, Pageable> {
return PageRequest.of(v.page, v.size);
}
SortDto sortDto = new SortDto();
var sortDto = new SortDto();
sortDto.orders = v.orders;
return PageRequest.of(v.page, v.size, SortAdapter.INSTANCE.unmarshal(sortDto));

View File

@@ -43,7 +43,7 @@ public class SortAdapter extends XmlAdapter<SortDto, Sort> {
return null;
}
SortDto dto = new SortDto();
var dto = new SortDto();
dto.orders = SpringDataJaxb.marshal(source, OrderAdapter.INSTANCE);
return dto;

View File

@@ -119,7 +119,7 @@ public abstract class SpringDataJaxb {
List<T> result = new ArrayList<>(source.size());
for (S element : source) {
for (var element : source) {
try {
result.add(adapter.unmarshal(element));
} catch (Exception o_O) {
@@ -146,7 +146,7 @@ public abstract class SpringDataJaxb {
List<S> result = new ArrayList<>();
for (T element : source) {
for (var element : source) {
try {
result.add(adapter.marshal(element));
} catch (Exception o_O) {