diff --git a/src/main/java/org/cactoos/scalar/And.java b/src/main/java/org/cactoos/scalar/And.java index d00007583e..90419d243a 100644 --- a/src/main/java/org/cactoos/scalar/And.java +++ b/src/main/java/org/cactoos/scalar/And.java @@ -23,6 +23,7 @@ */ package org.cactoos.scalar; +import java.util.Iterator; import org.cactoos.Func; import org.cactoos.Proc; import org.cactoos.Scalar; @@ -82,7 +83,7 @@ public final class And implements Scalar { /** * The iterator. */ - private final Iterable> iterable; + private final Iterable> origin; /** * Ctor. @@ -106,6 +107,17 @@ public And(final Func func, final X... src) { this(func, new IterableOf<>(src)); } + /** + * Ctor. + * @param src The iterable + * @param proc Proc to use + * @param Type of items in the iterable + * @since 0.24 + */ + public And(final Proc proc, final Iterator src) { + this(proc, new IterableOf<>(src)); + } + /** * Ctor. * @param src The iterable @@ -114,7 +126,7 @@ public And(final Func func, final X... src) { * @since 0.24 */ public And(final Proc proc, final Iterable src) { - this(new FuncOf<>(proc, true), src); + this(new FuncOf<>(proc, false), src); } /** @@ -124,6 +136,16 @@ public And(final Proc proc, final Iterable src) { * @param Type of items in the iterable * @since 0.24 */ + public And(final Func func, final Iterator src) { + this(func, new IterableOf<>(src)); + } + + /** + * Ctor. + * @param src The iterable + * @param func Func to map + * @param Type of items in the iterable + */ public And(final Func func, final Iterable src) { this( new Mapped<>( @@ -134,25 +156,50 @@ public And(final Func func, final Iterable src) { /** * Ctor. - * @param src The iterable + * @param subject The subject + * @param conditions Funcs to map + * @param Type of items in the iterable */ @SafeVarargs - public And(final Scalar... src) { - this(new IterableOf<>(src)); + public And(final X subject, final Func... conditions) { + this( + new Mapped<>( + item -> (Scalar) () -> item.apply(subject), + new IterableOf<>(conditions) + ) + ); } /** * Ctor. - * @param src The iterable + * @param scalar The Scalar. */ - public And(final Iterable> src) { - this.iterable = src; + @SafeVarargs + public And(final Scalar... scalar) { + this(new IterableOf<>(scalar)); + } + + /** + * Ctor. + * @param iterator The iterator. + * @since 0.24 + */ + public And(final Iterator> iterator) { + this(new IterableOf<>(iterator)); + } + + /** + * Ctor. + * @param iterable The iterable. + */ + public And(final Iterable> iterable) { + this.origin = iterable; } @Override public Boolean value() throws Exception { boolean result = true; - for (final Scalar item : this.iterable) { + for (final Scalar item : this.origin) { if (!item.value()) { result = false; break; @@ -160,5 +207,4 @@ public Boolean value() throws Exception { } return result; } - } diff --git a/src/main/java/org/cactoos/scalar/Or.java b/src/main/java/org/cactoos/scalar/Or.java index 7c6aec9562..5d27857288 100644 --- a/src/main/java/org/cactoos/scalar/Or.java +++ b/src/main/java/org/cactoos/scalar/Or.java @@ -179,11 +179,11 @@ public Or(final Scalar... scalar) { /** * Ctor. - * @param iterable The iterable. + * @param iterator The iterator. * @since 0.24 */ - public Or(final Iterator> iterable) { - this(new IterableOf<>(iterable)); + public Or(final Iterator> iterator) { + this(new IterableOf<>(iterator)); } /** diff --git a/src/test/java/org/cactoos/scalar/AndTest.java b/src/test/java/org/cactoos/scalar/AndTest.java index 934dac5321..6ec9e30c0f 100644 --- a/src/test/java/org/cactoos/scalar/AndTest.java +++ b/src/test/java/org/cactoos/scalar/AndTest.java @@ -23,26 +23,24 @@ */ package org.cactoos.scalar; -import java.util.Collections; import java.util.LinkedList; import java.util.List; import org.cactoos.Proc; import org.cactoos.Scalar; -import org.cactoos.func.FuncOf; import org.cactoos.iterable.IterableOf; -import org.cactoos.iterable.Mapped; +import org.cactoos.iterator.IteratorOf; import org.hamcrest.MatcherAssert; import org.hamcrest.Matchers; import org.junit.Test; -import org.llorllale.cactoos.matchers.MatcherOf; import org.llorllale.cactoos.matchers.ScalarHasValue; /** * Test case for {@link And}. * @since 0.8 * @checkstyle JavadocMethodCheck (500 lines) - * @checkstyle ClassDataAbstractionCouplingCheck (500 lines) + * @checkstyle MagicNumber (500 line) */ +@SuppressWarnings("PMD.TooManyMethods") public final class AndTest { @Test @@ -52,8 +50,8 @@ public void allTrue() throws Exception { new True(), new True(), new True() - ).value(), - Matchers.equalTo(true) + ), + new ScalarHasValue<>(true) ); } @@ -64,8 +62,8 @@ public void oneFalse() throws Exception { new True(), new False(), new True() - ).value(), - Matchers.equalTo(false) + ), + new ScalarHasValue<>(false) ); } @@ -78,86 +76,115 @@ public void allFalse() throws Exception { new False(), new False() ) - ).value(), - Matchers.equalTo(false) + ), + new ScalarHasValue<>(false) ); } @Test public void emptyIterator() throws Exception { MatcherAssert.assertThat( - new And(Collections.emptyList()).value(), - Matchers.equalTo(true) + new And(new IteratorOf>()), + new ScalarHasValue<>(true) ); } @Test - public void iteratesList() { - final List list = new LinkedList<>(); + public void testProcIterable() throws Exception { + final List list = new LinkedList<>(); + new And( + (Proc) list::add, + new IterableOf<>(1, 1) + ).value(); MatcherAssert.assertThat( - "Can't iterate a list with a procedure", - new And( - new Mapped>( - new FuncOf<>(list::add, () -> true), - new IterableOf<>("hello", "world") - ) - ), - new ScalarHasValue<>( - Matchers.allOf( - Matchers.equalTo(true), - new MatcherOf<>( - value -> list.size() == 2 - ) - ) - ) + list.size(), + Matchers.equalTo(1) ); } @Test - public void iteratesEmptyList() { - final List list = new LinkedList<>(); + public void testProcIterator() throws Exception { + final List list = new LinkedList<>(); + new And( + (Proc) list::add, + new IteratorOf<>(1, 1) + ).value(); MatcherAssert.assertThat( - "Can't iterate a list", - new And( - new Mapped>( - new FuncOf<>(list::add, () -> true), Collections.emptyList() - ) - ), - new ScalarHasValue<>( - Matchers.allOf( - Matchers.equalTo(true), - new MatcherOf<>( - value -> { - return list.isEmpty(); - } - ) - ) - ) + list.size(), + Matchers.equalTo(1) ); } @Test - public void testProc() throws Exception { + public void testProcVarargs() throws Exception { final List list = new LinkedList<>(); new And( (Proc) list::add, - 1, 1 + 2, 3, 4 ).value(); MatcherAssert.assertThat( list.size(), - Matchers.equalTo(2) + Matchers.equalTo(3) + ); + } + + @Test + public void testFuncIterable() throws Exception { + MatcherAssert.assertThat( + new And( + input -> input > 0, + new IterableOf<>(1, -1, 0) + ), + new ScalarHasValue<>(false) + ); + } + + @Test + public void testFuncIterator() throws Exception { + MatcherAssert.assertThat( + new And( + input -> input > 0, + new IteratorOf<>(1, -1, 0) + ), + new ScalarHasValue<>(false) + ); + } + + @Test + public void testFuncVarargs() throws Exception { + MatcherAssert.assertThat( + new And( + input -> input > 0, + -1, -2, 0 + ), + new ScalarHasValue<>(false) ); } @Test - public void testFunc() throws Exception { + public void testMultipleFuncConditionTrue() throws Exception { MatcherAssert.assertThat( + "Can't compare subject with true conditions", new And( + 3, input -> input > 0, - 1, -1, 0 - ).value(), - Matchers.equalTo(false) + input -> input > 5, + input -> input > 4 + ), + new ScalarHasValue<>(false) ); } + @Test + public void testMultipleFuncConditionFalse() throws Exception { + MatcherAssert.assertThat( + "Can't compare subject with false conditions", + new And( + "cactoos", + input -> input.contains("singleton"), + input -> input.contains("static") + ), + new ScalarHasValue<>(false) + ); + } } diff --git a/src/test/java/org/cactoos/scalar/OrTest.java b/src/test/java/org/cactoos/scalar/OrTest.java index 1adb348062..02627460f0 100644 --- a/src/test/java/org/cactoos/scalar/OrTest.java +++ b/src/test/java/org/cactoos/scalar/OrTest.java @@ -23,12 +23,12 @@ */ package org.cactoos.scalar; -import java.util.Collections; import java.util.LinkedList; import java.util.List; import org.cactoos.Proc; import org.cactoos.Scalar; import org.cactoos.iterable.IterableOf; +import org.cactoos.iterator.IteratorOf; import org.hamcrest.MatcherAssert; import org.hamcrest.Matchers; import org.junit.Test; @@ -41,6 +41,7 @@ * @checkstyle JavadocMethodCheck (500 lines) * @checkstyle MagicNumber (500 line) */ +@SuppressWarnings("PMD.TooManyMethods") public final class OrTest { @Test @@ -90,13 +91,13 @@ public void allTrue() throws Exception { @Test public void emptyIterator() throws Exception { MatcherAssert.assertThat( - new Or(Collections.emptyList()), + new Or(new IteratorOf>()), new ScalarHasValue<>(false) ); } @Test - public void testProc() throws Exception { + public void testProcIterable() throws Exception { final List list = new LinkedList<>(); new Or( (Proc) list::add, @@ -108,6 +109,19 @@ public void testProc() throws Exception { ); } + @Test + public void testProcIterator() throws Exception { + final List list = new LinkedList<>(); + new Or( + (Proc) list::add, + new IteratorOf<>(1, 2, 3, 4) + ).value(); + MatcherAssert.assertThat( + list.size(), + Matchers.equalTo(4) + ); + } + @Test public void testProcVarargs() throws Exception { final List list = new LinkedList<>(); @@ -122,7 +136,7 @@ public void testProcVarargs() throws Exception { } @Test - public void testFunc() throws Exception { + public void testFuncIterable() throws Exception { MatcherAssert.assertThat( new Or( input -> input > 0, @@ -132,6 +146,17 @@ public void testFunc() throws Exception { ); } + @Test + public void testFuncIterator() throws Exception { + MatcherAssert.assertThat( + new Or( + input -> input > 0, + new IteratorOf<>(-1, 1, 0) + ), + new ScalarHasValue<>(true) + ); + } + @Test public void testFuncVarargs() throws Exception { MatcherAssert.assertThat(