TimedExecutor.java
01 /*
02  * Java Genetic Algorithm Library (jenetics-3.5.0).
03  * Copyright (c) 2007-2016 Franz Wilhelmstötter
04  *
05  * Licensed under the Apache License, Version 2.0 (the "License");
06  * you may not use this file except in compliance with the License.
07  * You may obtain a copy of the License at
08  *
09  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * Author:
18  *    Franz Wilhelmstötter (franz.wilhelmstoetter@gmx.at)
19  */
20 package org.jenetics.engine;
21 
22 import static java.util.Objects.requireNonNull;
23 import static java.util.concurrent.CompletableFuture.supplyAsync;
24 
25 import java.time.Clock;
26 import java.util.concurrent.CompletableFuture;
27 import java.util.concurrent.Executor;
28 import java.util.function.Function;
29 import java.util.function.Supplier;
30 
31 /**
32  * @author <a href="mailto:franz.wilhelmstoetter@gmx.at">Franz Wilhelmstötter</a>
33  * @since 3.0
34  * @version 3.0
35  */
36 final class TimedExecutor {
37     private final Executor _executor;
38 
39     public TimedExecutor(final Executor executor) {
40         _executor = requireNonNull(executor);
41     }
42 
43     public <T> CompletableFuture<TimedResult<T>> async(
44         final Supplier<T> supplier,
45         final Clock clock
46     ) {
47         return supplyAsync(TimedResult.of(supplier, clock), _executor);
48     }
49 
50     public <U, T> CompletableFuture<TimedResult<T>> thenApply(
51         final CompletableFuture<U> result,
52         final Function<U, T> function,
53         final Clock clock
54     ) {
55         return result.thenApplyAsync(TimedResult.of(function, clock), _executor);
56     }
57 
58 
59     public Executor get() {
60         return _executor;
61     }
62 }