Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adjusted UUID item type to be a child type of IStringItem to allow fa… #103

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}

}