Java Code Examples for org.cactoos.Func#apply()

The following examples show how to use org.cactoos.Func#apply() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: StickyFuncTest.java    From cactoos with MIT License 6 votes vote down vote up
@Test
public void cachesWithLimitedBuffer() throws Exception {
    final Func<Integer, Integer> func = new StickyFunc<>(
        input -> new SecureRandom().nextInt(), 2
    );
    final int first = func.apply(0);
    final int second = func.apply(1);
    MatcherAssert.assertThat(
        first + second,
        Matchers.equalTo(func.apply(0) + func.apply(1))
    );
    final int third = func.apply(-1);
    MatcherAssert.assertThat(
        second + third,
        Matchers.equalTo(func.apply(1) + func.apply(-1))
    );
}
 
Example 2
Source File: ItemAt.java    From cactoos with MIT License 5 votes vote down vote up
/**
 * Ctor.
 *
 * @param position Position
 * @param fallback Fallback value
 * @param iterable Iterable
 */
public ItemAt(
    final int position,
    final Func<Iterable<T>, T> fallback,
    final Iterable<T> iterable
) {
    this.saved = new Sticky<T>(
        () -> {
            final T ret;
            if (position < 0) {
                throw new IOException(
                    new FormattedText(
                        "The position must be non-negative: %d",
                        position
                    ).asString()
                );
            }
            final Iterator<T> src = iterable.iterator();
            int cur;
            for (cur = 0; cur < position && src.hasNext(); ++cur) {
                src.next();
            }
            if (cur == position && src.hasNext()) {
                ret = src.next();
            } else {
                ret = fallback.apply(() -> src);
            }
            return ret;
        }
    );
}
 
Example 3
Source File: AndWithIndex.java    From cactoos with MIT License 5 votes vote down vote up
@Override
public Boolean value() throws Exception {
    boolean result = true;
    int pos = 0;
    for (final Func<Integer, Boolean> item : this.iterable) {
        if (!item.apply(pos)) {
            result = false;
            break;
        }
        ++pos;
    }
    return result;
}
 
Example 4
Source File: AndInThreads.java    From cactoos with MIT License 5 votes vote down vote up
/**
 * 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
 */
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
        )
    );
}
 
Example 5
Source File: Strict.java    From cactoos with MIT License 5 votes vote down vote up
/**
 * Ctor.
 * @param predicate The Func as a predicate
 * @param origin The Text
 */
public Strict(final Func<String, Boolean> predicate, final Text origin) {
    super((Scalar<String>) () -> {
        final String str = origin.asString();
        if (!predicate.apply(str)) {
            throw new IllegalArgumentException(
                new FormattedText(
                    "String '%s' does not match a given predicate",
                    str
                ).asString()
            );
        }
        return str;
    });
}
 
Example 6
Source File: Chained.java    From cactoos with MIT License 5 votes vote down vote up
@Override
public Z apply(final X input) throws Exception {
    Y temp = this.before.apply(input);
    for (final Func<Y, Y> func : this.funcs) {
        temp = func.apply(temp);
    }
    return this.after.apply(temp);
}
 
Example 7
Source File: AndInThreads.java    From cactoos with MIT License 5 votes vote down vote up
/**
 * Ctor.
 * @param func Func to map
 * @param src The iterable
 * @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
        )
    );
}
 
Example 8
Source File: StickyFunc.java    From cactoos with MIT License 5 votes vote down vote up
/**
 * Ctor.
 * @param fnc Func original
 * @param max Maximum cache size
 * @since 0.26
 */
public StickyFunc(final Func<X, Y> fnc, final int max) {
    this.func = new StickyBiFunc<>(
        (first, second) -> fnc.apply(first),
        max
    );
}
 
Example 9
Source File: Or.java    From cactoos with MIT License 5 votes vote down vote up
/**
 * Ctor.
 * @param func Func to map
 * @param src The iterable
 * @param <X> Type of items in the iterable
 */
public <X> Or(final Func<X, Boolean> func, final Iterable<X> src) {
    this(
        new Mapped<>(
            item -> (Scalar<Boolean>) () -> func.apply(item), src
        )
    );
}
 
Example 10
Source File: Sticky.java    From cactoos with MIT License 3 votes vote down vote up
/**
 * Ctor.
 * @param key Func to create key
 * @param value Func to create value
 * @param map The map to extend
 * @param list List of items
 * @param <Z> Type of items in the list
 * @since 0.12
 * @checkstyle ParameterNumberCheck (5 lines)
 */
public <Z> Sticky(final Func<Z, X> key,
    final Func<Z, Y> value, final Map<X, Y> map,
    final Iterable<Z> list) {
    this(
        item -> new MapEntry<>(key.apply(item), value.apply(item)),
        map, list
    );
}
 
Example 11
Source File: MapOf.java    From cactoos with MIT License 3 votes vote down vote up
/**
 * Ctor.
 * @param key Func to create key
 * @param value Func to create value
 * @param src The map to extend
 * @param list List of items
 * @param <Z> Type of items in the list
 * @since 0.12
 * @checkstyle ParameterNumberCheck (5 lines)
 */
public <Z> MapOf(final Func<Z, X> key,
    final Func<Z, Y> value, final Map<X, Y> src,
    final Iterable<Z> list) {
    this(
        item -> new MapEntry<>(key.apply(item), value.apply(item)),
        src, list
    );
}
 
Example 12
Source File: Synced.java    From cactoos with MIT License 3 votes vote down vote up
/**
 * Ctor.
 * @param key Func to create key
 * @param value Func to create value
 * @param map The map to extend
 * @param list List of items
 * @param <Z> Type of items in the list
 * @checkstyle ParameterNumberCheck (5 lines)
 */
public <Z> Synced(final Func<Z, X> key,
    final Func<Z, Y> value, final Map<X, Y> map,
    final Iterable<Z> list) {
    this(
        item -> new MapEntry<>(key.apply(item), value.apply(item)),
        map, list
    );
}
 
Example 13
Source File: Ternary.java    From cactoos with MIT License 3 votes vote down vote up
/**
 * Ctor.
 * @param input The input to pass to all of them
 * @param cnd The condition
 * @param cons The consequent
 * @param alter The alternative
 * @param <X> Type of input
 * @since 0.9
 * @checkstyle ParameterNumberCheck (5 lines)
 */
public <X> Ternary(final X input, final Func<X, Boolean> cnd,
    final Func<X, T> cons, final Func<X, T> alter) {
    this(
        () -> cnd.apply(input),
        () -> cons.apply(input),
        () -> alter.apply(input)
    );
}
 
Example 14
Source File: BiFuncOf.java    From cactoos with MIT License 2 votes vote down vote up
/**
 * Ctor.
 * @param fnc The func
 */
public BiFuncOf(final Func<X, Z> fnc) {
    this((first, second) -> fnc.apply(first));
}
 
Example 15
Source File: ResourceOf.java    From cactoos with MIT License 2 votes vote down vote up
/**
 * New resource input with specified {@link ClassLoader}.
 * @param res Resource name
 * @param fbk Fallback
 * @param ldr Resource class loader
 */
public ResourceOf(final CharSequence res,
    final Func<CharSequence, Input> fbk, final ClassLoader ldr) {
    this(new TextOf(res), input -> fbk.apply(input.asString()), ldr);
}
 
Example 16
Source File: CallableOf.java    From cactoos with MIT License 2 votes vote down vote up
/**
 * Ctor.
 * @param fnc Encapsulated func
 * @param ipt Input
 * @since 0.41
 */
public CallableOf(final Func<X, T> fnc, final X ipt) {
    this(() -> fnc.apply(ipt));
}
 
Example 17
Source File: MapOf.java    From cactoos with MIT License 2 votes vote down vote up
/**
 * Ctor.
 * @param key Func to create key
 * @param value Func to create value
 * @param list List of items
 * @param <Z> Type of items in the list
 * @since 0.11
 */
public <Z> MapOf(final Func<Z, X> key,
    final Func<Z, Y> value, final Iterable<Z> list) {
    this(item -> new MapEntry<>(key.apply(item), value.apply(item)), list);
}
 
Example 18
Source File: Solid.java    From cactoos with MIT License 2 votes vote down vote up
/**
 * Ctor.
 * @param key Func to create key
 * @param value Func to create value
 * @param list List of items
 * @param <Z> Type of items in the list
 */
public <Z> Solid(final Func<Z, X> key,
    final Func<Z, Y> value, final Iterable<Z> list) {
    this(item -> new MapEntry<>(key.apply(item), value.apply(item)), list);
}
 
Example 19
Source File: Sticky.java    From cactoos with MIT License 2 votes vote down vote up
/**
 * Ctor.
 * @param key Func to create key
 * @param value Func to create value
 * @param list List of items
 * @param <Z> Type of items in the list
 * @since 0.11
 */
public <Z> Sticky(final Func<Z, X> key,
    final Func<Z, Y> value, final Iterable<Z> list) {
    this(item -> new MapEntry<>(key.apply(item), value.apply(item)), list);
}
 
Example 20
Source File: Synced.java    From cactoos with MIT License 2 votes vote down vote up
/**
 * Ctor.
 * @param list List of items
 * @param key Func to create key
 * @param value Func to create value
 * @param <Z> Type of items in the list
 */
public <Z> Synced(final Iterable<Z> list, final Func<Z, X> key,
    final Func<Z, Y> value) {
    this(item -> new MapEntry<>(key.apply(item), value.apply(item)), list);
}