Example usage for java.util.function BiFunction apply

List of usage examples for java.util.function BiFunction apply

Introduction

In this page you can find the example usage for java.util.function BiFunction apply.

Prototype

R apply(T t, U u);

Source Link

Document

Applies this function to the given arguments.

Usage

From source file:org.briljantframework.data.Na.java

/**
 * Returns a {@code BiFunction} that ignores {@code NA} values by returning {@code NA} when either
 * the {@code left} or {@code right} value is {@code NA}.
 *
 * <p>/*from   w  ww . j  av a 2s.com*/
 * Example:
 * 
 * <pre>
 * {
 *   &#064;code
 *   BiFunction&lt;Integer, Integer, Integer&gt; adder = Na.ignore(Integer::sum);
 *   adder(null, 1); // null
 *   adder(null, null); // null
 *   adder(1, null); // null
 *   adder(1, 1); // 2
 * }
 * </pre>
 *
 * @param combine the function to apply for non-{@code NA} values
 * @param <T> the input type
 * @param <R> the output type
 * @return a {@code NA} safe {@code BiFunction}
 */
public static <T, R> BiFunction<T, T, R> ignore(BiFunction<T, T, R> combine) {
    return (a, b) -> {
        boolean aNA = Is.NA(a);
        boolean bNA = Is.NA(b);
        if (aNA || bNA) {
            return null;
        } else {
            return combine.apply(a, b);
        }
    };
}

From source file:at.gridtec.lambda4j.function.bi.BiFunction2.java

/**
 * Lifts a partial {@link BiFunction} into a total {@link BiFunction2} that returns an {@link Optional} result.
 *
 * @param <T> The type of the first argument to the function
 * @param <U> The type of the second argument to the function
 * @param <R> The type of return value from the function
 * @param partial A function that is only defined for some values in its domain
 * @return A partial {@code BiFunction} lifted into a total {@code BiFunction2} that returns an {@code Optional}
 * result./*from  w  w  w. j  a va2s.  co m*/
 * @throws NullPointerException If given argument is {@code null}
 */
@Nonnull
static <T, U, R> BiFunction2<T, U, Optional<R>> lift(
        @Nonnull final BiFunction<? super T, ? super U, ? extends R> partial) {
    Objects.requireNonNull(partial);
    return (t, u) -> Optional.ofNullable(partial.apply(t, u));
}

From source file:org.briljantframework.data.Na.java

/**
 * Returns a {@code BiFunction} that ignores {@code NA} values by returning {@code NA} when both
 * the {@code left} or {@code right} value is {@code NA} and fill the {@code left} or
 * {@code right} value when either is {@code NA}.
 *
 * <p>//from  w w w  .j a  v  a2  s .c  o  m
 * For example:
 * 
 * <pre>
 * {@code
 *  BiFunction<Integer, Integer, Integer> adder = Na.ignore((a, b) -> a + b), 10);
 *  adder(null, 1);    // 11
 *  adder(1, null);    // 11
 *  adder(null, null); // NA
 *  adder(1, 1);       // 2
 * }
 * </pre>
 *
 * @param combine the function to apply for non-{@code NA} values
 * @param <T> the input type
 * @param <R> the output type
 * @return a {@code NA} safe {@code BiFunction}
 */
public static <T, R> BiFunction<T, T, R> ignore(BiFunction<T, T, R> combine, T fillValue) {
    return (a, b) -> {
        boolean aNA = Is.NA(a);
        boolean bNA = Is.NA(b);
        if (aNA && bNA) {
            return null;
        } else {
            if (aNA) {
                return combine.apply(fillValue, b);
            } else if (bNA) {
                return combine.apply(a, fillValue);
            } else {
                return combine.apply(a, b);
            }
        }
    };
}

From source file:com.facebook.presto.operator.aggregation.AggregationTestUtils.java

private static void assertFunctionEquals(BiFunction<Object, Object, Boolean> isEqual, String testDescription,
        Object actualValue, Object expectedValue) {
    if (!isEqual.apply(actualValue, expectedValue)) {
        StringBuilder sb = new StringBuilder();
        if (testDescription != null) {
            sb.append(String.format("Test: %s, ", testDescription));
        }/*w  w w .  j  a  v  a2 s . c  o m*/
        sb.append(String.format("Expected: %s, actual: %s", expectedValue, actualValue));
        fail(sb.toString());
    }
}

From source file:com.github.nginate.commons.lang.NStrings.java

/**
 * Internal template generator. Consumes one of function for simple ('{}') or named placeholder ('{arg1}') template
 * @param template string template with placeholders
 * @param closeFinder function to determine end symbol for a placeholder
 * @param args args to inject in template
 * @return plain string with injected args
 *//*from ww  w  .ja  v a2 s  .c  o m*/
private static String formatInternal(String template, BiFunction<char[], Integer, Integer> closeFinder,
        Object... args) {
    StringBuilder sb = new StringBuilder(template.length() * 2);
    int index = 0;
    int argIndex = 0;
    char[] chars = template.toCharArray();
    while (index < template.length()) {
        char curChar = chars[index];
        if (index < template.length() - 1 && curChar == '{') {
            int closeIndex = closeFinder.apply(chars, index);
            if (closeIndex > 0) {
                sb.append(args[argIndex]);
                argIndex++;
                index = closeIndex + 1;
            } else {
                sb.append(curChar);
                index++;
            }
        } else {
            sb.append(curChar);
            index++;
        }
    }
    return sb.toString();
}

From source file:com.github.anba.es6draft.util.Resources.java

private static <T extends TestInfo> Function<Path, T> mapper(BiFunction<Path, Path, T> fn, Path basedir) {
    return file -> fn.apply(basedir, file);
}

From source file:com.streamsets.datacollector.pipeline.executor.spark.yarn.YarnAppLauncher.java

@SuppressWarnings("ReturnValueIgnored")
private static void applyConfIfPresent(String configName, String configValue,
        BiFunction<String, String, ?> configFn) {
    // Both being empty is valid, since the user may have just created a new one by clicking (+), but not
    // entered anything. So just don't pass it along.
    // Just one being empty is taken care of by the init method.
    if (!StringUtils.isEmpty(configName) && !StringUtils.isEmpty(configValue)) {
        configFn.apply(configName, configValue);
    }/*from   w w  w.j a  v  a 2  s.c o m*/
}

From source file:Main.java

public static <T> List<List<T>> innerJoin(List<T> l1, List<T> l2, BiFunction<T, T, T> function) {
    if (l1 == null || l2 == null) {
        throw new NullPointerException("inner join arrays must not be null");
    }//from w w w  . ja v  a 2 s .c  o m

    if (l1.isEmpty() && l2.isEmpty()) {
        return Collections.emptyList();
    } else if (l1.isEmpty()) {
        return Collections.singletonList(l2);
    } else if (l2.isEmpty()) {
        return Collections.singletonList(l1);
    }

    List<List<T>> result = new ArrayList<>(l1.size() * l2.size());
    l1.stream().forEach(t1 -> {
        List<T> l = new ArrayList<>();
        l2.stream().forEach(t2 -> l.add(function.apply(t1, t2)));
        result.add(l);
    });
    return result;
}

From source file:com.wrmsr.wava.transform.Transforms.java

public static Node uniquifyLabels(Node root, Supplier<Name> nameSupplier) {
    Map<Name, Name> remappedNames = new HashMap<>();
    return root.accept(new Visitor<Void, Node>() {
        @Override/* w w  w.ja v a2s  . co m*/
        protected Node visitNode(Node node, Void context) {
            return reconstructNode(node,
                    node.getChildren().stream().map(c -> c.accept(this, context)).iterator());
        }

        private Node processNamedNode(Node node, Name name, BiFunction<Name, Node, Node> ctor, Void context) {
            Name prevName = remappedNames.get(name);
            Name newName = prevName == null ? name : nameSupplier.get();
            remappedNames.put(name, newName);
            Node child = Iterables.getOnlyElement(node.getChildren());
            Node ret = ctor.apply(newName, child.accept(this, context));
            if (prevName == null) {
                remappedNames.remove(name);
            } else {
                remappedNames.put(name, prevName);
            }
            return ret;
        }

        @Override
        public Node visitBreak(Break node, Void context) {
            return new Break(requireNonNull(remappedNames.get(node.getTarget())),
                    node.getValue().accept(this, context));
        }

        @Override
        public Node visitBreakTable(BreakTable node, Void context) {
            return new BreakTable(
                    node.getTargets().stream().map(remappedNames::get).map(Objects::requireNonNull).collect(
                            toImmutableList()),
                    requireNonNull(remappedNames.get(node.getDefaultTarget())),
                    node.getCondition().accept(this, context));
        }

        @Override
        public Node visitLabel(Label node, Void context) {
            return processNamedNode(node, node.getName(), Label::new, context);
        }

        @Override
        public Node visitLoop(Loop node, Void context) {
            return processNamedNode(node, node.getName(), Loop::new, context);
        }
    }, null);
}

From source file:com.github.anba.es6draft.util.Resources.java

private static <T extends TestInfo> Function<Path, T> mapper(BiFunction<Path, Iterator<String>, T> fn) {
    return file -> {
        try (BufferedReader reader = Files.newBufferedReader(file, StandardCharsets.UTF_8)) {
            return fn.apply(file, new LineIterator(reader));
        } catch (IOException e) {
            throw new UncheckedIOException(e);
        }/*from w  ww .j a  v  a 2  s  . c  o  m*/
    };
}