Skip to content

Commit

Permalink
#18: Proc, ProcAsFunc, AllOf
Browse files Browse the repository at this point in the history
  • Loading branch information
yegor256 committed May 25, 2017
1 parent 90198b4 commit 4cb4dfd
Show file tree
Hide file tree
Showing 6 changed files with 305 additions and 2 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,12 +134,12 @@ new IterableAsCollection<>(
To iterate a collection:

```java
new Loop(
new AllOf(
new TransformedIterable<>(
new ArrayAsIterable<>("how", "are", "you"),
i -> System.out.printf("Item: %s\n", i)
)
).run();
).asValue();
```

To sort a list of words in the file:
Expand Down
47 changes: 47 additions & 0 deletions src/main/java/org/cactoos/Proc.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/**
* 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;

import java.io.IOException;

/**
* Procedure.
*
* <p>There is no thread-safety guarantee.
*
* @author Yegor Bugayenko ([email protected])
* @version $Id$
* @param <X> Type of input
* @since 0.1
*/
public interface Proc<X> {

/**
* Apply it.
* @param input The argument
* @throws IOException If fails
*/
void apply(X input) throws IOException;

}
87 changes: 87 additions & 0 deletions src/main/java/org/cactoos/func/ProcAsFunc.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/**
* 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.func;

import java.io.IOException;
import org.cactoos.Func;
import org.cactoos.Proc;

/**
* Consumer that is a func that returns {@code boolean}.
*
* <p>You may need this class when you iterate a collection, for example:</p>
*
* <pre> new AllOf(
* new TransformedIterable&lt;String&gt;(
* Arrays.asList("hello", "world"),
* new ConsumerAsFunction(
* i -> System.out.println(i)
* )
* )
* ).asValue();</pre>
*
* <p>There is no thread-safety guarantee.
*
* @author Yegor Bugayenko ([email protected])
* @version $Id$
* @param <T> Type of items
* @since 0.1
*/
public final class ProcAsFunc<T> implements Func<T, Boolean> {

/**
* Proc.
*/
private final Proc<T> proc;

/**
* The result to return.
*/
private final Boolean result;

/**
* Ctor.
* @param cns Consumer
*/
public ProcAsFunc(final Proc<T> cns) {
this(cns, true);
}

/**
* Ctor.
* @param cns Consumer
* @param rslt Result to return
*/
public ProcAsFunc(final Proc<T> cns, final Boolean rslt) {
this.proc = cns;
this.result = rslt;
}

@Override
public Boolean apply(final T input) throws IOException {
this.proc.apply(input);
return this.result;
}

}
32 changes: 32 additions & 0 deletions src/main/java/org/cactoos/func/package-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* 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.
*/

/**
* Functions and procedures.
*
* @author Yegor Bugayenko ([email protected])
* @version $Id$
* @since 0.1
*/
package org.cactoos.func;
76 changes: 76 additions & 0 deletions src/main/java/org/cactoos/list/AllOf.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/**
* 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 all items in the collection are {@code true}.
*
* <p>You can use this class in order to iterate all items
* in the collection. This is very similar to the {@code forEach()}
* in steams, but more object oriented. For example, if you want
* to print all items from the array:</p>
*
* <pre> new AllOf(
* new TransformedIterable&lt;String&gt;(
* Arrays.asList("hello", "world"),
* new Consumer() i -> System.out.println(i)
* )
* ).asValue();</pre>
*
* <p>There is no thread-safety guarantee.
*
* @author Yegor Bugayenko ([email protected])
* @version $Id$
* @since 0.1
*/
public final class AllOf implements Scalar<Boolean> {

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

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

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

}
61 changes: 61 additions & 0 deletions src/test/java/org/cactoos/list/AllOfTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/**
* 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 java.util.LinkedList;
import java.util.List;
import org.cactoos.func.ProcAsFunc;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.Test;

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

/**
* AllOf can test all items in the list.
*/
@Test
public void iteratesList() {
final List<String> list = new LinkedList<>();
MatcherAssert.assertThat(
new AllOf(
new TransformedIterable<>(
new ArrayAsIterable<>("hello", "world"),
new ProcAsFunc<>(list::add)
)
).asValue(),
Matchers.allOf(
Matchers.equalTo(true),
Matchers.equalTo(list.size() == 2)
)
);
}

}

0 comments on commit 4cb4dfd

Please sign in to comment.