Consistent equals/hashCode style (and related polishing)

This commit is contained in:
Juergen Hoeller
2023-07-19 00:35:19 +02:00
parent bbcc788f60
commit 2f33e77ab4
98 changed files with 413 additions and 835 deletions

View File

@@ -37,12 +37,14 @@ public abstract class AbstractTypeReference implements TypeReference {
@Nullable
private final TypeReference enclosingType;
protected AbstractTypeReference(String packageName, String simpleName, @Nullable TypeReference enclosingType) {
this.packageName = packageName;
this.simpleName = simpleName;
this.enclosingType = enclosingType;
}
@Override
public String getName() {
TypeReference enclosingType = getEnclosingType();
@@ -67,32 +69,28 @@ public abstract class AbstractTypeReference implements TypeReference {
return this.enclosingType;
}
protected abstract boolean isPrimitive();
protected String addPackageIfNecessary(String part) {
if (this.packageName.isEmpty() ||
this.packageName.equals("java.lang") && isPrimitive()) {
(this.packageName.equals("java.lang") && isPrimitive())) {
return part;
}
return this.packageName + '.' + part;
}
protected abstract boolean isPrimitive();
@Override
public boolean equals(@Nullable Object other) {
return (this == other || (other instanceof TypeReference that &&
getCanonicalName().equals(that.getCanonicalName())));
}
@Override
public int hashCode() {
return Objects.hash(getCanonicalName());
}
@Override
public boolean equals(@Nullable Object other) {
if (this == other) {
return true;
}
if (!(other instanceof TypeReference otherReference)) {
return false;
}
return getCanonicalName().equals(otherReference.getCanonicalName());
}
@Override
public String toString() {
return getCanonicalName();

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2021 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -51,14 +51,9 @@ public final class MethodClassKey implements Comparable<MethodClassKey> {
@Override
public boolean equals(@Nullable Object other) {
if (this == other) {
return true;
}
if (!(other instanceof MethodClassKey otherKey)) {
return false;
}
return (this.method.equals(otherKey.method) &&
ObjectUtils.nullSafeEquals(this.targetClass, otherKey.targetClass));
return (this == other || (other instanceof MethodClassKey that &&
this.method.equals(that.method) &&
ObjectUtils.nullSafeEquals(this.targetClass, that.targetClass)));
}
@Override

View File

@@ -750,17 +750,12 @@ public class MethodParameter {
@Override
public boolean equals(@Nullable Object other) {
if (this == other) {
return true;
}
if (!(other instanceof MethodParameter otherParam)) {
return false;
}
return (getContainingClass() == otherParam.getContainingClass() &&
ObjectUtils.nullSafeEquals(this.typeIndexesPerLevel, otherParam.typeIndexesPerLevel) &&
this.nestingLevel == otherParam.nestingLevel &&
this.parameterIndex == otherParam.parameterIndex &&
this.executable.equals(otherParam.executable));
return (this == other || (other instanceof MethodParameter that &&
getContainingClass() == that.getContainingClass() &&
ObjectUtils.nullSafeEquals(this.typeIndexesPerLevel, that.typeIndexesPerLevel) &&
this.nestingLevel == that.nestingLevel &&
this.parameterIndex == that.parameterIndex &&
this.executable.equals(that.executable)));
}
@Override

View File

@@ -1577,14 +1577,9 @@ public class ResolvableType implements Serializable {
@Override
public boolean equals(@Nullable Object other) {
if (this == other) {
return true;
}
if (!(other instanceof ParameterizedType otherType)) {
return false;
}
return (otherType.getOwnerType() == null && this.rawType.equals(otherType.getRawType()) &&
Arrays.equals(this.typeArguments, otherType.getActualTypeArguments()));
return (this == other || (other instanceof ParameterizedType that &&
that.getOwnerType() == null && this.rawType.equals(that.getRawType()) &&
Arrays.equals(this.typeArguments, that.getActualTypeArguments())));
}
@Override

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -260,16 +260,11 @@ public final class Property {
@Override
public boolean equals(@Nullable Object other) {
if (this == other) {
return true;
}
if (!(other instanceof Property otherProperty)) {
return false;
}
return (ObjectUtils.nullSafeEquals(this.objectType, otherProperty.objectType) &&
ObjectUtils.nullSafeEquals(this.name, otherProperty.name) &&
ObjectUtils.nullSafeEquals(this.readMethod, otherProperty.readMethod) &&
ObjectUtils.nullSafeEquals(this.writeMethod, otherProperty.writeMethod));
return (this == other || (other instanceof Property that &&
ObjectUtils.nullSafeEquals(this.objectType, that.objectType) &&
ObjectUtils.nullSafeEquals(this.name, that.name) &&
ObjectUtils.nullSafeEquals(this.readMethod, that.readMethod) &&
ObjectUtils.nullSafeEquals(this.writeMethod, that.writeMethod)));
}
@Override

View File

@@ -387,7 +387,7 @@ public class GenericConversionService implements ConfigurableConversionService {
@Override
public String toString() {
return (this.typeInfo + " : " + this.converter);
return this.typeInfo + " : " + this.converter;
}
}
@@ -438,7 +438,7 @@ public class GenericConversionService implements ConfigurableConversionService {
@Override
public String toString() {
return (this.typeInfo + " : " + this.converterFactory);
return this.typeInfo + " : " + this.converterFactory;
}
}
@@ -459,25 +459,19 @@ public class GenericConversionService implements ConfigurableConversionService {
@Override
public boolean equals(@Nullable Object other) {
if (this == other) {
return true;
}
if (!(other instanceof ConverterCacheKey otherKey)) {
return false;
}
return (this.sourceType.equals(otherKey.sourceType)) &&
this.targetType.equals(otherKey.targetType);
return (this == other || (other instanceof ConverterCacheKey that &&
this.sourceType.equals(that.sourceType)) &&
this.targetType.equals(that.targetType));
}
@Override
public int hashCode() {
return (this.sourceType.hashCode() * 29 + this.targetType.hashCode());
return this.sourceType.hashCode() * 29 + this.targetType.hashCode();
}
@Override
public String toString() {
return ("ConverterCacheKey [sourceType = " + this.sourceType +
", targetType = " + this.targetType + "]");
return "ConverterCacheKey [sourceType = " + this.sourceType + ", targetType = " + this.targetType + "]";
}
@Override

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2021 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -252,7 +252,7 @@ public abstract class CommandLinePropertySource<T> extends EnumerablePropertySou
@Override
public final boolean containsProperty(String name) {
if (this.nonOptionArgsPropertyName.equals(name)) {
return !this.getNonOptionArgs().isEmpty();
return !getNonOptionArgs().isEmpty();
}
return this.containsOption(name);
}
@@ -270,7 +270,7 @@ public abstract class CommandLinePropertySource<T> extends EnumerablePropertySou
@Nullable
public final String getProperty(String name) {
if (this.nonOptionArgsPropertyName.equals(name)) {
Collection<String> nonOptionArguments = this.getNonOptionArgs();
Collection<String> nonOptionArguments = getNonOptionArgs();
if (nonOptionArguments.isEmpty()) {
return null;
}
@@ -278,7 +278,7 @@ public abstract class CommandLinePropertySource<T> extends EnumerablePropertySou
return StringUtils.collectionToCommaDelimitedString(nonOptionArguments);
}
}
Collection<String> optionValues = this.getOptionValues(name);
Collection<String> optionValues = getOptionValues(name);
if (optionValues == null) {
return null;
}

View File

@@ -482,15 +482,10 @@ public class DefaultDataBuffer implements DataBuffer {
@Override
public boolean equals(@Nullable Object other) {
if (this == other) {
return true;
}
if (!(other instanceof DefaultDataBuffer otherBuffer)) {
return false;
}
return (this.readPosition == otherBuffer.readPosition &&
this.writePosition == otherBuffer.writePosition &&
this.byteBuffer.equals(otherBuffer.byteBuffer));
return (this == other || (other instanceof DefaultDataBuffer that &&
this.readPosition == that.readPosition &&
this.writePosition == that.writePosition &&
this.byteBuffer.equals(that.byteBuffer)));
}
@Override

View File

@@ -184,15 +184,10 @@ public class EncodedResource implements InputStreamSource {
@Override
public boolean equals(@Nullable Object other) {
if (this == other) {
return true;
}
if (!(other instanceof EncodedResource otherResource)) {
return false;
}
return (this.resource.equals(otherResource.resource) &&
ObjectUtils.nullSafeEquals(this.charset, otherResource.charset) &&
ObjectUtils.nullSafeEquals(this.encoding, otherResource.encoding));
return (this == other || (other instanceof EncodedResource that &&
this.resource.equals(that.resource) &&
ObjectUtils.nullSafeEquals(this.charset, that.charset) &&
ObjectUtils.nullSafeEquals(this.encoding, that.encoding)));
}
@Override

View File

@@ -751,28 +751,22 @@ public class ConcurrentReferenceHashMap<K, V> extends AbstractMap<K, V> implemen
return previous;
}
@Override
public boolean equals(@Nullable Object other) {
return (this == other || (other instanceof Map.Entry<?, ?> that &&
ObjectUtils.nullSafeEquals(getKey(), that.getKey()) &&
ObjectUtils.nullSafeEquals(getValue(), that.getValue())));
}
@Override
public int hashCode() {
return (ObjectUtils.nullSafeHashCode(this.key) ^ ObjectUtils.nullSafeHashCode(this.value));
}
@Override
public String toString() {
return (this.key + "=" + this.value);
}
@Override
@SuppressWarnings("rawtypes")
public final boolean equals(@Nullable Object other) {
if (this == other) {
return true;
}
if (!(other instanceof Map.Entry otherEntry)) {
return false;
}
return (ObjectUtils.nullSafeEquals(getKey(), otherEntry.getKey()) &&
ObjectUtils.nullSafeEquals(getValue(), otherEntry.getValue()));
}
@Override
public final int hashCode() {
return (ObjectUtils.nullSafeHashCode(this.key) ^ ObjectUtils.nullSafeHashCode(this.value));
}
}

View File

@@ -410,7 +410,7 @@ public class MimeType implements Comparable<MimeType>, Serializable {
return (thisSuffix.equals(other.getSubtype()) || thisSuffix.equals(otherSuffix));
}
else if (other.isWildcardSubtype() && otherSuffix != null) {
return (this.getSubtype().equals(otherSuffix) || otherSuffix.equals(thisSuffix));
return (getSubtype().equals(otherSuffix) || otherSuffix.equals(thisSuffix));
}
}
}
@@ -451,15 +451,10 @@ public class MimeType implements Comparable<MimeType>, Serializable {
@Override
public boolean equals(@Nullable Object other) {
if (this == other) {
return true;
}
if (!(other instanceof MimeType otherType)) {
return false;
}
return (this.type.equalsIgnoreCase(otherType.type) &&
return (this == other || (other instanceof MimeType otherType &&
this.type.equalsIgnoreCase(otherType.type) &&
this.subtype.equalsIgnoreCase(otherType.subtype) &&
parametersAreEqual(otherType));
parametersAreEqual(otherType)));
}
/**

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -107,16 +107,10 @@ public class NullSafeComparator<T> implements Comparator<T> {
@Override
@SuppressWarnings("unchecked")
public boolean equals(@Nullable Object other) {
if (this == other) {
return true;
}
if (!(other instanceof NullSafeComparator<?> otherComparator)) {
return false;
}
return (this.nonNullComparator.equals(otherComparator.nonNullComparator)
&& this.nullsLow == otherComparator.nullsLow);
return (this == other || (other instanceof NullSafeComparator<?> that &&
this.nonNullComparator.equals(that.nonNullComparator) &&
this.nullsLow == that.nullsLow));
}
@Override

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -99,7 +99,7 @@ public class MockPropertySource extends PropertiesPropertySource {
* @return this {@link MockPropertySource} instance
*/
public MockPropertySource withProperty(String name, Object value) {
this.setProperty(name, value);
setProperty(name, value);
return this;
}