Example usage for java.util LinkedList stream

List of usage examples for java.util LinkedList stream

Introduction

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

Prototype

default Stream<E> stream() 

Source Link

Document

Returns a sequential Stream with this collection as its source.

Usage

From source file:com.ikanow.aleph2.enrichment.utils.services.JsScriptEngineTestService.java

/** Entry point
 * @param args//  w ww.  jav a2 s.c o m
 * @throws IOException 
 */
public static void main(String[] args) throws IOException {
    if (args.length < 3) {
        System.out
                .println("ARGS: <script-file> <input-file> <output-prefix> [{[len: <LEN>], [group: <GROUP>]}]");
    }

    // STEP 1: load script file

    final String user_script = Files.toString(new File(args[0]), Charsets.UTF_8);

    // STEP 2: get a stream for the JSON file

    final InputStream io_stream = new FileInputStream(new File(args[1]));

    // STEP 3: set up control if applicable

    Optional<JsonNode> json = Optional.of("").filter(__ -> args.length > 3).map(__ -> args[3])
            .map(Lambdas.wrap_u(j -> _mapper.readTree(j)));

    // STEP 4: set up the various objects

    final DataBucketBean bucket = Mockito.mock(DataBucketBean.class);

    final JsScriptEngineService service_under_test = new JsScriptEngineService();

    final LinkedList<ObjectNode> emitted = new LinkedList<>();
    final LinkedList<JsonNode> grouped = new LinkedList<>();
    final LinkedList<JsonNode> externally_emitted = new LinkedList<>();

    final IEnrichmentModuleContext context = Mockito.mock(IEnrichmentModuleContext.class, new Answer<Void>() {
        @SuppressWarnings("unchecked")
        public Void answer(InvocationOnMock invocation) {
            try {
                Object[] args = invocation.getArguments();
                if (invocation.getMethod().getName().equals("emitMutableObject")) {
                    final Optional<JsonNode> grouping = (Optional<JsonNode>) args[3];
                    if (grouping.isPresent()) {
                        grouped.add(grouping.get());
                    }
                    emitted.add((ObjectNode) args[1]);
                } else if (invocation.getMethod().getName().equals("externalEmit")) {
                    final DataBucketBean to = (DataBucketBean) args[0];
                    final Either<JsonNode, Map<String, Object>> out = (Either<JsonNode, Map<String, Object>>) args[1];
                    externally_emitted
                            .add(((ObjectNode) out.left().value()).put("__a2_bucket", to.full_name()));
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }
    });

    final EnrichmentControlMetadataBean control = BeanTemplateUtils.build(EnrichmentControlMetadataBean.class)
            .with(EnrichmentControlMetadataBean::config,
                    new LinkedHashMap<String, Object>(
                            ImmutableMap.<String, Object>builder().put("script", user_script).build()))
            .done().get();

    service_under_test.onStageInitialize(context, bucket, control,
            Tuples._2T(ProcessingStage.batch, ProcessingStage.grouping), Optional.empty());

    final BeJsonParser json_parser = new BeJsonParser();

    // Run the file through

    final Stream<Tuple2<Long, IBatchRecord>> json_stream = StreamUtils
            .takeUntil(Stream.generate(() -> json_parser.getNextRecord(io_stream)), i -> null == i)
            .map(j -> Tuples._2T(0L, new BatchRecord(j)));

    service_under_test.onObjectBatch(json_stream, json.map(j -> j.get("len")).map(j -> (int) j.asLong(0L)),
            json.map(j -> j.get("group")));

    System.out.println("RESULTS: ");
    System.out.println("emitted: " + emitted.size());
    System.out.println("grouped: " + grouped.size());
    System.out.println("externally emitted: " + externally_emitted.size());
    Files.write(emitted.stream().map(j -> j.toString()).collect(Collectors.joining(";")),
            new File(args[2] + "emit.json"), Charsets.UTF_8);
    Files.write(grouped.stream().map(j -> j.toString()).collect(Collectors.joining(";")),
            new File(args[2] + "group.json"), Charsets.UTF_8);
    Files.write(externally_emitted.stream().map(j -> j.toString()).collect(Collectors.joining(";")),
            new File(args[2] + "external_emit.json"), Charsets.UTF_8);
}

From source file:org.martin.ftp.net.Searcher.java

private void startSearch(String directory, String filter) throws IOException {

    LinkedList<FTPFile> listSearch = accesador.toList(directory);
    listSearch.stream().forEach((file) -> {

        if (file.getName().contains(filter)) {
            resultsList.add(new FileFiltering(directory, file));
        }//from   ww w  .j  a v a 2  s.  com

        if (file.isDirectory()) {
            try {
                startSearch(directory + "/" + file.getName(), filter);
            } catch (IOException ex) {
                Logger.getLogger(Searcher.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    });
}

From source file:com.bwc.ora.OraUtils.java

public static void setLrpsForAnalysis() {
    Lrp fovealLrp = lrpCollection.getFovealLrp();
    if (fovealLrp == null) {
        return;//from  w w  w.ja  v a 2 s . c om
    }

    LrpSettings lrpSettings = ModelsCollection.getInstance().getLrpSettings();
    int octWidth = Oct.getInstance().getImageWidth();
    int distanceBetweenLrps = lrpSettings.getLrpSeperationDistanceInPixels();
    int numberOfLrpTotal = lrpSettings.getNumberOfLrp();
    int lrpPosToLeftOfFovea = fovealLrp.getLrpCenterXPosition();
    int lrpPosToRightOfFovea = fovealLrp.getLrpCenterXPosition();
    LinkedList<Lrp> lrps = new LinkedList<>();
    lrps.add(fovealLrp);
    while (lrps.size() < numberOfLrpTotal) {
        lrpPosToLeftOfFovea -= distanceBetweenLrps;
        lrps.push(new Lrp("Left " + ((lrps.size() + 1) / 2), lrpPosToLeftOfFovea,
                fovealLrp.getLrpCenterYPosition(), lrpSettings.getLrpWidth(), lrpSettings.getLrpHeight(),
                LrpType.PERIPHERAL));

        lrpPosToRightOfFovea += distanceBetweenLrps;
        if (lrps.size() < numberOfLrpTotal) {
            lrps.add(new Lrp("Right " + (lrps.size() / 2), lrpPosToRightOfFovea,
                    fovealLrp.getLrpCenterYPosition(), lrpSettings.getLrpWidth(), lrpSettings.getLrpHeight(),
                    LrpType.PERIPHERAL));
        }
    }

    boolean illegalPositionLrps = lrps.stream()
            .anyMatch(lrp -> lrp.getLrpCenterXPosition() - ((lrpSettings.getLrpWidth() - 1) / 2) < 0
                    || lrp.getLrpCenterXPosition() + ((lrpSettings.getLrpWidth() - 1) / 2) >= octWidth);

    if (illegalPositionLrps) {
        throw new IllegalArgumentException("Combination of LRP width, the number of LRPs and LRP "
                + "separation distance cannot fit within the bounds of the OCT.");
    }
    lrpCollection.setLrps(lrps);
}

From source file:com.ikanow.aleph2.management_db.controllers.actors.BucketDeletionActor.java

/** Deletes the data in all data services
 *  TODO (ALEPH-26): assume default ones for now 
 * @param bucket - the bucket to cleanse
 *//*from  w ww .j a  va2 s.  c om*/
public static CompletableFuture<Collection<BasicMessageBean>> deleteAllDataStoresForBucket(
        final DataBucketBean bucket, final IServiceContext service_context, boolean delete_bucket) {

    // Currently the only supported data service is the search index
    try {
        final LinkedList<CompletableFuture<BasicMessageBean>> vals = new LinkedList<>();

        service_context.listServiceProviders().stream().map(t3 -> t3._1().get())
                .filter(s -> IDataServiceProvider.class.isAssignableFrom(s.getClass()))
                .map(s -> (IDataServiceProvider) s).distinct().forEach(service -> {
                    if (!(delete_bucket && IStorageService.class.isAssignableFrom(service.getClass()))) {
                        // if deleting the bucket then don't need to remove the storage path
                        service.getDataService().ifPresent(ds -> vals
                                .add(ds.handleBucketDeletionRequest(bucket, Optional.empty(), delete_bucket)));
                    }
                });

        return CompletableFuture.allOf(vals.toArray(new CompletableFuture[0])).thenApply(__ -> {
            return vals.stream().map(x -> x.join()).collect(Collectors.toList());
        });
    } catch (Throwable t) {
        return CompletableFuture.completedFuture(
                Arrays.asList(ErrorUtils.buildErrorMessage(BucketDeletionActor.class.getSimpleName(),
                        "deleteAllDataStoresForBucket", ErrorUtils.getLongForm("{0}", t))));
    }
}

From source file:io.kodokojo.bdd.stage.BrickStateNotificationThen.java

public SELF i_receive_all_notification() {
    try {//from  w w w. j a  va  2 s .c  om
        nbMessageExpected.await(2, TimeUnit.MINUTES);
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
    }
    GsonBuilder builder = new GsonBuilder();
    builder.registerTypeAdapter(WebSocketMessage.class, new WebSocketMessageGsonAdapter());
    Gson gson = builder.create();
    LinkedList<String> srcMessages = new LinkedList<>(listener.getMessages());
    assertThat(srcMessages).isNotEmpty();

    List<WebSocketMessage> webSocketMessages = srcMessages.stream()
            .map(m -> gson.fromJson(m, WebSocketMessage.class)).collect(Collectors.toList());
    Map<String, List<WebSocketMessage>> messagePerBrick = new HashMap<>();
    for (WebSocketMessage message : webSocketMessages) {
        if ("brick".equals(message.getEntity())) {
            assertThat(message.getEntity()).isEqualTo("brick");
            assertThat(message.getAction()).isEqualTo("updateState");
            assertThat(message.getData().has("projectConfiguration")).isTrue();
            assertThat(message.getData().has("brickType")).isTrue();
            assertThat(message.getData().has("brickName")).isTrue();
            assertThat(message.getData().has("state")).isTrue();

            String brickName = message.getData().get("brickName").getAsString();
            assertThat(brickName).isIn(expectedBrickStarted);
            List<WebSocketMessage> previous = messagePerBrick.get(brickName);
            if (previous != null) {
            } else {
                previous = new ArrayList<>();
                messagePerBrick.put(brickName, previous);
            }
            previous.add(message);
        }
    }

    for (Map.Entry<String, List<WebSocketMessage>> entry : messagePerBrick.entrySet()) {
        List<WebSocketMessage> messages = entry.getValue();
        boolean configuring = false, starting = false, running = false;
        for (WebSocketMessage webSocketMessage : messages) {
            String state = webSocketMessage.getData().get("state").getAsString();
            switch (state) {
            case "STARTING":
                starting = true;
                break;
            case "CONFIGURING":
                configuring = true;
                break;
            case "RUNNING":
                running = true;
                break;
            default:
                fail("UnExpected state " + state);
            }
        }
        assertThat(configuring).isTrue();
        assertThat(starting).isTrue();
        assertThat(running).isTrue();
    }

    currentStep.addAttachment(Attachment.plainText(StringUtils.join(srcMessages, "\n"))
            .withTitle("WebSocket messages receive").withFileName("websocketMessages"));

    return self();
}

From source file:ru.apertum.qsystem.reports.model.QReportsList.java

@Override
protected LinkedList<QReport> load() {
    final LinkedList<QReport> reports = new LinkedList<>(Spring.getInstance().getHt().loadAll(QReport.class));
    QLog.l().logRep().debug("   " + reports.size() + " .");

    passMap = new HashMap<>();
    htmlRepList = "";
    htmlUsersList = "";
    reports.stream().map((report) -> {
        addGenerator(report);// w w  w .jav a  2 s. com
        return report;
    }).forEach((report) -> {
        htmlRepList = htmlRepList.concat("<tr>\n" + "<td style=\"text-align: left; padding-left: 60px;\">\n"
                + "<a href=\"" + report.getHref() + ".html\" target=\"_blank\">"
                + (RepResBundle.getInstance().present(report.getHref())
                        ? RepResBundle.getInstance().getStringSafe(report.getHref())
                        : report.getName())
                + "</a>\n" + "<a href=\"" + report.getHref() + ".rtf\" target=\"_blank\">[RTF]</a>\n"
                + "<a href=\"" + report.getHref() + ".pdf\" target=\"_blank\">[PDF]</a>\n" + "<a href=\""
                + report.getHref() + ".xlsx\" target=\"_blank\">[XLSX]</a>\n" + "</td>\n" + "</tr>\n");
    });
    /*
     *   . ?  ?? ,  ?   ?  
     * coocies ? ,    ??    ?   ? ? " ?".
     * ?    preparation(), ..   .
     */
    addGenerator(new ReportsList("reportList", ""));
    /*
     *    ???   ?
     */
    addGenerator(new ReportCurrentServices(Uses.REPORT_CURRENT_SERVICES.toLowerCase(),
            "/ru/apertum/qsystem/reports/templates/currentStateServices.jasper"));
    /*
     *    ???   
     */
    addGenerator(new RepCurrentUsers(Uses.REPORT_CURRENT_USERS.toLowerCase(),
            "/ru/apertum/qsystem/reports/templates/currentStateUsers.jasper"));

    String sel = " selected";
    for (QUser user : QUserList.getInstance().getItems()) {
        // ?? ,   
        if (user.getReportAccess()) {
            htmlUsersList = htmlUsersList.concat("<option" + sel + ">").concat(user.getName())
                    .concat("</option>\n");
            sel = "";
            if (user.getReportAccess()) {
                passMap.put(user.getName(), user.getPassword());
            }
        }
    }

    return reports;
}

From source file:AppMain.java

@Override
public void handle(Request request, Response response) {

    long time = System.currentTimeMillis();
    System.out.print(request.getPath().toString() + "\t" + request.getValues("Host") + " ");
    System.out.println(request.getValues("User-agent") + " ");
    response.setDate("Date", time);
    try {//www.  j a  v  a  2 s . co  m

        //static? send the file
        if (request.getPath().toString().startsWith("/static/")) {
            String path = request.getPath().toString().substring("/static/".length());
            File requested = new File("static" + File.separatorChar + path.replace('/', File.separatorChar));
            if (!requested.getCanonicalPath().startsWith(new File("static").getCanonicalPath())) {
                System.err.println("Error, path outside the static folder:" + path);
                return;
            }
            if (!requested.isFile()) {
                System.err.println("Error, file not found:" + path);
                return;
            }
            //valid path, send it
            String mimet = URLConnection.guessContentTypeFromName(path);
            if (path.endsWith(".js"))
                mimet = "application/javascript";
            if (path.endsWith(".css"))
                mimet = "text/css";
            System.out.println("sending static resource:'" + path + "' mimetype:" + mimet);
            response.setDate("Date", requested.lastModified());
            try (PrintStream body = response.getPrintStream()) {
                response.setValue("Content-Type", mimet);
                FileInputStream fis = new FileInputStream(requested);
                //copy the stream
                IOUtils.copy(fis, body);
                fis.close();
            }
            return;
        }
        //main page, show the index
        if (request.getPath().toString().equals("/")) {
            try (PrintStream body = response.getPrintStream()) {
                response.setValue("Content-Type", "text/html;charset=utf-8");
                File file = new File("index.html");
                System.out.println(file.getAbsolutePath());
                byte[] data;
                try (FileInputStream fis = new FileInputStream(file)) {
                    data = new byte[(int) file.length()];
                    fis.read(data);
                }
                body.write(data);
            }
        }

        if (request.getPath().toString().startsWith("/parse")) {
            response.setValue("Content-Type", "application/json;charset=utf-8");
            try (PrintStream body = response.getPrintStream()) {
                String text = request.getQuery().get("text");
                String pattern = request.getQuery().get("pattern");
                if (text == null || pattern == null) {
                    body.write("{'error':'pattern or text not specified'}".getBytes("UTF-8"));
                    body.close();
                }
                System.err.println("request to parse text: " + text + "\nfor pattern: " + pattern);
                MatchingResults results = null;
                JSONObject ret = new JSONObject();
                try {
                    long start = System.currentTimeMillis();
                    results = fm.matches(text, pattern, FlexiMatcher.getDefaultAnnotator(), true, false, true);
                    ret.put("time to parse", System.currentTimeMillis() - start);
                } catch (RuntimeException r) {
                    body.write(("{\"error\":" + JSONObject.quote(r.getMessage()) + "}").getBytes("UTF-8"));
                    body.close();
                    return;
                }
                ret.put("matches", results.isMatching());
                ret.put("empty_match", results.isEmptyMatch());
                ret.put("text", text);
                ret.put("pattern", pattern);
                if (!results.getAnnotations().isPresent()) {
                    body.write(ret.toString().getBytes("UTF-8"));
                    body.close();
                    return;
                }
                for (LinkedList<TextAnnotation> interpretation : results.getAnnotations().get()) {
                    JSONObject addMe = new JSONObject();

                    for (TextAnnotation v : interpretation) {
                        addMe.append("annotations", new JSONObject(v.toJSON()));
                    }
                    ret.append("interpretations", addMe);
                }
                body.write(ret.toString(1).getBytes("UTF-8"));
            }
            return;
        }

        if (request.getPath().toString().startsWith("/trace")) {
            response.setValue("Content-Type", "application/json;charset=utf-8");
            try (PrintStream body = response.getPrintStream()) {
                String text = request.getQuery().get("text");
                String pattern = request.getQuery().get("pattern");
                if (text == null || pattern == null) {
                    body.write("{'error':'pattern or text not specified'}".getBytes("UTF-8"));
                    body.close();
                }
                System.err.println("request to parse text: " + text + "\nfor pattern: " + pattern);
                JSONObject ret = new JSONObject();
                ListingAnnotatorHandler ah;
                try {
                    long start = System.currentTimeMillis();
                    ah = new ListingAnnotatorHandler();
                    fm.matches(text, pattern, ah, true, false, true);
                    ret.put("time", System.currentTimeMillis() - start);
                } catch (RuntimeException r) {
                    body.write(("{\"error\":" + JSONObject.quote(r.getMessage()) + "}").getBytes("UTF-8"));
                    body.close();
                    return;
                }
                ret.put("text", text);
                ret.put("pattern", pattern);
                LinkedList<TextAnnotation> nonOverlappingTA = new LinkedList<>();
                JSONObject addMe = new JSONObject();
                for (TextAnnotation v : ah.getAnnotations()) {
                    if (nonOverlappingTA.stream().anyMatch(p -> p.getSpan().intersects(v.getSpan()))) {
                        ret.append("interpretations", addMe);
                        addMe = new JSONObject();
                        addMe.append("annotations", new JSONObject(v.toJSON()));
                        nonOverlappingTA.clear();
                    } else {
                        addMe.append("annotations", new JSONObject(v.toJSON()));
                    }
                    nonOverlappingTA.add(v);

                }
                ret.append("interpretations", addMe);
                body.write(ret.toString(1).getBytes("UTF-8"));
            }
            return;
        }

        if (request.getPath().toString().startsWith("/addtag")) {
            if (tagCount == 0)
                fwTag.write("\n#tags added from web interface at " + LocalDate.now().toString() + "\n");

            tagCount++;
            response.setValue("Content-Type", "application/json;charset=utf-8");
            try (PrintStream body = response.getPrintStream()) {
                String tag = request.getQuery().getOrDefault("tag", "");
                String pattern = request.getQuery().getOrDefault("pattern", "");

                if (tag.isEmpty() || pattern.isEmpty()) {
                    body.write("{\"error\":\"tag or pattern not specified\"}".getBytes("UTF-8"));
                    body.close();
                    return;
                }

                String identifier = request.getQuery().getOrDefault("identifier", "");
                String annotationTemplate = request.getQuery().getOrDefault("annotation_template", "").trim();

                if (identifier.isEmpty()) {
                    identifier = "auto_" + LocalDate.now().toString() + "_" + tagCount;
                }

                //check that rule identifiers are known
                for (String part : ExpressionParser.split(pattern)) {
                    if (!part.startsWith("["))
                        continue;
                    if (!fm.isBoundRule(ExpressionParser.ruleName(part))) {
                        body.write(("{\"error\":\"rule '" + ExpressionParser.ruleName(part) + "' non known\"}")
                                .getBytes("UTF-8"));
                        body.close();
                        return;
                    }
                }

                fwTag.write(tag + "\t" + pattern + "\t" + identifier + "\t" + annotationTemplate + "\n");
                fwTag.flush();
                if (annotationTemplate.isEmpty())
                    fm.addTagRule(tag, pattern, identifier);
                else
                    fm.addTagRule(tag, pattern, identifier, annotationTemplate);
                body.write(("{\"identifier\":" + JSONObject.quote(identifier) + "}").getBytes("UTF-8"));
            }
            return;

        }
        //unknown request
        try (PrintStream body = response.getPrintStream()) {
            response.setValue("Content-Type", "text/plain");
            response.setDate("Last-Modified", time);
            response.setCode(401);
            body.println("HTTP request for page '" + request.getPath().toString() + "' non understood in "
                    + this.getClass().getCanonicalName());
        }
    } catch (IOException | JSONException e) {
        e.printStackTrace();
        System.exit(1);
    }
}

From source file:com.yahoo.bard.webservice.util.SimplifiedIntervalList.java

/**
 * Back elements off the list until the insertion is at the correct endpoint of the list and then merge and append
 * the original contents of the list back in.
 *
 * @param interval  The interval to be merged and added
 *//*from   ww  w .jav  a2 s.  com*/
private void mergeInner(Interval interval) {
    Interval previous = peekLast();
    LinkedList<Interval> buffer = new LinkedList<>();
    while (previous != null && interval.getStart().isBefore(previous.getStart())) {
        buffer.addFirst(previous);
        removeLast();
        previous = peekLast();
    }
    appendWithMerge(interval);
    buffer.stream().forEach(this::appendWithMerge);
}

From source file:org.martin.ftp.net.FTPLinker.java

/**
 * //  ww w. j  av  a2 s. c  o  m
 * Se obtiene un archivo o carpeta en una ruta especificada
 * @param ruta ruta del archivo
 * @param name nombre del archivo a obtener
 * @return archivo obtenido, si no se encuentra se retorna null
 * @throws IOException 
 */

public FTPFile get(String ruta, String name) throws IOException {

    FTPFile resultado = null;
    LinkedList<FTPFile> files = toList(ruta);
    Optional<FTPFile> res = files.stream().filter((file) -> file.getName().equalsIgnoreCase(name)).findFirst();

    if (res.isPresent())
        resultado = res.get();

    return resultado;
}

From source file:org.martin.ftp.net.FTPLinker.java

/**
 * Retorna una lista ordenada a partir de otra ya entrega, el resultado
 * es una nueva lista en la que se agrupan primero los directorios
 * y luego los archivos/* ww w .ja  va 2 s .com*/
 * @param files Lista a ordenar
 * @return Lista de archivos y carpetas creadas a partir 
 * del parametro, se ubican primero las carpetas y luego los
 * archivos
 */

public LinkedList<FTPFile> getOrdererFiles(LinkedList<FTPFile> files) {

    LinkedList<FTPFile> directories = new LinkedList<>();
    LinkedList<FTPFile> files2 = new LinkedList<>();
    files.stream().filter((d) -> d.isDirectory()).forEach((dir) -> directories.add(dir));
    files.stream().filter((f) -> !f.isDirectory()).forEach((file) -> files2.add(file));

    return files;
}