use of java.util.function.Function in project jena by apache.
the class TemplateLib method calcQuads.
/** Substitute into quad patterns */
public static Iterator<Quad> calcQuads(final List<Quad> quads, Iterator<Binding> bindings) {
return Iterators.concat(Iter.map(bindings, new Function<Binding, Iterator<Quad>>() {
Map<Node, Node> bNodeMap = new HashMap<>();
@Override
public Iterator<Quad> apply(final Binding b) {
// Iteration is a new mapping of bnodes.
bNodeMap.clear();
List<Quad> quadList = new ArrayList<>(quads.size());
for (Quad quad : quads) {
Quad q = subst(quad, b, bNodeMap);
if (!q.isConcrete()) {
// "+FmtUtils.stringForQuad(quad)) ;
continue;
}
quadList.add(q);
}
return quadList.iterator();
}
}));
}
use of java.util.function.Function in project jena by apache.
the class TemplateLib method calcTriples.
/** Substitute into triple patterns */
public static Iterator<Triple> calcTriples(final List<Triple> triples, Iterator<Binding> bindings) {
return Iterators.concat(Iter.map(bindings, new Function<Binding, Iterator<Triple>>() {
Map<Node, Node> bNodeMap = new HashMap<>();
@Override
public Iterator<Triple> apply(final Binding b) {
// Iteration is a new mapping of bnodes.
bNodeMap.clear();
List<Triple> tripleList = new ArrayList<>(triples.size());
for (Triple triple : triples) {
Triple q = subst(triple, b, bNodeMap);
if (!q.isConcrete() || !ModelUtils.isValidAsStatement(q.getSubject(), q.getPredicate(), q.getObject())) {
// "+FmtUtils.stringForQuad(quad)) ;
continue;
}
tripleList.add(q);
}
return tripleList.iterator();
}
}));
}
use of java.util.function.Function in project lucene-solr by apache.
the class CloudSolrClientCacheTest method getMockLbHttpSolrClient.
private LBHttpSolrClient getMockLbHttpSolrClient(Map<String, Function> responses) throws Exception {
LBHttpSolrClient mockLbclient = mock(LBHttpSolrClient.class);
when(mockLbclient.request(any(LBHttpSolrClient.Req.class))).then(invocationOnMock -> {
LBHttpSolrClient.Req req = invocationOnMock.getArgument(0);
Function f = responses.get("request");
if (f == null)
return null;
Object res = f.apply(null);
if (res instanceof Exception)
throw (Throwable) res;
LBHttpSolrClient.Rsp rsp = new LBHttpSolrClient.Rsp();
rsp.rsp = (NamedList<Object>) res;
rsp.server = req.servers.get(0);
return rsp;
});
return mockLbclient;
}
use of java.util.function.Function in project lucene-solr by apache.
the class TestMemoryIndex method testPointValues.
public void testPointValues() throws Exception {
List<Function<Long, IndexableField>> fieldFunctions = Arrays.asList((t) -> new IntPoint("number", t.intValue()), (t) -> new LongPoint("number", t), (t) -> new FloatPoint("number", t.floatValue()), (t) -> new DoublePoint("number", t.doubleValue()));
List<Function<Long, Query>> exactQueryFunctions = Arrays.asList((t) -> IntPoint.newExactQuery("number", t.intValue()), (t) -> LongPoint.newExactQuery("number", t), (t) -> FloatPoint.newExactQuery("number", t.floatValue()), (t) -> DoublePoint.newExactQuery("number", t.doubleValue()));
List<Function<long[], Query>> setQueryFunctions = Arrays.asList((t) -> IntPoint.newSetQuery("number", LongStream.of(t).mapToInt(value -> (int) value).toArray()), (t) -> LongPoint.newSetQuery("number", t), (t) -> FloatPoint.newSetQuery("number", Arrays.asList(LongStream.of(t).mapToObj(value -> (float) value).toArray(Float[]::new))), (t) -> DoublePoint.newSetQuery("number", LongStream.of(t).mapToDouble(value -> (double) value).toArray()));
List<BiFunction<Long, Long, Query>> rangeQueryFunctions = Arrays.asList((t, u) -> IntPoint.newRangeQuery("number", t.intValue(), u.intValue()), (t, u) -> LongPoint.newRangeQuery("number", t, u), (t, u) -> FloatPoint.newRangeQuery("number", t.floatValue(), u.floatValue()), (t, u) -> DoublePoint.newRangeQuery("number", t.doubleValue(), u.doubleValue()));
for (int i = 0; i < fieldFunctions.size(); i++) {
Function<Long, IndexableField> fieldFunction = fieldFunctions.get(i);
Function<Long, Query> exactQueryFunction = exactQueryFunctions.get(i);
Function<long[], Query> setQueryFunction = setQueryFunctions.get(i);
BiFunction<Long, Long, Query> rangeQueryFunction = rangeQueryFunctions.get(i);
Document doc = new Document();
for (int number = 1; number < 32; number += 2) {
doc.add(fieldFunction.apply((long) number));
}
MemoryIndex mi = MemoryIndex.fromDocument(doc, analyzer);
IndexSearcher indexSearcher = mi.createSearcher();
Query query = exactQueryFunction.apply(5L);
assertEquals(1, indexSearcher.count(query));
query = exactQueryFunction.apply(4L);
assertEquals(0, indexSearcher.count(query));
query = setQueryFunction.apply(new long[] { 3L, 9L, 19L });
assertEquals(1, indexSearcher.count(query));
query = setQueryFunction.apply(new long[] { 2L, 8L, 13L });
assertEquals(1, indexSearcher.count(query));
query = setQueryFunction.apply(new long[] { 2L, 8L, 16L });
assertEquals(0, indexSearcher.count(query));
query = rangeQueryFunction.apply(2L, 16L);
assertEquals(1, indexSearcher.count(query));
query = rangeQueryFunction.apply(24L, 48L);
assertEquals(1, indexSearcher.count(query));
query = rangeQueryFunction.apply(48L, 68L);
assertEquals(0, indexSearcher.count(query));
}
}
use of java.util.function.Function in project samza by apache.
the class TestMessageStreamImpl method testPartitionBy.
@Test
public void testPartitionBy() {
Map<String, String> map = new HashMap<>();
map.put(JobConfig.JOB_DEFAULT_SYSTEM(), "testsystem");
Config config = new MapConfig(map);
ApplicationRunner runner = ApplicationRunner.fromConfig(config);
StreamGraphImpl streamGraph = new StreamGraphImpl(runner, config);
MessageStreamImpl<TestMessageEnvelope> inputStream = new MessageStreamImpl<>(streamGraph);
Function<TestMessageEnvelope, String> keyExtractorFunc = m -> "222";
inputStream.partitionBy(keyExtractorFunc);
assertTrue(streamGraph.getInputStreams().size() == 1);
assertTrue(streamGraph.getOutputStreams().size() == 1);
Collection<OperatorSpec> subs = inputStream.getRegisteredOperatorSpecs();
assertEquals(subs.size(), 1);
OperatorSpec<TestMessageEnvelope> partitionByOp = subs.iterator().next();
assertTrue(partitionByOp instanceof SinkOperatorSpec);
assertNull(partitionByOp.getNextStream());
((SinkOperatorSpec) partitionByOp).getSinkFn().apply(new TestMessageEnvelope("111", "test", 1000), envelope -> assertTrue(envelope.getPartitionKey().equals("222")), null);
}
Aggregations