java.util.function.Function

Here are the examples of the java api class java.util.function.Function taken from open source projects.

1. ExecutionPipeline#whenComplete()

Project: cyclops-react
File: ExecutionPipeline.java
public <X extends Throwable, T> ExecutionPipeline whenComplete(BiConsumer<? super T, ? super X> fn) {
    Function before = functionList.get(functionList.size() - 1);
    Function except =  t -> {
        T res = null;
        X ex = null;
        try {
            res = (T) before.apply(t);
        } catch (Throwable e) {
            ex = (X) e;
        }
        fn.accept(res, ex);
        if (ex != null)
            throw (RuntimeException) ex;
        return res;
    };
    return new ExecutionPipeline(swapFn(except), execList, firstRecover, onFail);
}

2. MetricListenerInstanceTest#setup()

Project: monsoon
File: MetricListenerInstanceTest.java
@Before
public void setup() throws Exception {
    seqno = sequence.getAndIncrement();
    PREFIX = "com.groupon.lex.metrics.jmx:type=MetricListenerInstanceTest,seq=" + seqno + ",";
    NOT_PREFIX = "com.groupon.lex.metrics.jmx:type=NotThisMetricListenerInstanceTest,seq=" + seqno + ",";
    GROUP_PATH = new Function<Map<String, MetricValue>, GroupName>() {

        public GroupName apply(Map<String, MetricValue> extra_tags) {
            return new GroupName(new SimpleGroupPath("com", "groupon", "lex", "metrics", "jmx", "MetricListenerInstanceTest"), new HashMap<String, MetricValue>() {

                {
                    put("seq", MetricValue.fromIntValue(seqno));
                    putAll(extra_tags);
                }
            });
        }
    };
    jmx = new JmxClient();
    listener = new MetricListenerInstance(jmx, singleton(new ObjectName(PREFIX + "*")), EMPTY_LIST, Tags.EMPTY);
}

3. ExecutionPipeline#swapComposeFn()

Project: cyclops-react
File: ExecutionPipeline.java
private PStack<Function> swapComposeFn(Function fn) {
    if (functionList.size() == 0) {
        if (this.firstRecover.size() == 0) {
            return functionList.plus(fn);
        } else {
            Function except =  t -> {
                try {
                    return fn.apply(t);
                } catch (Throwable e) {
                    return composeFirstRecovery().apply(e);
                }
            };
            return functionList.plus(except);
        }
    }
    Function before = functionList.get(functionList.size() - 1);
    PStack<Function> removed = functionList.minus(functionList.size() - 1);
    return removed.plus(removed.size(), fn.compose(before));
}

4. DoComp#unwrapNestedFunction()

Project: cyclops-react
File: DoComp.java
private Object unwrapNestedFunction(ComprehensionData c, Function f, PVector<String> vars) {
    Function next = f;
    Object result = null;
    for (String e : vars) {
        result = next.apply(c.$(e));
        if (result instanceof Function) {
            next = ((Function) result);
        }
    }
    if (result instanceof Unwrapable)
        return ((Unwrapable) result).unwrap();
    return result;
}

5. ResourceManagerStateMachineExecutor#execute()

Project: atomix
File: ResourceManagerStateMachineExecutor.java
/**
   * Executes the given commit on the state machine.
   */
@SuppressWarnings("unchecked")
<T extends Operation<U>, U> U execute(Commit<T> commit) {
    // Get the function registered for the operation. If no function is registered, attempt to
    // use a global function if available.
    Function function = operations.get(commit.type());
    if (function == null) {
        throw new IllegalStateException("unknown state machine operation: " + commit.type());
    } else {
        // otherwise immediately complete the execution future.
        return (U) function.apply(commit);
    }
}

6. FluentFunctionTest#memoizeWithCache()

Project: cyclops-react
File: FluentFunctionTest.java
@Test
public void memoizeWithCache() throws InterruptedException {
    Cache<Object, Integer> cache = CacheBuilder.newBuilder().maximumSize(1).expireAfterWrite(1, TimeUnit.SECONDS).build();
    int[] requests = new int[1];
    Function<Integer, Integer> addOne =  i -> {
        requests[0]++;
        return i + 1;
    };
    Function fn = FluentFunctions.of(addOne).name("myFunction").memoize(( key,  f) -> cache.get(key, () -> f.apply(key)));
    fn.apply(10);
    fn.apply(10);
    Thread.sleep(2000);
    fn.apply(10);
    assertEquals(2, requests[0]);
}

7. StaticMethodAccessibleThroughStaticImportButExplicitlyQualified#main()

Project: intellij-community
File: StaticMethodAccessibleThroughStaticImportButExplicitlyQualified.java
public static void main(String[] args) throws Exception {
    Function f = Function.identity();
}

8. ExecutionPipeline#swapFn()

Project: cyclops-react
File: ExecutionPipeline.java
private PStack<Function> swapFn(Function fn) {
    if (functionList.size() == 0)
        return functionList.plus(fn);
    Function before = functionList.get(functionList.size() - 1);
    PStack<Function> removed = functionList.minus(functionList.size() - 1);
    return removed.plus(removed.size(), fn);
}

9. ServerStateMachineExecutor#executeOperation()

Project: copycat
File: ServerStateMachineExecutor.java
/**
   * Executes an operation.
   */
@SuppressWarnings("unchecked")
<T extends Operation<U>, U> U executeOperation(Commit commit) {
    // If the commit operation is a no-op command, complete the operation.
    if (commit.operation() instanceof NoOpCommand) {
        commit.close();
        return null;
    }
    // Get the function registered for the operation. If no function is registered, attempt to
    // use a global function if available.
    Function function = operations.get(commit.type());
    if (function == null) {
        // registered with a parent class.
        for (Map.Entry<Class, Function> entry : operations.entrySet()) {
            if (entry.getKey().isAssignableFrom(commit.type())) {
                function = entry.getValue();
                break;
            }
        }
        // If a parent operation function was found, store the function for future reference.
        if (function != null) {
            operations.put(commit.type(), function);
        }
    }
    if (function == null) {
        throw new IllegalStateException("unknown state machine operation: " + commit.type());
    } else {
        // otherwise immediately complete the execution future.
        try {
            return (U) function.apply(commit);
        } catch (Exception e) {
            throw new ApplicationException("An application error occurred", e);
        }
    }
}