Skip to content

Commit

Permalink
#18: AnyOf
Browse files Browse the repository at this point in the history
  • Loading branch information
yegor256 committed May 25, 2017
1 parent 4cb4dfd commit efbacbf
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 4 deletions.
4 changes: 1 addition & 3 deletions src/main/java/org/cactoos/func/ProcAsFunc.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,7 @@
* <pre> new AllOf(
* new TransformedIterable&lt;String&gt;(
* Arrays.asList("hello", "world"),
* new ConsumerAsFunction(
* i -> System.out.println(i)
* )
* new ProcAsFunc(i -> System.out.println(i))
* )
* ).asValue();</pre>
*
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/cactoos/list/AllOf.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
* <pre> new AllOf(
* new TransformedIterable&lt;String&gt;(
* Arrays.asList("hello", "world"),
* new Consumer() i -> System.out.println(i)
* new ProcAsFunc(i -> System.out.println(i))
* )
* ).asValue();</pre>
*
Expand Down
64 changes: 64 additions & 0 deletions src/main/java/org/cactoos/list/AnyOf.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/**
* The MIT License (MIT)
*
* Copyright (c) 2017 Yegor Bugayenko
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.cactoos.list;

import org.cactoos.Scalar;

/**
* Is {@code true} when any item in the collection is {@code true}.
*
* <p>There is no thread-safety guarantee.
*
* @author Yegor Bugayenko ([email protected])
* @version $Id$
* @since 0.1
*/
public final class AnyOf implements Scalar<Boolean> {

/**
* Iterable.
*/
private final Iterable<Boolean> iterable;

/**
* Ctor.
* @param src Source iterable
*/
public AnyOf(final Iterable<Boolean> src) {
this.iterable = src;
}

@Override
public Boolean asValue() {
boolean success = false;
for (final Boolean item : this.iterable) {
if (item) {
success = true;
break;
}
}
return success;
}

}
54 changes: 54 additions & 0 deletions src/test/java/org/cactoos/list/AnyOfTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/**
* The MIT License (MIT)
*
* Copyright (c) 2017 Yegor Bugayenko
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.cactoos.list;

import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.Test;

/**
* Test case for {@link AnyOf}.
* @author Yegor Bugayenko ([email protected])
* @version $Id$
* @since 0.1
*/
public final class AnyOfTest {

/**
* AnyOf can test any item in the list.
*/
@Test
public void iteratesList() {
MatcherAssert.assertThat(
new AnyOf(
new TransformedIterable<>(
new ArrayAsIterable<>("a", "file", "is", "corrupt"),
txt -> txt.length() > 2
)
).asValue(),
Matchers.equalTo(true)
);
}

}

0 comments on commit efbacbf

Please sign in to comment.