Skip to content

Commit e9d9131

Browse files
committed
8263358: Refactored equals methods
1 parent d5bcd25 commit e9d9131

File tree

9 files changed

+43
-57
lines changed

9 files changed

+43
-57
lines changed

src/java.base/share/classes/java/lang/PublicMethods.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,10 +113,10 @@ static boolean matches(Method method,
113113
@Override
114114
public boolean equals(Object o) {
115115
if (this == o) return true;
116-
if (!(o instanceof Key that)) return false;
117116
//noinspection StringEquality (guaranteed interned String(s))
118-
return name == that.name &&
119-
Arrays.equals(ptypes, that.ptypes);
117+
return (o instanceof Key that)
118+
&& name == that.name
119+
&& Arrays.equals(ptypes, that.ptypes);
120120
}
121121

122122
@Override

src/java.base/share/classes/java/lang/Runtime.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1425,10 +1425,8 @@ public boolean equals(Object obj) {
14251425
public boolean equalsIgnoreOptional(Object obj) {
14261426
if (this == obj)
14271427
return true;
1428-
if (!(obj instanceof Version that))
1429-
return false;
1430-
1431-
return (this.version().equals(that.version())
1428+
return (obj instanceof Version that)
1429+
&& (this.version().equals(that.version())
14321430
&& this.pre().equals(that.pre())
14331431
&& this.build().equals(that.build()));
14341432
}

src/java.base/share/classes/java/lang/StackTraceElement.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -406,15 +406,14 @@ public String toString() {
406406
public boolean equals(Object obj) {
407407
if (obj==this)
408408
return true;
409-
if (!(obj instanceof StackTraceElement e))
410-
return false;
411-
return Objects.equals(classLoaderName, e.classLoaderName) &&
412-
Objects.equals(moduleName, e.moduleName) &&
413-
Objects.equals(moduleVersion, e.moduleVersion) &&
414-
e.declaringClass.equals(declaringClass) &&
415-
e.lineNumber == lineNumber &&
416-
Objects.equals(methodName, e.methodName) &&
417-
Objects.equals(fileName, e.fileName);
409+
return (obj instanceof StackTraceElement e)
410+
&& Objects.equals(classLoaderName, e.classLoaderName)
411+
&& Objects.equals(moduleName, e.moduleName)
412+
&& Objects.equals(moduleVersion, e.moduleVersion)
413+
&& e.declaringClass.equals(declaringClass)
414+
&& e.lineNumber == lineNumber
415+
&& Objects.equals(methodName, e.methodName)
416+
&& Objects.equals(fileName, e.fileName);
418417
}
419418

420419
/**

src/java.base/share/classes/java/lang/String.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1818,12 +1818,9 @@ public boolean equals(Object anObject) {
18181818
if (this == anObject) {
18191819
return true;
18201820
}
1821-
if (anObject instanceof String aString) {
1822-
if (!COMPACT_STRINGS || this.coder == aString.coder) {
1823-
return StringLatin1.equals(value, aString.value);
1824-
}
1825-
}
1826-
return false;
1821+
return (anObject instanceof String aString)
1822+
&& (!COMPACT_STRINGS || this.coder == aString.coder)
1823+
&& StringLatin1.equals(value, aString.value);
18271824
}
18281825

18291826
/**

src/java.base/share/classes/java/lang/constant/DynamicConstantDesc.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -357,11 +357,11 @@ private static ConstantDesc canonicalizeArrayVarHandle(DynamicConstantDesc<?> de
357357
@Override
358358
public final boolean equals(Object o) {
359359
if (this == o) return true;
360-
if (!(o instanceof DynamicConstantDesc desc)) return false;
361-
return Objects.equals(bootstrapMethod, desc.bootstrapMethod) &&
362-
Arrays.equals(bootstrapArgs, desc.bootstrapArgs) &&
363-
Objects.equals(constantName, desc.constantName) &&
364-
Objects.equals(constantType, desc.constantType);
360+
return (o instanceof DynamicConstantDesc desc)
361+
&& Objects.equals(bootstrapMethod, desc.bootstrapMethod)
362+
&& Arrays.equals(bootstrapArgs, desc.bootstrapArgs)
363+
&& Objects.equals(constantName, desc.constantName)
364+
&& Objects.equals(constantType, desc.constantType);
365365
}
366366

367367
@Override

src/java.base/share/classes/java/lang/invoke/LambdaForm.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1175,8 +1175,9 @@ synchronized void resolve() {
11751175
public boolean equals(Object other) {
11761176
if (this == other) return true;
11771177
if (other == null) return false;
1178-
if (!(other instanceof NamedFunction that)) return false;
1179-
return this.member != null && this.member.equals(that.member);
1178+
return (other instanceof NamedFunction that)
1179+
&& this.member != null
1180+
&& this.member.equals(that.member);
11801181
}
11811182

11821183
@Override

src/java.base/share/classes/java/lang/module/ModuleDescriptor.java

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -310,9 +310,8 @@ public int compareTo(Requires that) {
310310
*/
311311
@Override
312312
public boolean equals(Object ob) {
313-
if (!(ob instanceof Requires that))
314-
return false;
315-
return name.equals(that.name) && mods.equals(that.mods)
313+
return (ob instanceof Requires that)
314+
&& name.equals(that.name) && mods.equals(that.mods)
316315
&& Objects.equals(compiledVersion, that.compiledVersion)
317316
&& Objects.equals(rawCompiledVersion, that.rawCompiledVersion);
318317
}
@@ -530,9 +529,8 @@ public int hashCode() {
530529
*/
531530
@Override
532531
public boolean equals(Object ob) {
533-
if (!(ob instanceof Exports other))
534-
return false;
535-
return Objects.equals(this.mods, other.mods)
532+
return (ob instanceof Exports other)
533+
&& Objects.equals(this.mods, other.mods)
536534
&& Objects.equals(this.source, other.source)
537535
&& Objects.equals(this.targets, other.targets);
538536
}
@@ -734,11 +732,10 @@ public int hashCode() {
734732
*/
735733
@Override
736734
public boolean equals(Object ob) {
737-
if (!(ob instanceof Opens other))
738-
return false;
739-
return Objects.equals(this.mods, other.mods)
740-
&& Objects.equals(this.source, other.source)
741-
&& Objects.equals(this.targets, other.targets);
735+
return (ob instanceof Opens other)
736+
&& Objects.equals(this.mods, other.mods)
737+
&& Objects.equals(this.source, other.source)
738+
&& Objects.equals(this.targets, other.targets);
742739
}
743740

744741
/**
@@ -869,10 +866,9 @@ public int hashCode() {
869866
*/
870867
@Override
871868
public boolean equals(Object ob) {
872-
if (!(ob instanceof Provides other))
873-
return false;
874-
return Objects.equals(this.service, other.service) &&
875-
Objects.equals(this.providers, other.providers);
869+
return (ob instanceof Provides other)
870+
&& Objects.equals(this.service, other.service)
871+
&& Objects.equals(this.providers, other.providers);
876872
}
877873

878874
/**
@@ -2237,9 +2233,8 @@ public int compareTo(ModuleDescriptor that) {
22372233
public boolean equals(Object ob) {
22382234
if (ob == this)
22392235
return true;
2240-
if (!(ob instanceof ModuleDescriptor that))
2241-
return false;
2242-
return (name.equals(that.name)
2236+
return (ob instanceof ModuleDescriptor that)
2237+
&& (name.equals(that.name)
22432238
&& modifiers.equals(that.modifiers)
22442239
&& requires.equals(that.requires)
22452240
&& Objects.equals(packages, that.packages)

src/java.base/share/classes/java/lang/module/ResolvedModule.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -138,11 +138,9 @@ public int hashCode() {
138138
*/
139139
@Override
140140
public boolean equals(Object ob) {
141-
if (!(ob instanceof ResolvedModule that))
142-
return false;
143-
144-
return Objects.equals(this.cf, that.cf)
145-
&& Objects.equals(this.mref, that.mref);
141+
return (ob instanceof ResolvedModule that)
142+
&& Objects.equals(this.cf, that.cf)
143+
&& Objects.equals(this.mref, that.mref);
146144
}
147145

148146
/**

src/java.base/share/classes/java/lang/reflect/Parameter.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,9 @@ public final class Parameter implements AnnotatedElement {
7777
*/
7878
@Override
7979
public boolean equals(Object obj) {
80-
if(obj instanceof Parameter other) {
81-
return (other.executable.equals(executable) &&
82-
other.index == index);
83-
}
84-
return false;
80+
return (obj instanceof Parameter other)
81+
&& other.executable.equals(executable)
82+
&& other.index == index;
8583
}
8684

8785
/**

0 commit comments

Comments
 (0)