Skip to content

Commit

Permalink
#754 Align And and Or ctors
Browse files Browse the repository at this point in the history
  • Loading branch information
pbenety committed May 15, 2018
1 parent 80ed872 commit 10f8850
Show file tree
Hide file tree
Showing 4 changed files with 170 additions and 72 deletions.
66 changes: 56 additions & 10 deletions src/main/java/org/cactoos/scalar/And.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
*/
package org.cactoos.scalar;

import java.util.Iterator;
import org.cactoos.Func;
import org.cactoos.Proc;
import org.cactoos.Scalar;
Expand Down Expand Up @@ -82,7 +83,7 @@ public final class And implements Scalar<Boolean> {
/**
* The iterator.
*/
private final Iterable<Scalar<Boolean>> iterable;
private final Iterable<Scalar<Boolean>> origin;

/**
* Ctor.
Expand All @@ -106,6 +107,17 @@ public <X> And(final Func<X, Boolean> func, final X... src) {
this(func, new IterableOf<>(src));
}

/**
* Ctor.
* @param src The iterable
* @param proc Proc to use
* @param <X> Type of items in the iterable
* @since 0.24
*/
public <X> And(final Proc<X> proc, final Iterator<X> src) {
this(proc, new IterableOf<>(src));
}

/**
* Ctor.
* @param src The iterable
Expand All @@ -114,7 +126,7 @@ public <X> And(final Func<X, Boolean> func, final X... src) {
* @since 0.24
*/
public <X> And(final Proc<X> proc, final Iterable<X> src) {
this(new FuncOf<>(proc, true), src);
this(new FuncOf<>(proc, false), src);
}

/**
Expand All @@ -124,6 +136,16 @@ public <X> And(final Proc<X> proc, final Iterable<X> src) {
* @param <X> Type of items in the iterable
* @since 0.24
*/
public <X> And(final Func<X, Boolean> func, final Iterator<X> src) {
this(func, new IterableOf<>(src));
}

/**
* Ctor.
* @param src The iterable
* @param func Func to map
* @param <X> Type of items in the iterable
*/
public <X> And(final Func<X, Boolean> func, final Iterable<X> src) {
this(
new Mapped<>(
Expand All @@ -134,31 +156,55 @@ public <X> And(final Func<X, Boolean> func, final Iterable<X> src) {

/**
* Ctor.
* @param src The iterable
* @param subject The subject
* @param conditions Funcs to map
* @param <X> Type of items in the iterable
*/
@SafeVarargs
public And(final Scalar<Boolean>... src) {
this(new IterableOf<>(src));
public <X> And(final X subject, final Func<X, Boolean>... conditions) {
this(
new Mapped<>(
item -> (Scalar<Boolean>) () -> item.apply(subject),
new IterableOf<>(conditions)
)
);
}

/**
* Ctor.
* @param src The iterable
* @param scalar The Scalar.
*/
public And(final Iterable<Scalar<Boolean>> src) {
this.iterable = src;
@SafeVarargs
public And(final Scalar<Boolean>... scalar) {
this(new IterableOf<>(scalar));
}

/**
* Ctor.
* @param iterator The iterator.
* @since 0.24
*/
public And(final Iterator<Scalar<Boolean>> iterator) {
this(new IterableOf<>(iterator));
}

/**
* Ctor.
* @param iterable The iterable.
*/
public And(final Iterable<Scalar<Boolean>> iterable) {
this.origin = iterable;
}

@Override
public Boolean value() throws Exception {
boolean result = true;
for (final Scalar<Boolean> item : this.iterable) {
for (final Scalar<Boolean> item : this.origin) {
if (!item.value()) {
result = false;
break;
}
}
return result;
}

}
6 changes: 3 additions & 3 deletions src/main/java/org/cactoos/scalar/Or.java
Original file line number Diff line number Diff line change
Expand Up @@ -179,11 +179,11 @@ public Or(final Scalar<Boolean>... scalar) {

/**
* Ctor.
* @param iterable The iterable.
* @param iterator The iterator.
* @since 0.24
*/
public Or(final Iterator<Scalar<Boolean>> iterable) {
this(new IterableOf<>(iterable));
public Or(final Iterator<Scalar<Boolean>> iterator) {
this(new IterableOf<>(iterator));
}

/**
Expand Down
137 changes: 82 additions & 55 deletions src/test/java/org/cactoos/scalar/AndTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -52,8 +50,8 @@ public void allTrue() throws Exception {
new True(),
new True(),
new True()
).value(),
Matchers.equalTo(true)
),
new ScalarHasValue<>(true)
);
}

Expand All @@ -64,8 +62,8 @@ public void oneFalse() throws Exception {
new True(),
new False(),
new True()
).value(),
Matchers.equalTo(false)
),
new ScalarHasValue<>(false)
);
}

Expand All @@ -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<Scalar<Boolean>>()),
new ScalarHasValue<>(true)
);
}

@Test
public void iteratesList() {
final List<String> list = new LinkedList<>();
public void testProcIterable() throws Exception {
final List<Integer> list = new LinkedList<>();
new And(
(Proc<Integer>) list::add,
new IterableOf<>(1, 1)
).value();
MatcherAssert.assertThat(
"Can't iterate a list with a procedure",
new And(
new Mapped<String, Scalar<Boolean>>(
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<String> list = new LinkedList<>();
public void testProcIterator() throws Exception {
final List<Integer> list = new LinkedList<>();
new And(
(Proc<Integer>) list::add,
new IteratorOf<>(1, 1)
).value();
MatcherAssert.assertThat(
"Can't iterate a list",
new And(
new Mapped<String, Scalar<Boolean>>(
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<Integer> list = new LinkedList<>();
new And(
(Proc<Integer>) 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)
);
}
}
Loading

0 comments on commit 10f8850

Please sign in to comment.