Java Code Examples for java.util.function.Function.andThen()

The following are Jave code examples for showing how to use andThen() of the java.util.function.Function class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: omero-ms-queue   File: ArtemisQConnector.java   View Source Code Vote up 6 votes
@Override
public ArtemisMessage queueMessage(QMessageType t) {
    requireNonNull(t, "message type");

    Function<Boolean, ClientMessage> create = session::createMessage;
    Function<Boolean, ArtemisMessage> builder =
            create.andThen(ArtemisMessage::new);  // (2)
    switch (t) {
        case Durable:
            return builder.apply(true);
        case NonDurable:
            return builder.apply(false);
        default:  // (1)
            throw new IllegalArgumentException("unsupported message type");
    }
}
 
Example 2
Project: Unified-World-Units   File: CompositeUnit.java   View Source Code Vote up 6 votes
/**
 * {@inheritDoc}
 */
@Override
public Function<BigDecimalAmount, BigDecimalAmount> getTranslationToCanonical() {
	Function<BigDecimalAmount, BigDecimalAmount> result = Function.identity();

	for (UnitCount unitCount : unitCounter.getBaseUnits()) {
		BaseUnit unit = unitCount.getUnit();
		int count = unitCount.getCount();

		if (count > 0) {
			for (int i = 0; i < count; i++) {
				result = result.andThen(unit.getTranslationToCanonical());
			}
		} else if (count < 0) {
			for (int i = 0; i > count; i--) {
				result = result.andThen(unit.getTranslationFromCanonical());
			}
		}
	}

	return result;
}
 
Example 3
Project: Unified-World-Units   File: CompositeUnit.java   View Source Code Vote up 6 votes
/**
 * {@inheritDoc}
 */
@Override
public Function<BigDecimalAmount, BigDecimalAmount> getTranslationFromCanonical() {
	Function<BigDecimalAmount, BigDecimalAmount> result = Function.identity();

	for (UnitCount unitCount : unitCounter.getBaseUnits()) {
		BaseUnit unit = unitCount.getUnit();
		int count = unitCount.getCount();

		if (count > 0) {
			for (int i = 0; i < count; i++) {
				result = result.andThen(unit.getTranslationFromCanonical());
			}
		} else if (count < 0) {
			for (int i = 0; i > count; i--) {
				result = result.andThen(unit.getTranslationToCanonical());
			}
		}
	}

	return result;
}
 
Example 4
Project: mug   File: Maybe.java   View Source Code Vote up 5 votes
/**
 * Wraps {@code function} that returns {@code Stream<T>} to one that returns
 * {@code Stream<Maybe<T, E>>} with exceptions of type {@code E} wrapped.
 */
public static <F, T, E extends Throwable> Function<F, Stream<Maybe<T, E>>> maybeStream(
    CheckedFunction<? super F, ? extends Stream<? extends T>, ? extends E> function,
    Class<E> exceptionType) {
  Function<F, Maybe<Stream<? extends T>, E>> wrapped = maybe(function, exceptionType);
  return wrapped.andThen(Maybe::maybeStream);
}
 
Example 5
Project: kubernetes-alexa   File: IntentContext.java   View Source Code Vote up 5 votes
public T asOperation(Function<IntentContext, IntentContext>... functions) {
    Function<IntentContext, IntentContext> next = s -> s;
    for (Function<IntentContext, IntentContext> f : functions) {
        next = next.andThen(f);
    }

    return (T) next.apply(new IntentContext<T>(intent, session, operation, client)).getOperation();
}
 
Example 6
Project: kubernetes-alexa   File: IntentContext.java   View Source Code Vote up 5 votes
public T asOperation(Iterable<Function<IntentContext, IntentContext>> functions) {
    Function<IntentContext, IntentContext> next = s -> s;
    for (Function<IntentContext, IntentContext> f : functions) {
        next = next.andThen(f);
    }

    return (T) next.apply(new IntentContext<T>(intent, session, operation, client)).getOperation();
}
 
Example 7
Project: outcomes   File: Main.java   View Source Code Vote up 4 votes
public static void main(String... args) {

        Predicate<String> predicate = String::isEmpty;

        //等价与  Predicate<String> predicate = s -> !s.isEmpty();
        // predicate.negate();

        // ps : and 和 or是按照其表达式在链中的位置  由左到右确定优先级
        // a.or(b).and(c)可以看作(a || b) && c
        // predicate.and()
        // predicate.or()


        // predicate.equals()

        //编译错误 Lambda表达式引用的局部变量必须是final的
        // Integer i = 1337;
        // Runnable foo = () -> System.out.print(i);
        // i = 2000;

        //构造对象 只提供了两个参数  多个参数的可以自己写函数式接口
        Supplier<Lambdaer> s = Lambdaer::new;
        Lambdaer lambdaerOne = s.get();

        Function<String, Lambdaer> lambda = Lambdaer::new;
        Lambdaer guard = lambda.apply("guard");

        BiFunction<String, String, Lambdaer> b = Lambdaer::new;
        Lambdaer lambdaerTwo = b.apply("guardOne", "guardTwo");

        //正序
        List<Integer> str = Arrays.asList(3, 5, 0, 10, 1, 2);
        Comparator<Integer> compare = Integer::compare;
        str.sort(compare);
        //逆序
        str.sort(compare.reversed());
        //链 优先顺序
        // str.sort(compare.reversed().thenComparing());

        // 等价于数学上的g(f(x))
        Function<Integer, Integer> f = x -> x + 1;
        Function<Integer, Integer> g = x -> x * 2;
        Function<Integer, Integer> h = f.andThen(g);
        int result = h.apply(1);

        // 等价于数学上的f(g(x))
        // Function<Integer, Integer> f = x -> x + 1;
        // Function<Integer, Integer> g = x -> x * 2;
        // Function<Integer, Integer> h = f.compose(g);
        // int result = h.apply(1);
    }
 
Example 8
Project: mug   File: Maybe.java   View Source Code Vote up 2 votes
/**
 * Wraps {@code function} that returns {@code Stream<T>} to one that returns
 * {@code Stream<Maybe<T, E>>} with exceptions of type {@code E} wrapped.
 *
 * <p>Useful to be passed to {@link Stream#flatMap}.
 *
 * <p>Unchecked exceptions will be immediately propagated without being wrapped.
 */
public static <F, T, E extends Throwable> Function<F, Stream<Maybe<T, E>>> maybeStream(
    CheckedFunction<? super F, ? extends Stream<? extends T>, E> function) {
  Function<F, Maybe<Stream<? extends T>, E>> wrapped = maybe(function);
  return wrapped.andThen(Maybe::maybeStream);
}
 
Example 9
Project: control   File: ApplicableWrapper.java   View Source Code Vote up 2 votes
/**
 * When passed to ApplicableWrapper.then(), applies the given function to the wrapped value,
 * returning a new ApplicableWrapper containing the result.
 */
public static <I, O> Function<I, ApplicableWrapper<O>> apply(Function<I, O> function) {
    return function.andThen(ApplicableWrapper::new);
}