Introduce ObjectUtils#nullSafeHash(Object... element)

This commit deprecates the various nullSafeHashCode methods taking array
types as they are superseded by Arrays.hashCode now. This means that
the now only remaining nullSafeHashCode method does not trigger a
warning only if the target type is not an array. At the same time, there
are multiple use of this method on several elements, handling the
accumulation of hash codes.

For that reason, this commit also introduces a nullSafeHash that takes
an array of elements. The only difference between Objects.hash is that
this method handles arrays.

The codebase has been reviewed to use any of those two methods when it
is possible.

Closes gh-29051
This commit is contained in:
Stephane Nicoll
2023-08-25 15:20:24 +02:00
parent f2e898d35d
commit 01f717375b
38 changed files with 276 additions and 322 deletions

View File

@@ -240,15 +240,8 @@ public final class ContentDisposition {
@Override
public int hashCode() {
int result = ObjectUtils.nullSafeHashCode(this.type);
result = 31 * result + ObjectUtils.nullSafeHashCode(this.name);
result = 31 * result + ObjectUtils.nullSafeHashCode(this.filename);
result = 31 * result + ObjectUtils.nullSafeHashCode(this.charset);
result = 31 * result + ObjectUtils.nullSafeHashCode(this.size);
result = 31 * result + ObjectUtils.nullSafeHashCode(this.creationDate);
result = 31 * result + ObjectUtils.nullSafeHashCode(this.modificationDate);
result = 31 * result + ObjectUtils.nullSafeHashCode(this.readDate);
return result;
return ObjectUtils.nullSafeHash(this.type, this.name,this.filename,
this.charset, this.size, this.creationDate, this.modificationDate, this.readDate);
}
/**

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.
@@ -141,7 +141,7 @@ public class HttpEntity<T> {
@Override
public int hashCode() {
return (ObjectUtils.nullSafeHashCode(this.headers) * 29 + ObjectUtils.nullSafeHashCode(this.body));
return ObjectUtils.nullSafeHash(this.headers, this.body);
}
@Override

View File

@@ -21,6 +21,7 @@ import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.StringJoiner;
import org.springframework.core.io.InputStreamResource;
@@ -276,8 +277,7 @@ public abstract class HttpRange {
@Override
public int hashCode() {
return (ObjectUtils.nullSafeHashCode(this.firstPos) * 31 +
ObjectUtils.nullSafeHashCode(this.lastPos));
return Objects.hash(this.firstPos, this.lastPos);
}
@Override

View File

@@ -19,6 +19,7 @@ package org.springframework.http;
import java.net.URI;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Objects;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
@@ -244,13 +245,8 @@ public class ProblemDetail {
@Override
public int hashCode() {
int result = this.type.hashCode();
result = 31 * result + ObjectUtils.nullSafeHashCode(getTitle());
result = 31 * result + this.status;
result = 31 * result + ObjectUtils.nullSafeHashCode(this.detail);
result = 31 * result + ObjectUtils.nullSafeHashCode(this.instance);
result = 31 * result + ObjectUtils.nullSafeHashCode(this.properties);
return result;
return Objects.hash(this.type, getTitle(), this.status, this.detail,
this.instance, this.properties);
}
@Override

View File

@@ -24,6 +24,7 @@ import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.StringJoiner;
import java.util.function.BiFunction;
import java.util.function.UnaryOperator;
@@ -566,14 +567,8 @@ final class HierarchicalUriComponents extends UriComponents {
@Override
public int hashCode() {
int result = ObjectUtils.nullSafeHashCode(getScheme());
result = 31 * result + ObjectUtils.nullSafeHashCode(this.userInfo);
result = 31 * result + ObjectUtils.nullSafeHashCode(this.host);
result = 31 * result + ObjectUtils.nullSafeHashCode(this.port);
result = 31 * result + this.path.hashCode();
result = 31 * result + this.queryParams.hashCode();
result = 31 * result + ObjectUtils.nullSafeHashCode(getFragment());
return result;
return Objects.hash(getScheme(), this.userInfo, this.host, this.port,
this.path, this.queryParams, getFragment());
}

View File

@@ -21,6 +21,7 @@ import java.net.URISyntaxException;
import java.nio.charset.Charset;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import org.springframework.lang.Nullable;
import org.springframework.util.LinkedMultiValueMap;
@@ -166,10 +167,7 @@ final class OpaqueUriComponents extends UriComponents {
@Override
public int hashCode() {
int result = ObjectUtils.nullSafeHashCode(getScheme());
result = 31 * result + ObjectUtils.nullSafeHashCode(this.ssp);
result = 31 * result + ObjectUtils.nullSafeHashCode(getFragment());
return result;
return Objects.hash(getScheme(), this.ssp, getFragment());
}
}