Example usage for java.util Scanner hasNext

List of usage examples for java.util Scanner hasNext

Introduction

In this page you can find the example usage for java.util Scanner hasNext.

Prototype

public boolean hasNext(Pattern pattern) 

Source Link

Document

Returns true if the next complete token matches the specified pattern.

Usage

From source file:logicProteinHypernetwork.analysis.complexes.offline.ComplexPredictionCommand.java

/**
 * Returns predicted complexes for a given undirected graph of proteins.
 *
 * @param g the graph//ww w . j a va 2  s. c o  m
 * @return the predicted complexes
 */
public Collection<Complex> transform(UndirectedGraph<Protein, Interaction> g) {
    try {
        Process p = Runtime.getRuntime().exec(command);
        BufferedOutputStream outputStream = new BufferedOutputStream(p.getOutputStream());
        BufferedInputStream inputStream = new BufferedInputStream(p.getInputStream());

        for (Interaction i : g.getEdges()) {
            String out = i.first() + " pp " + i.second();
            outputStream.write(out.getBytes());
        }
        outputStream.close();
        p.waitFor();

        if (!hypernetwork.getProteins().hasIndex()) {
            hypernetwork.getProteins().buildIndex();
        }

        Collection<Complex> complexes = new ArrayList<Complex>();
        Scanner s = new Scanner(inputStream);
        Pattern rowPattern = Pattern.compile(".*?\\n");
        Pattern proteinPattern = Pattern.compile(".*?\\s");
        while (s.hasNext(rowPattern)) {
            Complex c = new Complex();
            while (s.hasNext(proteinPattern)) {
                c.add(hypernetwork.getProteins().getProteinById(s.next(proteinPattern).trim()));
            }
            complexes.add(c);
        }
        inputStream.close();

        return complexes;

    } catch (InterruptedException ex) {
        Logger.getLogger(ComplexPredictionCommand.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(ComplexPredictionCommand.class.getName()).log(Level.SEVERE, null, ex);
    }
    return null;
}

From source file:org.springframework.data.gemfire.samples.helloworld.CommandProcessor.java

String process(final String line) {
    final Scanner sc = new Scanner(line);

    return template.execute(new GemfireCallback<String>() {

        public String doInGemfire(Region reg) throws GemFireCheckedException, GemFireException {
            Region<String, String> region = reg;

            if (!sc.hasNext(COM)) {
                return "Invalid command - type 'help' for supported operations";
            }//  w w  w. ja  v  a2s  .  c  o  m
            String command = sc.next();
            String arg1 = (sc.hasNext() ? sc.next() : null);
            String arg2 = (sc.hasNext() ? sc.next() : null);

            // query shortcut
            if ("query".equalsIgnoreCase(command)) {
                String query = line.trim().substring(command.length());
                return region.query(query).toString();
            }

            // parse commands w/o arguments
            if ("exit".equalsIgnoreCase(command)) {
                threadActive = false;
                return "Node exiting...";
            }
            if ("help".equalsIgnoreCase(command)) {
                return help;
            }
            if ("size".equalsIgnoreCase(command)) {
                return EMPTY + region.size();
            }
            if ("clear".equalsIgnoreCase(command)) {
                region.clear();
                return "Clearing grid..";
            }
            if ("keys".equalsIgnoreCase(command)) {
                return region.keySet().toString();
            }
            if ("values".equalsIgnoreCase(command)) {
                return region.values().toString();
            }

            if ("map".equalsIgnoreCase(command)) {
                Set<Entry<String, String>> entrySet = region.entrySet();
                if (entrySet.size() == 0)
                    return "[]";

                StringBuilder sb = new StringBuilder();
                for (Entry<String, String> entry : entrySet) {
                    sb.append("[");
                    sb.append(entry.getKey());
                    sb.append("=");
                    sb.append(entry.getValue());
                    sb.append("] ");
                }
                return sb.toString();
            }

            // commands w/ 1 arg
            if ("containsKey".equalsIgnoreCase(command)) {
                return EMPTY + region.containsKey(arg1);
            }
            if ("containsValue".equalsIgnoreCase(command)) {
                return EMPTY + region.containsValue(arg1);
            }
            if ("get".equalsIgnoreCase(command)) {
                return region.get(arg1);
            }
            if ("remove".equalsIgnoreCase(command)) {
                return region.remove(arg1);
            }

            // commands w/ 2 args

            if ("put".equalsIgnoreCase(command)) {
                return region.put(arg1, arg2);
            }

            sc.close();
            return "unknown command - run 'help' for available commands";
        }
    });
}