Example usage for java.util.stream Stream builder

List of usage examples for java.util.stream Stream builder

Introduction

In this page you can find the example usage for java.util.stream Stream builder.

Prototype

public static <T> Builder<T> builder() 

Source Link

Document

Returns a builder for a Stream .

Usage

From source file:Main.java

public static void main(String[] args) {
    Stream.Builder<String> b = Stream.builder();
    b.accept("a");
    Stream<String> s = b.build();
    s.limit(10).forEach(System.out::println);

}

From source file:Main.java

public static void main(String[] args) {
    Stream.Builder<String> b = Stream.builder();
    b.accept("a");
    b.accept("b");
    b.accept("c");
    b.accept("d");
    b.accept("e");

    Stream<String> s = b.build();
    s.forEach(System.out::println);

}

From source file:Main.java

public static void main(String[] args) {
    Stream.Builder<String> b = Stream.builder();
    b.add("a");/*from  w  w w.  j a  va  2  s  . co  m*/
    b.add("b");
    b.add("c");
    b.add("d");
    b.add("e");

    Stream<String> s = b.build();
    s.forEach(System.out::println);

}

From source file:Main.java

public static <A, B, C> Stream<C> zip(Stream<A> as, Stream<B> bs, BiFunction<A, B, C> f) {
    Iterator<A> asIterator = as.iterator();
    Iterator<B> bsIterator = bs.iterator();

    Builder<C> builder = Stream.builder();

    while (asIterator.hasNext() && bsIterator.hasNext()) {
        builder.add(f.apply(asIterator.next(), bsIterator.next()));
    }//from  ww  w . j  ava2  s  .  c o m

    return builder.build();
}

From source file:pe.chalk.telegram.util.JSONHelper.java

public static <T> Stream<T> buildStream(final JSONArray array, Class<? extends T> typeClass) {
    if (array == null)
        return Stream.empty();

    final Stream.Builder<T> builder = Stream.builder();
    for (int i = 0; i < array.length(); i++) {
        if (array.isNull(i))
            continue;

        final Object item = array.get(i);
        if (typeClass.isInstance(item))
            builder.add(typeClass.cast(item));
    }//  w ww. j  a va  2  s .c  o m
    return builder.build();
}

From source file:org.trellisldp.http.core.TimemapGenerator.java

/**
 * Generate RDF triples from mementos./*from  www  . ja va  2  s.  co  m*/
 * @param identifier the identifier
 * @param mementos the mementos
 * @return a stream of triples
 */
default Stream<Triple> asRdf(String identifier, List<Link> mementos) {
    final String timeIriPrefix = "http://reference.data.gov.uk/id/gregorian-instant/";
    final RDF rdf = getInstance();
    final IRI originalResource = rdf.createIRI(identifier);
    final List<Triple> descriptions = new ArrayList<>();

    descriptions.add(rdf.createTriple(originalResource, type, Memento.OriginalResource));
    descriptions.add(rdf.createTriple(originalResource, type, Memento.TimeGate));
    descriptions.add(rdf.createTriple(originalResource, Memento.timegate, originalResource));
    descriptions.add(
            rdf.createTriple(originalResource, Memento.timemap, rdf.createIRI(identifier + "?ext=timemap")));

    mementos.stream().filter(link -> link.getRels().contains(MEMENTO)).map(link -> rdf
            .createTriple(originalResource, Memento.memento, rdf.createIRI(link.getUri().toString())))
            .forEach(descriptions::add);

    return Stream.concat(descriptions.stream(), mementos.stream().flatMap(link -> {
        final String linkUri = link.getUri().toString();
        final IRI iri = rdf.createIRI(linkUri);
        final Stream.Builder<Triple> buffer = Stream.builder();

        // TimeMap triples
        if (link.getParams().containsKey(FROM)) {
            buffer.add(rdf.createTriple(iri, type, Memento.TimeMap));
            buffer.add(rdf.createTriple(iri, Time.hasBeginning, rdf.createIRI(
                    timeIriPrefix + parse(link.getParams().get(FROM), RFC_1123_DATE_TIME).toString())));
        }
        if (link.getParams().containsKey(UNTIL)) {
            buffer.add(rdf.createTriple(iri, Time.hasEnd, rdf.createIRI(
                    timeIriPrefix + parse(link.getParams().get(UNTIL), RFC_1123_DATE_TIME).toString())));
        }

        // Memento triples
        if (isMementoLink(link)) {
            final IRI original = rdf.createIRI(linkUri.split("\\?")[0]);
            final IRI timemapUrl = rdf.createIRI(linkUri.split("\\?")[0] + "?ext=timemap");
            buffer.add(rdf.createTriple(iri, type, Memento.Memento));
            buffer.add(rdf.createTriple(iri, Memento.original, original));
            buffer.add(rdf.createTriple(iri, Memento.timegate, original));
            buffer.add(rdf.createTriple(iri, Memento.timemap, timemapUrl));
            buffer.add(rdf.createTriple(iri, Time.hasTime, rdf.createIRI(
                    timeIriPrefix + parse(link.getParams().get(DATETIME), RFC_1123_DATE_TIME).toString())));
            buffer.add(rdf.createTriple(iri, Memento.mementoDatetime, rdf.createLiteral(
                    parse(link.getParams().get(DATETIME), RFC_1123_DATE_TIME).toString(), XSD.dateTime)));
        }
        return buffer.build();
    }));
}

From source file:org.trellisldp.http.impl.HttpUtils.java

/**
 * Get all of the LDP resource (super) types for the given LDP interaction model.
 *
 * @param ixnModel the interaction model
 * @return a stream of types//from  w  w  w.j a  v  a2  s .c  o m
 */
public static Stream<IRI> ldpResourceTypes(final IRI ixnModel) {
    final Stream.Builder<IRI> supertypes = Stream.builder();
    if (nonNull(ixnModel)) {
        LOGGER.debug("Finding types that subsume {}", ixnModel.getIRIString());
        supertypes.accept(ixnModel);
        final IRI superClass = LDP.getSuperclassOf(ixnModel);
        LOGGER.debug("... including {}", superClass);
        ldpResourceTypes(superClass).forEach(supertypes::accept);
    }
    return supertypes.build();
}

From source file:edu.jhu.hlt.concrete.ingesters.simple.DoubleLineBreakFileIngester.java

@Override
public Communication fromCharacterBasedFile(Path path) throws IngestException {
    try {/*from  w  w  w.  j  a v  a  2s. com*/
        ExistingNonDirectoryFile f = new ExistingNonDirectoryFile(path);
        try (InputStream is = Files.newInputStream(path);) {
            String content = IOUtils.toString(is, StandardCharsets.UTF_8);
            AnalyticUUIDGeneratorFactory fact = new AnalyticUUIDGeneratorFactory();
            AnalyticUUIDGenerator g = fact.create();
            Communication c = new Communication();
            c.setUuid(g.next());
            c.setId(f.getName());
            c.setText(content);
            c.setType(this.commKind);
            c.setMetadata(TooledMetadataConverter.convert(this));

            String[] split2xNewline = content.split(doubleLineSep);
            Stream.Builder<TextSpanKindTuple> stream = Stream.builder();
            int charCtr = 0;
            for (String s : split2xNewline) {
                final int len = s.length();
                final int sum = len + charCtr;
                TextSpan ts = new TextSpan(charCtr, sum);
                charCtr = sum + 2;
                stream.add(new TextSpanKindTuple(ts, this.sectionKindLabel));
            }

            Stream<Section> sections = new SectionFactory(g).fromTextSpanStream(stream.build());
            sections.forEach(s -> c.addToSectionList(s));
            return c;
        } catch (IOException e) {
            throw new IngestException("Caught exception reading in document.", e);
        }
    } catch (NoSuchFileException | NotFileException e) {
        throw new IngestException("Path did not exist or was a directory.", e);
    }
}

From source file:org.bsc.confluence.rest.AbstractRESTConfluenceService.java

protected Stream<JsonObject> mapToStream(Response res) {

    final ResponseBody body = res.body();

    try (Reader r = body.charStream()) {

        final JsonReader rdr = Json.createReader(r);

        final JsonObject root = rdr.readObject();

        final Stream.Builder<JsonObject> stream = Stream.builder();

        // Check for Array
        if (root.containsKey("results")) {
            final JsonArray results = root.getJsonArray("results");

            if (results != null) {
                for (int ii = 0; ii < results.size(); ++ii)
                    stream.add(results.getJsonObject(ii));
            }// w ww  .  j  av  a2  s .co  m
        } else {
            stream.add(root);
        }

        return stream.build();

    } catch (IOException | JsonParsingException e) {
        throw new Error(e);
    }

}

From source file:org.sonar.java.se.xproc.MethodYield.java

static Stream<Constraint> pmapToStream(@Nullable PMap<Class<? extends Constraint>, Constraint> pmap) {
    if (pmap == null) {
        return Stream.empty();
    }//  www.j  a va2  s .c o  m
    Stream.Builder<Constraint> result = Stream.builder();
    pmap.forEach((d, c) -> result.add(c));
    return result.build();
}