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

OAK-11370 : add utils in oak-commons to check equality of 2 iterables #1968

Open
wants to merge 2 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -21,6 +21,7 @@
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
Expand Down Expand Up @@ -520,6 +521,34 @@ public static <T> Stream<T> toStream(@NotNull Iterator<T> iterator) {
return StreamSupport.stream(toIterable(iterator).spliterator(), false);
}

public static boolean elementsEqual(Iterable<?> iterable1, Iterable<?> iterable2) {
Objects.requireNonNull(iterable1);
Objects.requireNonNull(iterable2);

if (iterable1 instanceof Collection && iterable2 instanceof Collection) {
Collection<?> collection1 = (Collection<?>) iterable1;
Collection<?> collection2 = (Collection<?>) iterable2;
if (collection1.size() != collection2.size()) {
return false;
}
}

Copy link
Contributor

@reschke reschke Jan 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would put that into an "else" (return values from the same nesting level).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes done.

return elementsEqual(iterable1.iterator(), iterable2.iterator());
}

public static boolean elementsEqual(Iterator<?> iterator1, Iterator<?> iterator2) {
Objects.requireNonNull(iterator1);
Objects.requireNonNull(iterator2);

while (iterator1.hasNext() && iterator2.hasNext()) {
if (!Objects.equals(iterator1.next(), iterator2.next())) {
return false;
}
}

return !iterator1.hasNext() && !iterator2.hasNext();
}

/**
* Ensure the capacity of a map or set given the expected number of elements.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

public class CollectionUtilsTest {
Expand Down Expand Up @@ -725,4 +727,106 @@ public void ensureCapacityWithNegativeValue() {
int capacity = CollectionUtils.ensureCapacity(-8);
fail("Should throw IllegalArgumentException");
}

@Test
public void testEqualsNonEmptyIterables() {
Iterable<Integer> iterable1 = Arrays.asList(1, 2, 3);
Iterable<Integer> iterable2 = Arrays.asList(1, 2, 3);
assertTrue(CollectionUtils.elementsEqual(iterable1, iterable2));
}

@Test
public void testUnequalNonEmptyIterables() {
Iterable<Integer> iterable1 = Arrays.asList(1, 2, 3);
Iterable<Integer> iterable2 = Arrays.asList(1, 2, 4);
assertFalse(CollectionUtils.elementsEqual(iterable1, iterable2));
}

@Test
public void testEqualsEmptyIterables() {
Iterable<Integer> iterable1 = Collections.emptyList();
Iterable<Integer> iterable2 = Collections.emptyList();
assertTrue(CollectionUtils.elementsEqual(iterable1, iterable2));
}

@Test
public void testEqualsOneEmptyOneNonEmptyIterable() {
Iterable<Integer> iterable1 = Collections.emptyList();
Iterable<Integer> iterable2 = Arrays.asList(1, 2, 3);
assertFalse(CollectionUtils.elementsEqual(iterable1, iterable2));
}

@Test
public void testEqualsDifferentTypeOfElements() {
Iterable<String> iterable1 = Arrays.asList("apple", "banana", "cherry");
Iterable<Integer> iterable2 = Arrays.asList(1, 2, 3);
assertFalse(CollectionUtils.elementsEqual(iterable1, iterable2));
}

@Test
public void testEqualsNullValuesInIterables() {
Iterable<String> iterable1 = Arrays.asList("apple", null, "cherry");
Iterable<String> iterable2 = Arrays.asList("apple", null, "cherry");
assertTrue(CollectionUtils.elementsEqual(iterable1, iterable2));
}

@Test
public void testEqualsDifferentSizesIterables() {
Iterable<Integer> iterable1 = Arrays.asList(1, 2);
Iterable<Integer> iterable2 = Arrays.asList(1, 2, 3);
assertFalse(CollectionUtils.elementsEqual(iterable1, iterable2));
}

@Test
public void testEqualNonEmptyIterators() {
Iterator<Integer> iterator1 = Arrays.asList(1, 2, 3).iterator();
Iterator<Integer> iterator2 = Arrays.asList(1, 2, 3).iterator();
assertTrue(CollectionUtils.elementsEqual(iterator1, iterator2));
}


@Test
public void testEqualNonEmptyIteratorsWithDifferentValues() {
Iterator<Integer> iterator1 = Arrays.asList(1, 2, 3).iterator();
Iterator<Integer> iterator2 = Arrays.asList(1, 2, 4).iterator();
assertFalse(CollectionUtils.elementsEqual(iterator1, iterator2));

}

@Test
public void testEqualsEmptyIterators() {
Iterator<?> iterator1 = Collections.emptyIterator();
Iterator<?> iterator2 = Collections.emptyIterator();
assertTrue(CollectionUtils.elementsEqual(iterator1, iterator2));
}

@Test
public void testEqualsOneEmptyAndOneNonEmptyIterator() {
Iterator<?> iterator1 = Collections.emptyIterator();
Iterator<Integer> iterator2 = Arrays.asList(1, 2, 3).iterator();
assertFalse(CollectionUtils.elementsEqual(iterator1, iterator2));

}

@Test
public void testEqualsDifferentTypesElementsInIterators() {
Iterator<String> iterator1 = Arrays.asList("apple", "banana").iterator();
Iterator<Integer> iterator2 = Arrays.asList(1, 2).iterator();
assertFalse(CollectionUtils.elementsEqual(iterator1, iterator2));

}

@Test
public void testEqualsNullValuesInIterators() {
Iterator<String> iterator1 = Arrays.asList("apple", null).iterator();
Iterator<String> iterator2 = Arrays.asList("apple", null).iterator();
assertTrue(CollectionUtils.elementsEqual(iterator1, iterator2));
}

@Test
public void testEqualsDifferentSizesIterators() {
Iterator<Integer> iterator1 = Arrays.asList(1, 2).iterator();
Iterator<Integer> iterator2 = Arrays.asList(1, 2, 3).iterator();
assertFalse(CollectionUtils.elementsEqual(iterator1, iterator2));
}
}