org.cactoos.scalar.And Maven / Gradle / Ivy
Show all versions of cactoos Show documentation
/**
* The MIT License (MIT)
*
* Copyright (c) 2017-2018 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 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.
*
* This class can be effectively used to iterate through
* a collection, just like
* {@link java.util.stream.Stream#forEach(java.util.function.Consumer)}
* works:
*
*
* new And(
* new ProcOf<>(input -> System.out.printf("\'%s\' ", input) ),
* new IterableOf<>("Mary", "John", "William", "Napkin")
* ).value(); // will print 'Mary' 'John' 'William' 'Napkin' to standard output
*
*
* 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.
*
* There is no thread-safety guarantee.
*
* @author Vseslav Sekorin ([email protected])
* @version $Id: e6b0050e784f3078d9e6554e186c9154a48390e5 $
* @see UncheckedScalar
* @see IoCheckedScalar
* @since 0.8
*/
public final class And implements Scalar {
/**
* The iterator.
*/
private final Iterable> iterable;
/**
* Ctor.
* @param proc Proc to map
* @param src The iterable
* @param Type of items in the iterable
*/
@SafeVarargs
public And(final Proc proc, final X... src) {
this(new FuncOf<>(proc, true), src);
}
/**
* Ctor.
* @param func Func to map
* @param src The iterable
* @param Type of items in the iterable
*/
@SafeVarargs
public And(final Func func, final X... src) {
this(func, new IterableOf<>(src));
}
/**
* Ctor.
* @param src The iterable
* @param proc Proc to use
* @param Type of items in the iterable
* @since 0.24
*/
public And(final Proc proc, final Iterable src) {
this(new FuncOf<>(proc, true), src);
}
/**
* Ctor.
* @param src The iterable
* @param func Func to map
* @param Type of items in the iterable
* @since 0.24
*/
public And(final Func func, final Iterable src) {
this(
new Mapped<>(
item -> (Scalar) () -> func.apply(item), src
)
);
}
/**
* Ctor.
* @param src The iterable
*/
@SafeVarargs
public And(final Scalar... src) {
this(new IterableOf<>(src));
}
/**
* Ctor.
* @param src The iterable
*/
public And(final Iterable> src) {
this.iterable = src;
}
@Override
public Boolean value() throws Exception {
boolean result = true;
for (final Scalar item : this.iterable) {
if (!item.value()) {
result = false;
break;
}
}
return result;
}
}