List of usage examples for java.util LinkedList LinkedList
public LinkedList()
From source file:Main.java
public static List<Element> getChildElements(Element element) { List<Element> elements = new LinkedList<Element>(); NodeList children = element.getChildNodes(); if (children == null) { return elements; }//from w ww .j a v a 2 s .co m for (int i = 0, j = children.getLength(); i < j; i++) { Node child = children.item(i); if (child.getNodeType() == Node.ELEMENT_NODE) { elements.add((Element) child); } } return elements; }
From source file:Main.java
/** * Gets a string list based on an iterator. * <p>/*from w ww. j a v a 2 s . c o m*/ * As the wrapped Iterator is traversed, an LinkedList of its string values is * created. At the end, the list is returned. * * @param iterator the iterator to use, not null * @return a list of the iterator string contents * @throws NullPointerException if iterator parameter is null */ public static List<String> toStringList(Iterator<?> iterator) { if (iterator == null) { throw new NullPointerException("Iterator must not be null"); } List<String> list = new LinkedList<String>(); while (iterator.hasNext()) { list.add(String.valueOf(iterator.next())); } return list; }
From source file:Main.java
public static <E> LinkedList<E> newLinkedList() { return new LinkedList<E>(); }
From source file:Main.java
public static List singletonList(Object value) { List list = new LinkedList(); list.add(value); return list; }
From source file:Main.java
private static <T> Iterator<T> reverse(Iterable<T> values) { Deque<T> reversed = new LinkedList<>(); for (T value : values) { reversed.add(value);/* w w w . j a v a2s .com*/ } return reversed.descendingIterator(); }
From source file:Main.java
/** * Scans an input string for double values. For each value found, places * in a list. This method regards any characters not part of a floating * point value to be seperators. Thus this will parse whitespace seperated, * comma seperated, and many other separation schemes correctly. *//*from w w w .j a v a2 s . c o m*/ public synchronized static double[] parseDoubleList(String list) { if (list == null) return null; fpMatch.reset(list); LinkedList<Double> doubList = new LinkedList<Double>(); while (fpMatch.find()) { String val = fpMatch.group(1); doubList.add(Double.valueOf(val)); } double[] retArr = new double[doubList.size()]; Iterator<Double> it = doubList.iterator(); int idx = 0; while (it.hasNext()) { retArr[idx++] = ((Double) it.next()).doubleValue(); } return retArr; }
From source file:edu.cornell.mannlib.vitro.webapp.controller.edit.utils.RoleLevelOptionsSetup.java
public static List<Option> getDisplayOptionsList(ResourceBean b) { List<Option> hiddenFromDisplayList = new LinkedList<Option>(); try {/* w ww . ja va 2 s . c om*/ BaseResourceBean.RoleLevel currentLevel = b.getHiddenFromDisplayBelowRoleLevel(); BaseResourceBean.RoleLevel roles[] = BaseResourceBean.RoleLevel.values(); boolean someLevelSet = false; Option publicOption = null; for (BaseResourceBean.RoleLevel level : roles) { Option option = new Option(level.getURI(), level.getDisplayLabel(), false); if (level == BaseResourceBean.RoleLevel.PUBLIC) { publicOption = option; } if (level == currentLevel) { option.setSelected(true); someLevelSet = true; } hiddenFromDisplayList.add(option); } if (!someLevelSet) { publicOption.setSelected(true); } } catch (Exception ex) { log.error("cannot create HiddenFromDisplayBelowRoleLevel options"); } return hiddenFromDisplayList; }
From source file:Main.java
public static Element[] getChildrenByName(Element parentElement, String childrenName) { NodeList nl = parentElement.getChildNodes(); int max = nl.getLength(); LinkedList<Node> list = new LinkedList<Node>(); for (int i = 0; i < max; i++) { Node n = nl.item(i);//from w ww .j a va 2s. co m if (n.getNodeType() == Node.ELEMENT_NODE && n.getNodeName().equals(childrenName)) { list.add(n); } } return list.toArray(new Element[list.size()]); }
From source file:Main.java
/** * Generates permutations from a list of collections. Each permutation contains an element * from each of the collections. /*from w w w. j av a 2 s . com*/ * * @param <T> the generic type for object in the collections * @param collections the list of collections * @return the collection of permutations */ public static <T> Collection<List<T>> generatePermutations(List<Collection<T>> collections) { if (collections == null || collections.isEmpty()) { return Collections.emptyList(); } else { Collection<List<T>> res = new LinkedList<List<T>>(); permutationsImpl(collections, res, 0, new LinkedList<T>()); return res; } }
From source file:com.twentyn.patentSearch.DocumentSearch.java
public static void main(String[] args) throws Exception { System.out.println("Starting up..."); System.out.flush();/* w w w. ja v a 2s .co m*/ Options opts = new Options(); opts.addOption(Option.builder("x").longOpt("index").hasArg().required().desc("Path to index file to read") .build()); opts.addOption(Option.builder("h").longOpt("help").desc("Print this help message and exit").build()); opts.addOption(Option.builder("v").longOpt("verbose").desc("Print verbose log output").build()); opts.addOption(Option.builder("f").longOpt("field").hasArg().desc("The indexed field to search").build()); opts.addOption( Option.builder("q").longOpt("query").hasArg().desc("The query to use when searching").build()); opts.addOption(Option.builder("l").longOpt("list-file").hasArg() .desc("A file containing a list of queries to run in sequence").build()); opts.addOption( Option.builder("e").longOpt("enumerate").desc("Enumerate the documents in the index").build()); opts.addOption(Option.builder("d").longOpt("dump").hasArg() .desc("Dump terms in the document index for a specified field").build()); opts.addOption( Option.builder("o").longOpt("output").hasArg().desc("Write results JSON to this file.").build()); opts.addOption(Option.builder("n").longOpt("inchi-field").hasArg() .desc("The index of the InChI field if an input TSV is specified.").build()); opts.addOption(Option.builder("s").longOpt("synonym-field").hasArg() .desc("The index of the chemical synonym field if an input TSV is specified.").build()); HelpFormatter helpFormatter = new HelpFormatter(); CommandLineParser cmdLineParser = new DefaultParser(); CommandLine cmdLine = null; try { cmdLine = cmdLineParser.parse(opts, args); } catch (ParseException e) { System.out.println("Caught exception when parsing command line: " + e.getMessage()); helpFormatter.printHelp("DocumentIndexer", opts); System.exit(1); } if (cmdLine.hasOption("help")) { helpFormatter.printHelp("DocumentIndexer", opts); System.exit(0); } if (!(cmdLine.hasOption("enumerate") || cmdLine.hasOption("dump") || (cmdLine.hasOption("field") && (cmdLine.hasOption("query") || cmdLine.hasOption("list-file"))))) { System.out.println("Must specify one of 'enumerate', 'dump', or 'field' + {'query', 'list-file'}"); helpFormatter.printHelp("DocumentIndexer", opts); System.exit(1); } if (cmdLine.hasOption("verbose")) { // With help from http://stackoverflow.com/questions/23434252/programmatically-change-log-level-in-log4j2 LoggerContext ctx = (LoggerContext) LogManager.getContext(false); Configuration ctxConfig = ctx.getConfiguration(); LoggerConfig logConfig = ctxConfig.getLoggerConfig(LogManager.ROOT_LOGGER_NAME); logConfig.setLevel(Level.DEBUG); ctx.updateLoggers(); LOGGER.debug("Verbose logging enabled"); } ObjectMapper objectMapper = new ObjectMapper(); objectMapper.enable(SerializationFeature.INDENT_OUTPUT); objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); LOGGER.info("Opening index at " + cmdLine.getOptionValue("index")); try (Directory indexDir = FSDirectory.open(new File(cmdLine.getOptionValue("index")).toPath()); IndexReader indexReader = DirectoryReader.open(indexDir);) { if (cmdLine.hasOption("enumerate")) { /* Enumerate all documents in the index. * With help from * http://stackoverflow.com/questions/2311845/is-it-possible-to-iterate-through-documents-stored-in-lucene-index */ for (int i = 0; i < indexReader.maxDoc(); i++) { Document doc = indexReader.document(i); LOGGER.info("Doc " + i + ":"); LOGGER.info(doc); } } else if (cmdLine.hasOption("dump")) { /* Dump indexed terms for a specific field. * With help from http://stackoverflow.com/questions/11148036/find-list-of-terms-indexed-by-lucene */ Terms terms = SlowCompositeReaderWrapper.wrap(indexReader).terms(cmdLine.getOptionValue("dump")); LOGGER.info("Has positions: " + terms.hasPositions()); LOGGER.info("Has offsets: " + terms.hasOffsets()); LOGGER.info("Has freqs: " + terms.hasFreqs()); LOGGER.info("Stats: " + terms.getStats()); LOGGER.info(terms); TermsEnum termsEnum = terms.iterator(); BytesRef br = null; while ((br = termsEnum.next()) != null) { LOGGER.info(" " + br.utf8ToString()); } } else { IndexSearcher searcher = new IndexSearcher(indexReader); String field = cmdLine.getOptionValue("field"); List<Pair<String, String>> queries = null; if (cmdLine.hasOption("query")) { queries = Collections.singletonList(Pair.of("", cmdLine.getOptionValue("query"))); } else if (cmdLine.hasOption("list-file")) { if (!(cmdLine.hasOption("inchi-field") && cmdLine.hasOption("synonym-field"))) { LOGGER.error("Must specify both inchi-field and synonym-field when using list-file."); System.exit(1); } Integer inchiField = Integer.parseInt(cmdLine.getOptionValue("inchi-field")); Integer synonymField = Integer.parseInt(cmdLine.getOptionValue("synonym-field")); queries = new LinkedList<>(); BufferedReader r = new BufferedReader(new FileReader(cmdLine.getOptionValue("list-file"))); String line; while ((line = r.readLine()) != null) { line = line.trim(); if (!line.isEmpty()) { // TODO: use a proper TSV reader; this is intentionally terrible as is. String[] fields = line.split("\t"); queries.add(Pair.of(fields[inchiField].replace("\"", ""), fields[synonymField])); } } r.close(); } if (queries == null || queries.size() == 0) { LOGGER.error("Found no queries to run."); return; } List<SearchResult> searchResults = new ArrayList<>(queries.size()); for (Pair<String, String> queryPair : queries) { String inchi = queryPair.getLeft(); String rawQueryString = queryPair.getRight(); /* The Lucene query parser interprets the kind of structural annotations we see in chemical entities * as query directives, which is not what we want at all. Phrase queries seem to work adequately * with the analyzer we're currently using. */ String queryString = rawQueryString.trim().toLowerCase(); String[] parts = queryString.split("\\s+"); PhraseQuery query = new PhraseQuery(); for (String p : parts) { query.add(new Term(field, p)); } LOGGER.info("Running query: " + query.toString()); BooleanQuery bq = new BooleanQuery(); bq.add(query, BooleanClause.Occur.MUST); bq.add(new TermQuery(new Term(field, "yeast")), BooleanClause.Occur.SHOULD); bq.add(new TermQuery(new Term(field, "ferment")), BooleanClause.Occur.SHOULD); bq.add(new TermQuery(new Term(field, "fermentation")), BooleanClause.Occur.SHOULD); bq.add(new TermQuery(new Term(field, "fermentive")), BooleanClause.Occur.SHOULD); bq.add(new TermQuery(new Term(field, "saccharomyces")), BooleanClause.Occur.SHOULD); LOGGER.info(" Full query: " + bq.toString()); TopDocs topDocs = searcher.search(bq, 100); ScoreDoc[] scoreDocs = topDocs.scoreDocs; if (scoreDocs.length == 0) { LOGGER.info("Search returned no results."); } List<ResultDocument> results = new ArrayList<>(scoreDocs.length); for (int i = 0; i < scoreDocs.length; i++) { ScoreDoc scoreDoc = scoreDocs[i]; Document doc = indexReader.document(scoreDoc.doc); LOGGER.info("Doc " + i + ": " + scoreDoc.doc + ", score " + scoreDoc.score + ": " + doc.get("id") + ", " + doc.get("title")); results.add(new ResultDocument(scoreDoc.doc, scoreDoc.score, doc.get("title"), doc.get("id"), null)); } LOGGER.info("----- Done with query " + query.toString()); // TODO: reduce memory usage when not writing results to an output file. searchResults.add(new SearchResult(inchi, rawQueryString, bq, results)); } if (cmdLine.hasOption("output")) { try (FileWriter writer = new FileWriter(cmdLine.getOptionValue("output"));) { writer.write(objectMapper.writeValueAsString(searchResults)); } } } } }