Skip to content

Commit

Permalink
sameElements
Browse files Browse the repository at this point in the history
  • Loading branch information
orionll committed Sep 22, 2013
1 parent aa44e33 commit da35bca
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/main/java/com/github/xtension/IterableExtensions.xtend
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,13 @@ final class IterableExtensions {
}
}

/**
* Checks if the other iterable contains the same elements in the same order as this iterable.
*/
def static <T> boolean sameElements(Iterable<T> iterable, Iterable<T> other) {
iterable.iterator.sameElements(other.iterator)
}

/**
* Sums up the elements of this iterable.
*/
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/com/github/xtension/IteratorExtensions.xtend
Original file line number Diff line number Diff line change
Expand Up @@ -568,6 +568,19 @@ final class IteratorExtensions {
}
}

/**
* Checks if the other iterator contains the same elements in the same order as this iterator.
*/
def static <T> boolean sameElements(Iterator<T> iterator, Iterator<T> other) {
while (iterator.hasNext && other.hasNext) {
if (iterator.next != other.next) {
return false
}
}

!iterator.hasNext && !other.hasNext
}

/**
* Sums up the elements of this iterator.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,15 @@ class TestIterableExtensions {
org.assertj.guava.api.Assertions::assertThat(new ArrayList<Integer>().maxByOptional[-it]).isAbsent
}

@Test
def void sameElements() {
assertThat(#[].sameElements(#[])).isTrue
assertThat(#[1,2,3].sameElements(#[1,2,3])).isTrue
assertThat(#[1,2,3].sameElements(#[1,3,2])).isFalse
assertThat(#[1,2,3].sameElements(#[1,2,3,4])).isFalse
assertThat(#[1,2,3,4].sameElements(#[1,2,3])).isFalse
}

@Test
def void sumInt() {
assertThat(#[1,2,3].sumInt).isEqualTo(6)
Expand Down

0 comments on commit da35bca

Please sign in to comment.