Skip to content

Commit

Permalink
Merge branch '__rultor'
Browse files Browse the repository at this point in the history
  • Loading branch information
rultor committed May 3, 2020
2 parents ab8107d + 0603544 commit 9b4627e
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 7 deletions.
23 changes: 18 additions & 5 deletions src/main/java/org/cactoos/iterable/IterableOf.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,14 @@
import org.cactoos.func.UncheckedFunc;
import org.cactoos.iterator.IteratorOf;
import org.cactoos.scalar.And;
import org.cactoos.scalar.FallbackFrom;
import org.cactoos.scalar.False;
import org.cactoos.scalar.Folded;
import org.cactoos.scalar.Or;
import org.cactoos.scalar.ScalarWithFallback;
import org.cactoos.scalar.Sticky;
import org.cactoos.scalar.SumOfInt;
import org.cactoos.scalar.True;
import org.cactoos.scalar.Unchecked;
import org.cactoos.text.TextOf;
import org.cactoos.text.UncheckedText;
Expand Down Expand Up @@ -146,6 +150,7 @@ public Iterator<X> iterator() {

@Override
@edu.umd.cs.findbugs.annotations.SuppressFBWarnings("EQ_UNUSUAL")
@SuppressWarnings (value = "unchecked")
public boolean equals(final Object other) {
return new Unchecked<>(
new Or(
Expand All @@ -154,12 +159,20 @@ public boolean equals(final Object other) {
() -> other != null,
() -> Iterable.class.isAssignableFrom(other.getClass()),
() -> {
final Iterable<?> compared = (Iterable<?>) other;
final Iterator<?> iterator = compared.iterator();
return new Unchecked<>(
final Iterable<X> compared = (Iterable<X>) other;
return new ScalarWithFallback<>(
new And(
(X input) -> input.equals(iterator.next()),
this
(X value) -> true,
new Matched<>(
this,
compared
)
),
new IterableOf<>(
new FallbackFrom<>(
IllegalStateException.class,
ex -> false
)
)
).value();
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/cactoos/iterable/Matched.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ public Matched(
final Iterator<X> ftr = fst.iterator();
final Iterator<X> str = snd.iterator();
final List<X> rslt = new LinkedList<>();
while (ftr.hasNext()) {
if (!str.hasNext()) {
while (ftr.hasNext() || str.hasNext()) {
if (!str.hasNext() || !ftr.hasNext()) {
throw new IllegalStateException(
"Size mismatch of iterators"
);
Expand Down
19 changes: 19 additions & 0 deletions src/test/java/org/cactoos/iterable/IterableOfTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.hamcrest.core.IsNot;
import org.junit.Test;
import org.llorllale.cactoos.matchers.Assertion;
import org.llorllale.cactoos.matchers.IsTrue;

/**
* Test case for {@link IterableOf}.
Expand Down Expand Up @@ -105,6 +106,24 @@ public void containAllPagedContentInOrder() throws Exception {
);
}

@Test
public void isNotEqualsToIterableWithMoreElements() {
new Assertion<>(
"Must compare iterables and second one is bigger",
new IterableOf<>("a", "b").equals(new IterableOf<>("a")),
new IsNot<>(new IsTrue())
).affirm();
}

@Test
public void isNotEqualsToIterableWithLessElements() {
new Assertion<>(
"Must compare iterables and first one is bigger",
new IterableOf<>("a").equals(new IterableOf<>("a", "b")),
new IsNot<>(new IsTrue())
).affirm();
}

@Test
@SuppressWarnings("unchecked")
public void reportTotalPagedLength() throws Exception {
Expand Down
28 changes: 28 additions & 0 deletions src/test/java/org/cactoos/iterable/MatchedTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
import org.hamcrest.Matchers;
import org.junit.Assert;
import org.junit.Test;
import org.llorllale.cactoos.matchers.Assertion;
import org.llorllale.cactoos.matchers.Throws;

/**
* Test case for {@link Matched}.
Expand All @@ -51,6 +53,32 @@ public void iterator() {
);
}

@Test
public void noCorrelationWithBiggerSecondIterable() throws IllegalStateException {
new Assertion<>(
"All elements have correlation function as `endsWith`",
() -> new Matched<>(
(fst, snd) -> fst.endsWith("elem") && snd.endsWith("elem"),
new IterableOf<>("1st elem", "2nd elem"),
new IterableOf<>("`A` elem", "`B` elem", "'C' elem")
).iterator(),
new Throws<>(IllegalStateException.class)
).affirm();
}

@Test
public void noCorrelationWithSmallerSecondIterable() throws IllegalStateException {
new Assertion<>(
"All elements have correlation function as `endsWith`",
() -> new Matched<>(
(fst, snd) -> fst.endsWith("elem") && snd.endsWith("elem"),
new IterableOf<>("1st elem", "2nd elem", "3rd elem"),
new IterableOf<>("`A` elem", "`B` elem")
).iterator(),
new Throws<>(IllegalStateException.class)
).affirm();
}

@Test
public void endsWith() {
MatcherAssert.assertThat(
Expand Down

0 comments on commit 9b4627e

Please sign in to comment.