Skip to content

Commit a5f34da

Browse files
authored
Add missing equals and hashCode methods in modular Java path type (#11118)
1 parent b102437 commit a5f34da

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

api/maven-api-core/src/main/java/org/apache/maven/api/JavaPathType.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,25 @@ public String[] option(Iterable<? extends Path> paths) {
376376
return format(moduleName, paths);
377377
}
378378

379+
/**
380+
* {@return a hash code value based on the raw type and module name}.
381+
*/
382+
@Override
383+
public int hashCode() {
384+
return rawType().hashCode() + 17 * moduleName.hashCode();
385+
}
386+
387+
/**
388+
* {@return whether the given object represents the same type of path as this object}.
389+
*/
390+
@Override
391+
public boolean equals(Object obj) {
392+
if (obj instanceof Modular m) {
393+
return rawType() == m.rawType() && moduleName.equals(m.moduleName);
394+
}
395+
return false;
396+
}
397+
379398
/**
380399
* Returns the programmatic name of this path type, including the module to patch.
381400
* For example, if this type was created by {@code JavaPathType.patchModule("foo.bar")},

api/maven-api-core/src/test/java/org/apache/maven/api/JavaPathTypeTest.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.junit.jupiter.api.Test;
2626

2727
import static org.junit.jupiter.api.Assertions.assertEquals;
28+
import static org.junit.jupiter.api.Assertions.assertNotEquals;
2829

2930
public class JavaPathTypeTest {
3031
/**
@@ -65,4 +66,18 @@ public void testModularOption() {
6566
assertEquals("--patch-module", formatted[0]);
6667
assertEquals(toPlatformSpecific("my.module=\"src/foo.java:src/bar.java\""), formatted[1]);
6768
}
69+
70+
/**
71+
* Tests the {@code equals} and {@code hashCode} methods of options.
72+
*/
73+
@Test
74+
public void testEqualsHashCode() {
75+
JavaPathType.Modular foo1 = JavaPathType.patchModule("foo");
76+
JavaPathType.Modular foo2 = JavaPathType.patchModule("foo");
77+
JavaPathType.Modular bar = JavaPathType.patchModule("bar");
78+
assertEquals(foo1, foo2);
79+
assertEquals(foo1.hashCode(), foo2.hashCode());
80+
assertNotEquals(foo1, bar);
81+
assertNotEquals(foo1.hashCode(), bar.hashCode());
82+
}
6883
}

0 commit comments

Comments
 (0)