From fd541b9cf4afc340ae6cdef165d3b5ca9856678f Mon Sep 17 00:00:00 2001 From: David Waltermire Date: Sat, 7 Sep 2024 11:39:48 -0400 Subject: [PATCH] Adjusted UUID item type to be a child type of IStringItem to allow fallback compairison as a string. --- .../core/metapath/item/atomic/IUuidItem.java | 6 +-- .../metapath/item/atomic/UuidItemImpl.java | 17 ++++++++ .../metapath/item/atomic/IUUIDItemTest.java | 40 +++++++++++++++++++ 3 files changed, 60 insertions(+), 3 deletions(-) create mode 100644 core/src/test/java/gov/nist/secauto/metaschema/core/metapath/item/atomic/IUUIDItemTest.java diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/item/atomic/IUuidItem.java b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/item/atomic/IUuidItem.java index 3db50136a..b942c823c 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/item/atomic/IUuidItem.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/item/atomic/IUuidItem.java @@ -12,7 +12,7 @@ import edu.umd.cs.findbugs.annotations.NonNull; -public interface IUuidItem extends IAnyAtomicItem { +public interface IUuidItem extends IStringItem { /** * Construct a new item using the provided {@code value}. @@ -78,7 +78,7 @@ default int compareTo(@NonNull IUuidItem item) { } @Override - default int compareTo(IAnyAtomicItem item) { - return compareTo(cast(item)); + default int compareTo(IAnyAtomicItem other) { + return compareTo(other.asStringItem()); } } diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/item/atomic/UuidItemImpl.java b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/item/atomic/UuidItemImpl.java index 1a67f352a..597e8299d 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/item/atomic/UuidItemImpl.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/item/atomic/UuidItemImpl.java @@ -36,6 +36,23 @@ public IMapKey asMapKey() { return new MapKey(); } + @Override + public String asString() { + return asUuid().toString(); + } + + @Override + public int hashCode() { + return asString().hashCode(); + } + + @SuppressWarnings("PMD.OnlyOneReturn") + @Override + public boolean equals(Object obj) { + return this == obj + || (obj instanceof IStringItem && compareTo((IStringItem) obj) == 0); + } + private final class MapKey implements IMapKey { @Override public IUuidItem getKey() { diff --git a/core/src/test/java/gov/nist/secauto/metaschema/core/metapath/item/atomic/IUUIDItemTest.java b/core/src/test/java/gov/nist/secauto/metaschema/core/metapath/item/atomic/IUUIDItemTest.java new file mode 100644 index 000000000..111617dbd --- /dev/null +++ b/core/src/test/java/gov/nist/secauto/metaschema/core/metapath/item/atomic/IUUIDItemTest.java @@ -0,0 +1,40 @@ +/* + * SPDX-FileCopyrightText: none + * SPDX-License-Identifier: CC0-1.0 + */ + +package gov.nist.secauto.metaschema.core.metapath.item.atomic; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; + +import java.util.UUID; +import java.util.stream.Stream; + +import edu.umd.cs.findbugs.annotations.NonNull; + +class IUuidItemTest { + private static Stream testCompare() { // NOPMD - false positive + UUID uuidRandom = UUID.randomUUID(); + UUID uuid1 = UUID.fromString("4cfa2c52-9345-4012-8055-0bc9ac9b03fa"); + UUID uuid2 = UUID.fromString("25a6d916-f179-4550-ad2b-7e7cd9df35d2"); + String uuid2String = uuid2.toString(); + + return Stream.of( + Arguments.of(IUuidItem.valueOf(uuidRandom), IUuidItem.valueOf(uuidRandom), IIntegerItem.ZERO), + Arguments.of(IUuidItem.valueOf(uuid1), IUuidItem.valueOf(uuid2), IIntegerItem.valueOf(2)), + Arguments.of(IUuidItem.valueOf(uuid2), IStringItem.valueOf(uuid2String), IIntegerItem.ZERO), + Arguments.of(IStringItem.valueOf(uuid2String), IUuidItem.valueOf(uuid2), IIntegerItem.ZERO)); + } + + @ParameterizedTest + @MethodSource + void testCompare(@NonNull IAnyAtomicItem left, @NonNull IAnyAtomicItem right, @NonNull IIntegerItem expectedResult) { + IIntegerItem result = IIntegerItem.valueOf(left.compareTo(right)); + assertEquals(expectedResult, result); + } + +}