Skip to content

Commit

Permalink
Merge pull request #241 from keithc-ca/hash
Browse files Browse the repository at this point in the history
Back-port fix for ModuleDescriptor.hashCode()
  • Loading branch information
tajila authored Jul 26, 2023
2 parents bf3d2d7 + b9b17af commit 7511966
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions src/java.base/share/classes/java/lang/module/ModuleDescriptor.java
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ public boolean equals(Object ob) {
*/
@Override
public int hashCode() {
int hash = name.hashCode() * 43 + mods.hashCode();
int hash = name.hashCode() * 43 + modsHashCode(mods);
if (compiledVersion != null)
hash = hash * 43 + compiledVersion.hashCode();
if (rawCompiledVersion != null)
Expand Down Expand Up @@ -505,7 +505,7 @@ public int compareTo(Exports that) {
*/
@Override
public int hashCode() {
int hash = mods.hashCode();
int hash = modsHashCode(mods);
hash = hash * 43 + source.hashCode();
return hash * 43 + targets.hashCode();
}
Expand Down Expand Up @@ -708,7 +708,7 @@ public int compareTo(Opens that) {
*/
@Override
public int hashCode() {
int hash = mods.hashCode();
int hash = modsHashCode(mods);
hash = hash * 43 + source.hashCode();
return hash * 43 + targets.hashCode();
}
Expand Down Expand Up @@ -2261,7 +2261,7 @@ public int hashCode() {
int hc = hash;
if (hc == 0) {
hc = name.hashCode();
hc = hc * 43 + Objects.hashCode(modifiers);
hc = hc * 43 + modsHashCode(modifiers);
hc = hc * 43 + requires.hashCode();
hc = hc * 43 + Objects.hashCode(packages);
hc = hc * 43 + exports.hashCode();
Expand Down Expand Up @@ -2546,6 +2546,18 @@ private static <M> String toString(Set<M> mods, String what) {
.collect(Collectors.joining(" "));
}

/**
* Generates and returns a hashcode for the enum instances. The returned hashcode
* is a value based on the {@link Enum#name() name} of each enum instance.
*/
private static int modsHashCode(Iterable<? extends Enum<?>> enums) {
int h = 0;
for (Enum<?> e : enums) {
h += e.name().hashCode();
}
return h;
}

private static <T extends Object & Comparable<? super T>>
int compare(T obj1, T obj2) {
if (obj1 != null) {
Expand Down

0 comments on commit 7511966

Please sign in to comment.