From bb6d64cff011dab6ec2c0fa9c25fbc63b0acc0f9 Mon Sep 17 00:00:00 2001 From: Martin Desruisseaux Date: Sun, 14 Sep 2025 22:50:36 +0200 Subject: [PATCH] Add missing equals and hashCode methods in modular Java path type. --- .../org/apache/maven/api/JavaPathType.java | 19 +++++++++++++++++++ .../apache/maven/api/JavaPathTypeTest.java | 15 +++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/api/maven-api-core/src/main/java/org/apache/maven/api/JavaPathType.java b/api/maven-api-core/src/main/java/org/apache/maven/api/JavaPathType.java index 63188ec6fd37..b2e98ed3c6ad 100644 --- a/api/maven-api-core/src/main/java/org/apache/maven/api/JavaPathType.java +++ b/api/maven-api-core/src/main/java/org/apache/maven/api/JavaPathType.java @@ -376,6 +376,25 @@ public String[] option(Iterable paths) { return format(moduleName, paths); } + /** + * {@return a hash code value based on the raw type and module name}. + */ + @Override + public int hashCode() { + return rawType().hashCode() + 17 * moduleName.hashCode(); + } + + /** + * {@return whether the given object represents the same type of path as this object}. + */ + @Override + public boolean equals(Object obj) { + if (obj instanceof Modular m) { + return rawType() == m.rawType() && moduleName.equals(m.moduleName); + } + return false; + } + /** * Returns the programmatic name of this path type, including the module to patch. * For example, if this type was created by {@code JavaPathType.patchModule("foo.bar")}, diff --git a/api/maven-api-core/src/test/java/org/apache/maven/api/JavaPathTypeTest.java b/api/maven-api-core/src/test/java/org/apache/maven/api/JavaPathTypeTest.java index e46ca80a269d..701a82775bde 100644 --- a/api/maven-api-core/src/test/java/org/apache/maven/api/JavaPathTypeTest.java +++ b/api/maven-api-core/src/test/java/org/apache/maven/api/JavaPathTypeTest.java @@ -25,6 +25,7 @@ import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotEquals; public class JavaPathTypeTest { /** @@ -65,4 +66,18 @@ public void testModularOption() { assertEquals("--patch-module", formatted[0]); assertEquals(toPlatformSpecific("my.module=\"src/foo.java:src/bar.java\""), formatted[1]); } + + /** + * Tests the {@code equals} and {@code hashCode} methods of options. + */ + @Test + public void testEqualsHashCode() { + JavaPathType.Modular foo1 = JavaPathType.patchModule("foo"); + JavaPathType.Modular foo2 = JavaPathType.patchModule("foo"); + JavaPathType.Modular bar = JavaPathType.patchModule("bar"); + assertEquals(foo1, foo2); + assertEquals(foo1.hashCode(), foo2.hashCode()); + assertNotEquals(foo1, bar); + assertNotEquals(foo1.hashCode(), bar.hashCode()); + } }