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 16, 2018
2 parents 0564dd8 + 91a5b06 commit 30ef8bf
Show file tree
Hide file tree
Showing 4 changed files with 176 additions and 76 deletions.
66 changes: 57 additions & 9 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 iterator
* @param proc Proc to use
* @param <X> Type of items in the iterator
* @since 0.34
*/
public <X> And(final Proc<X> proc, final Iterator<X> src) {
this(proc, new IterableOf<>(src));
}

/**
* Ctor.
* @param src The iterable
Expand All @@ -117,6 +129,17 @@ public <X> And(final Proc<X> proc, final Iterable<X> src) {
this(new FuncOf<>(proc, true), src);
}

/**
* Ctor.
* @param src The iterator
* @param func Func to map
* @param <X> Type of items in the iterator
* @since 0.34
*/
public <X> And(final Func<X, Boolean> func, final Iterator<X> src) {
this(func, new IterableOf<>(src));
}

/**
* Ctor.
* @param src The iterable
Expand All @@ -134,31 +157,56 @@ 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
* @since 0.34
*/
@SafeVarargs
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 scalar The Scalar.
*/
@SafeVarargs
public And(final Scalar<Boolean>... src) {
this(new IterableOf<>(src));
public And(final Scalar<Boolean>... scalar) {
this(new IterableOf<>(scalar));
}

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

/**
* Ctor.
* @param iterable The iterable.
*/
public And(final Iterable<Scalar<Boolean>> src) {
this.iterable = src;
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
139 changes: 83 additions & 56 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,
Matchers.contains(1, 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,
Matchers.contains(1, 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)
list,
Matchers.contains(2, 3, 4)
);
}

@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 testFunc() throws Exception {
public void testFuncVarargs() throws Exception {
MatcherAssert.assertThat(
new And(
input -> input > 0,
1, -1, 0
).value(),
Matchers.equalTo(false)
-1, -2, 0
),
new ScalarHasValue<>(false)
);
}

@Test
public void testMultipleFuncConditionTrue() throws Exception {
MatcherAssert.assertThat(
"Can't compare subject with true conditions",
new And(
3,
input -> input > 0,
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 30ef8bf

Please sign in to comment.