use of java.util.function.BinaryOperator in project ignite by apache.
the class CacheUtils method sparseFold.
/**
* Sparse version of fold. This method also applicable to sparse zeroes.
*
* @param cacheName Cache name.
* @param folder Folder.
* @param keyFilter Key filter.
* @param accumulator Accumulator.
* @param zeroValSupp Zero value supplier.
* @param defVal Default value.
* @param defKey Default key.
* @param defValCnt Def value count.
* @param isNilpotent Is nilpotent.
*/
private static <K, V, A> A sparseFold(String cacheName, IgniteBiFunction<Cache.Entry<K, V>, A, A> folder, IgnitePredicate<K> keyFilter, BinaryOperator<A> accumulator, IgniteSupplier<A> zeroValSupp, V defVal, K defKey, long defValCnt, boolean isNilpotent) {
A defRes = zeroValSupp.get();
if (!isNilpotent)
for (int i = 0; i < defValCnt; i++) defRes = folder.apply(new CacheEntryImpl<>(defKey, defVal), defRes);
Collection<A> totalRes = bcast(cacheName, () -> {
Ignite ignite = Ignition.localIgnite();
IgniteCache<K, V> cache = ignite.getOrCreateCache(cacheName);
int partsCnt = ignite.affinity(cacheName).partitions();
// Use affinity in filter for ScanQuery. Otherwise we accept consumer in each node which is wrong.
Affinity affinity = ignite.affinity(cacheName);
ClusterNode locNode = ignite.cluster().localNode();
A a = zeroValSupp.get();
// Iterate over all partitions. Some of them will be stored on that local node.
for (int part = 0; part < partsCnt; part++) {
int p = part;
// Query returns an empty cursor if this partition is not stored on this node.
for (Cache.Entry<K, V> entry : cache.query(new ScanQuery<K, V>(part, (k, v) -> affinity.mapPartitionToNode(p) == locNode && (keyFilter == null || keyFilter.apply(k))))) a = folder.apply(entry, a);
}
return a;
});
return totalRes.stream().reduce(defRes, accumulator);
}
use of java.util.function.BinaryOperator in project JDK8 by ABHSY.
the class MethodReference method test2.
@Test
public void test2() {
System.out.println(sortList1().toString());
System.out.println(sortList2().toString());
System.out.println(sortList3().toString());
BinaryOperator<Integer> tComparator = (x, y) -> x + y;
Runnable runnable = () -> System.out.print(1);
ActionListener tConsumer = event -> System.out.print(1);
Comparator<Integer> tComparator1 = (x, y) -> x + y;
String name = getName();
Runnable runnable1 = () -> System.out.print(name);
runnable1.run();
}
use of java.util.function.BinaryOperator in project halyard by spinnaker.
the class DaemonTaskHandler method reduceChildren.
public static <U, T> DaemonResponse<U> reduceChildren(U base, BiFunction<U, ? super T, U> accumulator, BinaryOperator<U> combiner) {
DaemonTask task = getTask();
if (task != null) {
U responseBody = base;
ProblemSet problemSet = new ProblemSet();
DaemonResponse<U> response = new DaemonResponse<>(responseBody, problemSet);
return (DaemonResponse) task.getChildren().stream().reduce(response, (o, t) -> {
DaemonResponse<U> collector = (DaemonResponse<U>) o;
DaemonTask child = (DaemonTask) t;
DaemonResponse<T> childResponse;
try {
childResponse = task.reapChild(child);
} catch (InterruptedException e) {
throw new DaemonTaskInterrupted("Interrupted during reap", e);
}
DaemonTask.State state = child.getState();
if (!state.isTerminal()) {
throw new IllegalStateException("Child task " + child + " reaped but non-terminal.");
}
switch(state) {
case FAILED:
throw new HalException(childResponse.getProblemSet().getProblems());
case INTERRUPTED:
task.interrupt();
throw new DaemonTaskInterrupted(child.getFatalError());
case TIMED_OUT:
task.timeout();
throw new DaemonTaskInterrupted("Child task timed out");
case SUCCEEDED:
break;
default:
throw new IllegalStateException("Unknown terminal state " + state);
}
collector.getProblemSet().addAll(childResponse.getProblemSet());
collector.setResponseBody(accumulator.apply(collector.getResponseBody(), childResponse.getResponseBody()));
return collector;
}, (Object o1, Object o2) -> {
DaemonResponse<U> r1 = (DaemonResponse<U>) o1;
DaemonResponse<U> r2 = (DaemonResponse<U>) o2;
r1.setResponseBody(combiner.apply(r1.getResponseBody(), r2.getResponseBody()));
r1.getProblemSet().addAll(r2.getProblemSet());
return r1;
});
} else {
throw new IllegalStateException("You must be running a DaemonTask to reduce child tasks");
}
}
use of java.util.function.BinaryOperator in project sw360portal by sw360.
the class XhtmlGeneratorTest method findLicenses.
private String findLicenses(Document document, String releaseNameString) {
List list = document.selectNodes("//*[local-name()='li'][@id='" + releaseNameString + "']/*[local-name()='ul'][@class='licenseEntries']");
StringBuffer result = new StringBuffer();
for (Iterator iter = list.iterator(); iter.hasNext(); ) {
Element element = (Element) iter.next();
for (Object liObject : element.content()) {
Element liElement = (Element) liObject;
String licenseEntryId = liElement.attribute("id").getValue();
String licenseTextId = licenseEntryId.replace("licenseEntry", "licenseText");
List licenseTexts = document.selectNodes("//*[local-name()='pre'][@id='" + licenseTextId + "']/text()");
Object licenseText = licenseTexts.stream().map(l -> ((Text) l).getStringValue()).reduce("", (BinaryOperator<String>) (l1, l2) -> (String) (l1 + l2));
result.append(((String) licenseText).trim());
result.append("\n");
}
}
return result.toString();
}
use of java.util.function.BinaryOperator in project Gaffer by gchq.
the class ElementAggregatorTest method shouldAggregateWithTuple2BinaryOperator.
@Test
public void shouldAggregateWithTuple2BinaryOperator() {
// Given
final String property1 = "property 1";
final String property2 = "property 2";
final BinaryOperator func1 = new ExampleTuple2BinaryOperator();
final ElementAggregator aggregator = new ElementAggregator.Builder().select(property1, property2).execute(func1).build();
final Properties props1 = new Properties();
props1.put(property1, 1);
props1.put(property2, "value1");
final Properties props2 = new Properties();
props2.put(property1, 10);
props2.put(property2, "value10");
final Properties props3 = new Properties();
props3.put(property1, 5);
props3.put(property2, "value5");
// When
Properties state = props1;
state = aggregator.apply(state, props2);
state = aggregator.apply(state, props3);
// Then
assertEquals(props2, state);
}
Aggregations