Skip to content

Commit

Permalink
Adjusted UUID item type to be a child type of IStringItem to allow fa…
Browse files Browse the repository at this point in the history
…llback compairison as a string.
  • Loading branch information
david-waltermire committed Sep 7, 2024
1 parent aa2ac75 commit fd541b9
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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}.
Expand Down Expand Up @@ -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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Arguments> 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);
}

}

0 comments on commit fd541b9

Please sign in to comment.