Skip to content

Commit

Permalink
#473 AndInThreads
Browse files Browse the repository at this point in the history
  • Loading branch information
yegor256 committed Nov 27, 2017
1 parent 25dc45d commit cc8f3ea
Show file tree
Hide file tree
Showing 3 changed files with 462 additions and 2 deletions.
294 changes: 294 additions & 0 deletions src/main/java/org/cactoos/scalar/AndInThreads.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,294 @@
/**
* 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.scalar;

import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import org.cactoos.Func;
import org.cactoos.Proc;
import org.cactoos.Scalar;
import org.cactoos.func.FuncOf;
import org.cactoos.iterable.IterableOf;
import org.cactoos.iterable.Mapped;

/**
* Logical conjunction, in multiple threads.
*
* <p>This class can be effectively used to iterate through
* a collection, just like
* {@link java.util.stream.Stream#forEach(java.util.function.Consumer)}
* works:</p>
*
* <pre> new AndInThreads(
* new IterableOf("Mary", "John", "William", "Napkin"),
* name -> System.out.printf("The name: %s\n", name)
* ).value();</pre>
*
* <p>This class implements {@link Scalar}, which throws a checked
* {@link Exception}. This may not be convenient in many cases. To make
* it more convenient and get rid of the checked exception you can
* use {@link UncheckedScalar} or {@link IoCheckedScalar} decorators.</p>
*
* <p>There is no thread-safety guarantee.
*
* @author Yegor Bugayenko (yegor256@gmail.com)
* @version $Id$
* @see UncheckedScalar
* @see IoCheckedScalar
* @since 0.25
*/
public final class AndInThreads implements Scalar<Boolean> {

/**
* The service.
*/
private final ExecutorService service;

/**
* The iterator.
*/
private final Iterable<Scalar<Boolean>> iterable;

/**
* Ctor.
* @param proc Proc to map
* @param src The iterable
* @param <X> Type of items in the iterable
*/
@SafeVarargs
public <X> AndInThreads(final Proc<X> proc, final X... src) {
this(new FuncOf<>(proc, true), src);
}

/**
* Ctor.
* @param func Func to map
* @param src The iterable
* @param <X> Type of items in the iterable
*/
@SafeVarargs
public <X> AndInThreads(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
*/
public <X> AndInThreads(final Proc<X> proc, final Iterator<X> src) {
this(new FuncOf<>(proc, true), src);
}

/**
* Ctor.
* @param src The iterable
* @param proc Proc to use
* @param <X> Type of items in the iterable
*/
public <X> AndInThreads(final Proc<X> proc, final Iterable<X> src) {
this(new FuncOf<>(proc, true), src);
}

/**
* Ctor.
* @param src The iterable
* @param func Func to map
* @param <X> Type of items in the iterable
*/
public <X> AndInThreads(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> AndInThreads(final Func<X, Boolean> func,
final Iterable<X> src) {
this(
new Mapped<>(
item -> (Scalar<Boolean>) () -> func.apply(item), src
)
);
}

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

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

/**
* Ctor.
* @param src The iterable
*/
public AndInThreads(final Iterable<Scalar<Boolean>> src) {
this(Executors.newCachedThreadPool(), src);
}

/**
* Ctor.
* @param svc Executable service to run thread in
* @param proc Proc to map
* @param src The iterable
* @param <X> Type of items in the iterable
*/
@SafeVarargs
public <X> AndInThreads(final ExecutorService svc,
final Proc<X> proc, final X... src) {
this(svc, new FuncOf<>(proc, true), src);
}

/**
* Ctor.
* @param svc Executable service to run thread in
* @param func Func to map
* @param src The iterable
* @param <X> Type of items in the iterable
*/
@SafeVarargs
public <X> AndInThreads(final ExecutorService svc,
final Func<X, Boolean> func, final X... src) {
this(svc, func, new IterableOf<>(src));
}

/**
* Ctor.
* @param svc Executable service to run thread in
* @param src The iterable
* @param proc Proc to use
* @param <X> Type of items in the iterable
*/
public <X> AndInThreads(final ExecutorService svc,
final Proc<X> proc, final Iterator<X> src) {
this(svc, new FuncOf<>(proc, true), src);
}

/**
* Ctor.
* @param svc Executable service to run thread in
* @param src The iterable
* @param proc Proc to use
* @param <X> Type of items in the iterable
*/
public <X> AndInThreads(final ExecutorService svc,
final Proc<X> proc, final Iterable<X> src) {
this(svc, new FuncOf<>(proc, true), src);
}

/**
* Ctor.
* @param svc Executable service to run thread in
* @param src The iterable
* @param func Func to map
* @param <X> Type of items in the iterable
*/
public <X> AndInThreads(final ExecutorService svc,
final Func<X, Boolean> func, final Iterator<X> src) {
this(svc, func, new IterableOf<>(src));
}

/**
* Ctor.
* @param svc Executable service to run thread in
* @param src The iterable
* @param func Func to map
* @param <X> Type of items in the iterable
*/
public <X> AndInThreads(final ExecutorService svc,
final Func<X, Boolean> func, final Iterable<X> src) {
this(
svc,
new Mapped<>(
item -> (Scalar<Boolean>) () -> func.apply(item), src
)
);
}

/**
* Ctor.
* @param svc Executable service to run thread in
* @param src The iterable
*/
@SafeVarargs
public AndInThreads(final ExecutorService svc,
final Scalar<Boolean>... src) {
this(svc, new IterableOf<>(src));
}

/**
* Ctor.
* @param svc Executable service to run thread in
* @param src The iterable
*/
public AndInThreads(final ExecutorService svc,
final Iterator<Scalar<Boolean>> src) {
this(svc, new IterableOf<>(src));
}

/**
* Ctor.
* @param svc Executable service to run thread in
* @param src The iterable
*/
public AndInThreads(final ExecutorService svc,
final Iterable<Scalar<Boolean>> src) {
this.service = svc;
this.iterable = src;
}

@Override
public Boolean value() throws Exception {
final Collection<Future<Boolean>> futures = new LinkedList<>();
for (final Scalar<Boolean> item : this.iterable) {
futures.add(this.service.submit(item::value));
}
return new And(
(Func<Future<Boolean>, Boolean>) Future::get,
futures
).value();
}

}
4 changes: 2 additions & 2 deletions src/test/java/org/cactoos/package-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@
*/

/**
* Input.NoNulls, Output.NoNulls and Func<>.NoNulls, tests.
* Tests.
*
* @author Fabricio Cabral (fabriciofx@gmail.com)
* @version $Id$
* @since 0.9
*/
package org.cactoos.io;
package org.cactoos;
Loading

0 comments on commit cc8f3ea

Please sign in to comment.