use of java.util.function.Function in project elasticsearch by elastic.
the class AbstractFilteringJsonGeneratorTestCase method testWithLfAtEnd.
public void testWithLfAtEnd() throws IOException {
final Function<XContentBuilder, XContentBuilder> build = builder -> {
try {
return builder.startObject().startObject("foo").field("bar", "baz").endObject().endObject().prettyPrint().lfAtEnd();
} catch (IOException e) {
throw new RuntimeException(e);
}
};
XContentBuilder expected = build.apply(newXContentBuilder());
assertXContentBuilder(expected, build.apply(newXContentBuilderWithIncludes("foo")));
assertXContentBuilder(expected, build.apply(newXContentBuilderWithExcludes("bar")));
assertXContentBuilder(expected, build.apply(newXContentBuilder(singleton("f*"), singleton("baz"))));
expected = newXContentBuilder().startObject().endObject().prettyPrint().lfAtEnd();
assertXContentBuilder(expected, build.apply(newXContentBuilderWithExcludes("foo")));
assertXContentBuilder(expected, build.apply(newXContentBuilderWithIncludes("bar")));
assertXContentBuilder(expected, build.apply(newXContentBuilder(singleton("f*"), singleton("foo"))));
}
use of java.util.function.Function in project failsafe by jhalterman.
the class Java8Example method main.
@SuppressWarnings("unused")
public static void main(String... args) {
ScheduledExecutorService executor = Executors.newScheduledThreadPool(2);
RetryPolicy retryPolicy = new RetryPolicy();
// Create a retryable functional interface
Function<String, String> bar = value -> Failsafe.with(retryPolicy).get(() -> value + "bar");
// Create a retryable runnable Stream
Failsafe.with(retryPolicy).run(() -> Stream.of("foo").map(value -> value + "bar").forEach(System.out::println));
// Create a retryable callable Stream
Failsafe.with(retryPolicy).get(() -> Stream.of("foo").map(value -> Failsafe.with(retryPolicy).get(() -> value + "bar")).collect(Collectors.toList()));
// Create a individual retryable Stream operation
Stream.of("foo").map(value -> Failsafe.with(retryPolicy).get(() -> value + "bar")).forEach(System.out::println);
// Create a retryable CompletableFuture
Failsafe.with(retryPolicy).with(executor).future(() -> CompletableFuture.supplyAsync(() -> "foo").thenApplyAsync(value -> value + "bar").thenAccept(System.out::println));
// Create an individual retryable CompletableFuture stages
CompletableFuture.supplyAsync(() -> Failsafe.with(retryPolicy).get(() -> "foo")).thenApplyAsync(value -> Failsafe.with(retryPolicy).get(() -> value + "bar")).thenAccept(System.out::println);
}
use of java.util.function.Function in project storm by apache.
the class WorkerState method refreshLoad.
public void refreshLoad() {
Set<Integer> remoteTasks = Sets.difference(new HashSet<Integer>(outboundTasks), new HashSet<>(taskIds));
Long now = System.currentTimeMillis();
Map<Integer, Double> localLoad = shortExecutorReceiveQueueMap.entrySet().stream().collect(Collectors.toMap((Function<Map.Entry<Integer, DisruptorQueue>, Integer>) Map.Entry::getKey, (Function<Map.Entry<Integer, DisruptorQueue>, Double>) entry -> {
DisruptorQueue.QueueMetrics qMetrics = entry.getValue().getMetrics();
return ((double) qMetrics.population()) / qMetrics.capacity();
}));
Map<Integer, Load> remoteLoad = new HashMap<>();
cachedNodeToPortSocket.get().values().stream().forEach(conn -> remoteLoad.putAll(conn.getLoad(remoteTasks)));
loadMapping.setLocal(localLoad);
loadMapping.setRemote(remoteLoad);
if (now > nextUpdate.get()) {
receiver.sendLoadMetrics(localLoad);
nextUpdate.set(now + LOAD_REFRESH_INTERVAL_MS);
}
}
use of java.util.function.Function in project hbase by apache.
the class TestAsyncTableBatch method test.
@Test
public void test() throws InterruptedException, ExecutionException, IOException {
AsyncTableBase table = tableGetter.apply(TABLE_NAME);
table.putAll(IntStream.range(0, COUNT).mapToObj(i -> new Put(getRow(i)).addColumn(FAMILY, CQ, Bytes.toBytes(i))).collect(Collectors.toList())).get();
List<Result> results = table.getAll(IntStream.range(0, COUNT).mapToObj(i -> Arrays.asList(new Get(getRow(i)), new Get(Arrays.copyOf(getRow(i), 4)))).flatMap(l -> l.stream()).collect(Collectors.toList())).get();
assertEquals(2 * COUNT, results.size());
for (int i = 0; i < COUNT; i++) {
assertEquals(i, Bytes.toInt(results.get(2 * i).getValue(FAMILY, CQ)));
assertTrue(results.get(2 * i + 1).isEmpty());
}
Admin admin = TEST_UTIL.getAdmin();
admin.flush(TABLE_NAME);
TEST_UTIL.getHBaseCluster().getRegions(TABLE_NAME).forEach(r -> {
byte[] startKey = r.getRegionInfo().getStartKey();
int number = startKey.length == 0 ? 55 : Integer.parseInt(Bytes.toString(startKey));
byte[] splitPoint = Bytes.toBytes(String.format("%03d", number + 55));
try {
admin.splitRegion(r.getRegionInfo().getRegionName(), splitPoint);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
});
// we are not going to test the function of split so no assertion here. Just wait for a while
// and then start our work.
Thread.sleep(5000);
table.deleteAll(IntStream.range(0, COUNT).mapToObj(i -> new Delete(getRow(i))).collect(Collectors.toList())).get();
results = table.getAll(IntStream.range(0, COUNT).mapToObj(i -> new Get(getRow(i))).collect(Collectors.toList())).get();
assertEquals(COUNT, results.size());
results.forEach(r -> assertTrue(r.isEmpty()));
}
use of java.util.function.Function in project che by eclipse.
the class RefactoringService method createMoveRefactoring.
/**
* Create move refactoring session.
*
* @param cmr
* move settings, contains resource paths to move.
* @return refactoring session id.
* @throws JavaModelException
* when JavaModel has a failure
* @throws RefactoringException
* when impossible to create move refactoring session
*/
@POST
@Path("move/create")
@Consumes("application/json")
@Produces("text/plain")
public String createMoveRefactoring(CreateMoveRefactoring cmr) throws JavaModelException, RefactoringException {
IJavaProject javaProject = model.getJavaProject(cmr.getProjectPath());
IJavaElement[] javaElements;
try {
Function<ElementToMove, IJavaElement> map = javaElement -> {
try {
if (javaElement.isPack()) {
return javaProject.findPackageFragment(new org.eclipse.core.runtime.Path(javaElement.getPath()));
} else {
return javaProject.findType(javaElement.getPath()).getCompilationUnit();
}
} catch (JavaModelException e) {
throw new IllegalArgumentException(e);
}
};
javaElements = cmr.getElements().stream().map(map).toArray(IJavaElement[]::new);
} catch (IllegalArgumentException e) {
if (e.getCause() instanceof JavaModelException) {
throw (JavaModelException) e.getCause();
} else {
throw e;
}
}
if (RefactoringAvailabilityTester.isMoveAvailable(new IResource[0], javaElements)) {
return manager.createMoveRefactoringSession(javaElements);
}
throw new RefactoringException("Can't create move refactoring.");
}
Aggregations