Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#18: AnyOf, AllOf #19

Merged
merged 4 commits into from
May 26, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,12 +134,23 @@ 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)
new ProcAsFunc<>(
i -> System.out.printf("Item: %s\n", i)
)
)
).run();
).asValue();
```

Or even more compact:

```java
new IterableAsBoolean(
new ArrayAsIterable<>("how", "are", "you"),
i -> System.out.printf("Item: %s\n", i)
).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;

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

/**
* Func that always returns the same result
*
* <p>There is no thread-safety guarantee.
*
* @author Yegor Bugayenko ([email protected])
* @version $Id$
* @param <X> Type of input
* @param <Y> Type of output
* @since 0.1
*/
public final class ConstFunc<X, Y> implements Func<X, Y> {

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

/**
* Ctor.
* @param rslt What to return
*/
public ConstFunc(final Y rslt) {
this.result = rslt;
}

@Override
public Y apply(final X input) {
return this.result;
}

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

/**
* Proc that does nothing.
*
* <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 DeadProc<T> implements Proc<T> {

@Override
public void apply(final T input) {
// nothing to do
}

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

/**
* Proc that is a {@link 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 ProcAsFunc(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;
92 changes: 92 additions & 0 deletions src/main/java/org/cactoos/list/AllOf.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/**
* 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 ProcAsFunc&lt;&gt;(i -> System.out.println(i))
* )
* ).asValue();</pre>
*
* <p>Or even more compact, through {@link IterableAsBooleans}:</p>
*
* <pre> new AllOf(
* new IterableAsBooleans&lt;String&gt;(
* Arrays.asList("hello", "world"),
* i -> System.out.println(i)
* )
* ).asValue();</pre>
*
* <p>Or you can even use {@link IterableAsBoolean}:</p>
*
* <pre> new IterableAsBoolean&lt;String&gt;(
* Arrays.asList("hello", "world"),
* 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;
}

}
Loading