mirror of
https://github.com/openjdk/jdk.git
synced 2025-08-27 23:04:50 +02:00
8311188: Simplify and modernize equals and hashCode in java.text
Reviewed-by: lancea, naoto, rriggs
This commit is contained in:
parent
1fc726a8b3
commit
1dfb0fb3e2
15 changed files with 72 additions and 98 deletions
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright (c) 1997, 2021, Oracle and/or its affiliates. All rights reserved.
|
||||
* Copyright (c) 1997, 2023, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
|
@ -618,7 +618,7 @@ public class AttributedString {
|
|||
int currIndex = runIndex;
|
||||
int runStart = runStarts[currIndex];
|
||||
while (runStart >= beginIndex &&
|
||||
valuesMatch(value, getAttribute(attribute, currIndex - 1))) {
|
||||
Objects.equals(value, getAttribute(attribute, currIndex - 1))) {
|
||||
currIndex--;
|
||||
runStart = runStarts[currIndex];
|
||||
}
|
||||
|
@ -632,7 +632,7 @@ public class AttributedString {
|
|||
int currIndex = runIndex;
|
||||
int runLimit = (currIndex < runCount - 1) ? runStarts[currIndex + 1] : textLength;
|
||||
while (runLimit <= endIndex &&
|
||||
valuesMatch(value, getAttribute(attribute, currIndex + 1))) {
|
||||
Objects.equals(value, getAttribute(attribute, currIndex + 1))) {
|
||||
currIndex++;
|
||||
runLimit = (currIndex < runCount - 1) ? runStarts[currIndex + 1] : textLength;
|
||||
}
|
||||
|
@ -650,22 +650,13 @@ public class AttributedString {
|
|||
// returns whether all specified attributes have equal values in the runs with the given indices
|
||||
private boolean attributeValuesMatch(Set<? extends Attribute> attributes, int runIndex1, int runIndex2) {
|
||||
for (Attribute key : attributes) {
|
||||
if (!valuesMatch(getAttribute(key, runIndex1), getAttribute(key, runIndex2))) {
|
||||
if (!Objects.equals(getAttribute(key, runIndex1), getAttribute(key, runIndex2))) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// returns whether the two objects are either both null or equal
|
||||
private static final boolean valuesMatch(Object value1, Object value2) {
|
||||
if (value1 == null) {
|
||||
return value2 == null;
|
||||
} else {
|
||||
return value1.equals(value2);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Appends the contents of the CharacterIterator iterator into the
|
||||
* StringBuilder buf.
|
||||
|
@ -760,6 +751,7 @@ public class AttributedString {
|
|||
|
||||
// Object methods. See documentation in that class.
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj) {
|
||||
return true;
|
||||
|
@ -775,6 +767,7 @@ public class AttributedString {
|
|||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return text.hashCode() ^ currentIndex ^ beginIndex ^ endIndex;
|
||||
}
|
||||
|
@ -861,7 +854,7 @@ public class AttributedString {
|
|||
int runStart = currentRunStart;
|
||||
int runIndex = currentRunIndex;
|
||||
while (runStart > beginIndex &&
|
||||
valuesMatch(value, AttributedString.this.getAttribute(attribute, runIndex - 1))) {
|
||||
Objects.equals(value, AttributedString.this.getAttribute(attribute, runIndex - 1))) {
|
||||
runIndex--;
|
||||
runStart = runStarts[runIndex];
|
||||
}
|
||||
|
@ -902,7 +895,7 @@ public class AttributedString {
|
|||
int runLimit = currentRunLimit;
|
||||
int runIndex = currentRunIndex;
|
||||
while (runLimit < endIndex &&
|
||||
valuesMatch(value, AttributedString.this.getAttribute(attribute, runIndex + 1))) {
|
||||
Objects.equals(value, AttributedString.this.getAttribute(attribute, runIndex + 1))) {
|
||||
runIndex++;
|
||||
runLimit = runIndex < runCount - 1 ? runStarts[runIndex + 1] : endIndex;
|
||||
}
|
||||
|
@ -1081,11 +1074,10 @@ class AttributeEntry implements Map.Entry<Attribute,Object> {
|
|||
this.value = value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (!(o instanceof AttributeEntry other)) {
|
||||
return false;
|
||||
}
|
||||
return other.key.equals(key) && Objects.equals(other.value, value);
|
||||
return o instanceof AttributeEntry other
|
||||
&& other.key.equals(key) && Objects.equals(other.value, value);
|
||||
}
|
||||
|
||||
public Attribute getKey() {
|
||||
|
@ -1100,8 +1092,9 @@ class AttributeEntry implements Map.Entry<Attribute,Object> {
|
|||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return key.hashCode() ^ (value==null ? 0 : value.hashCode());
|
||||
return key.hashCode() ^ Objects.hashCode(value);
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue