Search in sources :

Example 61 with Function

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();
        }
    }));
}
Also used : Binding(org.apache.jena.sparql.engine.binding.Binding) Function(java.util.function.Function) Quad(org.apache.jena.sparql.core.Quad)

Example 62 with Function

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();
        }
    }));
}
Also used : Triple(org.apache.jena.graph.Triple) Binding(org.apache.jena.sparql.engine.binding.Binding) Function(java.util.function.Function)

Example 63 with Function

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;
}
Also used : Function(java.util.function.Function) IOException(java.io.IOException) NoHttpResponseException(org.apache.http.NoHttpResponseException) SocketException(java.net.SocketException) ConnectException(java.net.ConnectException)

Example 64 with Function

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));
    }
}
Also used : Query(org.apache.lucene.search.Query) CoreMatchers.is(org.hamcrest.CoreMatchers.is) Arrays(java.util.Arrays) BinaryPoint(org.apache.lucene.document.BinaryPoint) FieldType(org.apache.lucene.document.FieldType) BiFunction(java.util.function.BiFunction) IndexableField(org.apache.lucene.index.IndexableField) SortedNumericDocValuesField(org.apache.lucene.document.SortedNumericDocValuesField) Term(org.apache.lucene.index.Term) PhraseQuery(org.apache.lucene.search.PhraseQuery) StoredField(org.apache.lucene.document.StoredField) DoublePoint(org.apache.lucene.document.DoublePoint) Document(org.apache.lucene.document.Document) TermsEnum(org.apache.lucene.index.TermsEnum) LeafReaderContext(org.apache.lucene.index.LeafReaderContext) NumericDocValuesField(org.apache.lucene.document.NumericDocValuesField) SortedSetDocValuesField(org.apache.lucene.document.SortedSetDocValuesField) ClassicSimilarity(org.apache.lucene.search.similarities.ClassicSimilarity) BytesRef(org.apache.lucene.util.BytesRef) BinaryDocValuesField(org.apache.lucene.document.BinaryDocValuesField) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) StandardCharsets(java.nio.charset.StandardCharsets) SortedNumericDocValues(org.apache.lucene.index.SortedNumericDocValues) MockAnalyzer(org.apache.lucene.analysis.MockAnalyzer) List(java.util.List) FieldInvertState(org.apache.lucene.index.FieldInvertState) SortedDocValuesField(org.apache.lucene.document.SortedDocValuesField) LeafReader(org.apache.lucene.index.LeafReader) LuceneTestCase(org.apache.lucene.util.LuceneTestCase) MockPayloadAnalyzer(org.apache.lucene.analysis.MockPayloadAnalyzer) BinaryDocValues(org.apache.lucene.index.BinaryDocValues) IndexReader(org.apache.lucene.index.IndexReader) BM25Similarity(org.apache.lucene.search.similarities.BM25Similarity) IndexSearcher(org.apache.lucene.search.IndexSearcher) LongPoint(org.apache.lucene.document.LongPoint) StringField(org.apache.lucene.document.StringField) NumericDocValues(org.apache.lucene.index.NumericDocValues) TestUtil(org.apache.lucene.util.TestUtil) CoreMatchers.not(org.hamcrest.CoreMatchers.not) Function(java.util.function.Function) StringContains.containsString(org.junit.internal.matchers.StringContains.containsString) Similarity(org.apache.lucene.search.similarities.Similarity) SortedSetDocValues(org.apache.lucene.index.SortedSetDocValues) IntPoint(org.apache.lucene.document.IntPoint) SortedDocValues(org.apache.lucene.index.SortedDocValues) TermStatistics(org.apache.lucene.search.TermStatistics) Before(org.junit.Before) LongStream(java.util.stream.LongStream) PostingsEnum(org.apache.lucene.index.PostingsEnum) FloatPoint(org.apache.lucene.document.FloatPoint) Analyzer(org.apache.lucene.analysis.Analyzer) IOException(java.io.IOException) Test(org.junit.Test) CollectionStatistics(org.apache.lucene.search.CollectionStatistics) TermQuery(org.apache.lucene.search.TermQuery) Field(org.apache.lucene.document.Field) DocValuesType(org.apache.lucene.index.DocValuesType) TextField(org.apache.lucene.document.TextField) IndexOptions(org.apache.lucene.index.IndexOptions) IndexSearcher(org.apache.lucene.search.IndexSearcher) Query(org.apache.lucene.search.Query) PhraseQuery(org.apache.lucene.search.PhraseQuery) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) TermQuery(org.apache.lucene.search.TermQuery) LongPoint(org.apache.lucene.document.LongPoint) Document(org.apache.lucene.document.Document) BinaryPoint(org.apache.lucene.document.BinaryPoint) DoublePoint(org.apache.lucene.document.DoublePoint) LongPoint(org.apache.lucene.document.LongPoint) IntPoint(org.apache.lucene.document.IntPoint) FloatPoint(org.apache.lucene.document.FloatPoint) IndexableField(org.apache.lucene.index.IndexableField) IntPoint(org.apache.lucene.document.IntPoint) BiFunction(java.util.function.BiFunction) Function(java.util.function.Function) FloatPoint(org.apache.lucene.document.FloatPoint) BiFunction(java.util.function.BiFunction) DoublePoint(org.apache.lucene.document.DoublePoint)

Example 65 with Function

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);
}
Also used : StreamOperatorSpec(org.apache.samza.operators.spec.StreamOperatorSpec) JobConfig(org.apache.samza.config.JobConfig) TestInputMessageEnvelope(org.apache.samza.operators.data.TestInputMessageEnvelope) HashMap(java.util.HashMap) Function(java.util.function.Function) MapFunction(org.apache.samza.operators.functions.MapFunction) ArrayList(java.util.ArrayList) OperatorSpec(org.apache.samza.operators.spec.OperatorSpec) PartialJoinOperatorSpec(org.apache.samza.operators.spec.PartialJoinOperatorSpec) ArgumentCaptor(org.mockito.ArgumentCaptor) ImmutableList(com.google.common.collect.ImmutableList) SinkOperatorSpec(org.apache.samza.operators.spec.SinkOperatorSpec) MessageCollector(org.apache.samza.task.MessageCollector) SystemStream(org.apache.samza.system.SystemStream) Duration(java.time.Duration) Map(java.util.Map) Mockito.doAnswer(org.mockito.Mockito.doAnswer) TestOutputMessageEnvelope(org.apache.samza.operators.data.TestOutputMessageEnvelope) MapConfig(org.apache.samza.config.MapConfig) TestMessageEnvelope(org.apache.samza.operators.data.TestMessageEnvelope) ApplicationRunner(org.apache.samza.runtime.ApplicationRunner) FilterFunction(org.apache.samza.operators.functions.FilterFunction) TaskContext(org.apache.samza.task.TaskContext) MessageType(org.apache.samza.operators.data.MessageType) TestExtOutputMessageEnvelope(org.apache.samza.operators.data.TestExtOutputMessageEnvelope) Collection(java.util.Collection) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Mockito.times(org.mockito.Mockito.times) Mockito.when(org.mockito.Mockito.when) JoinFunction(org.apache.samza.operators.functions.JoinFunction) FlatMapFunction(org.apache.samza.operators.functions.FlatMapFunction) TaskCoordinator(org.apache.samza.task.TaskCoordinator) Mockito.verify(org.mockito.Mockito.verify) Matchers.any(org.mockito.Matchers.any) List(java.util.List) Assert.assertNull(org.junit.Assert.assertNull) SinkFunction(org.apache.samza.operators.functions.SinkFunction) OutgoingMessageEnvelope(org.apache.samza.system.OutgoingMessageEnvelope) Config(org.apache.samza.config.Config) Assert.assertEquals(org.junit.Assert.assertEquals) Mockito.mock(org.mockito.Mockito.mock) HashMap(java.util.HashMap) JobConfig(org.apache.samza.config.JobConfig) MapConfig(org.apache.samza.config.MapConfig) Config(org.apache.samza.config.Config) StreamOperatorSpec(org.apache.samza.operators.spec.StreamOperatorSpec) OperatorSpec(org.apache.samza.operators.spec.OperatorSpec) PartialJoinOperatorSpec(org.apache.samza.operators.spec.PartialJoinOperatorSpec) SinkOperatorSpec(org.apache.samza.operators.spec.SinkOperatorSpec) TestMessageEnvelope(org.apache.samza.operators.data.TestMessageEnvelope) ApplicationRunner(org.apache.samza.runtime.ApplicationRunner) MapConfig(org.apache.samza.config.MapConfig) SinkOperatorSpec(org.apache.samza.operators.spec.SinkOperatorSpec) Test(org.junit.Test)

Aggregations

Function (java.util.function.Function)176 List (java.util.List)75 ArrayList (java.util.ArrayList)55 Map (java.util.Map)51 Test (org.junit.Test)47 IOException (java.io.IOException)44 HashMap (java.util.HashMap)37 Set (java.util.Set)36 Collectors (java.util.stream.Collectors)33 Arrays (java.util.Arrays)30 Collections (java.util.Collections)26 Collection (java.util.Collection)25 File (java.io.File)20 HashSet (java.util.HashSet)19 Supplier (java.util.function.Supplier)19 BiFunction (java.util.function.BiFunction)18 Consumer (java.util.function.Consumer)16 Test (org.testng.annotations.Test)16 Stream (java.util.stream.Stream)14 Assert.assertEquals (org.junit.Assert.assertEquals)13