// Circular buffer is a linear data structure which follows the FIFO principle public static void main(String[] args) { CircularFifoBuffer buf = new CircularFifoBuffer(4); // We use CircularFifoBuffer from apache commons collection for the implementation buf.add("A"); buf.add("B"); buf.add("C"); buf.add("D"); buf.add("E"); buf.remove(); System.out.println("buf = " + buf); }
public static void main(String[] args) { if (args.length == 0) { System.out.println( "SimpleMovingAvgZkConsumer {zookeeper} {group.id} {topic} {window-size} {wait-time}"); return; } String next; int num; SimpleMovingAvgZkConsumer movingAvg = new SimpleMovingAvgZkConsumer(); String zkUrl = args[0]; String groupId = args[1]; String topic = args[2]; int window = Integer.parseInt(args[3]); movingAvg.waitTime = args[4]; CircularFifoBuffer buffer = new CircularFifoBuffer(window); movingAvg.configure(zkUrl, groupId); movingAvg.start(topic); while ((next = movingAvg.getNextMessage()) != null) { int sum = 0; try { num = Integer.parseInt(next); buffer.add(num); } catch (NumberFormatException e) { // just ignore strings } for (Object o : buffer) { sum += (Integer) o; } if (buffer.size() > 0) { System.out.println("Moving avg is: " + (sum / buffer.size())); } // uncomment if you wish to commit offsets on every message // movingAvg.consumer.commitOffsets(); } movingAvg.consumer.shutdown(); System.exit(0); }
private void evolve() { generation++; ArrayList<EvolvedString> strings = new ArrayList<>(); for (int i = 0; i < 50; i++) strings.add(new EvolvedString(evolvingWord)); double maxSimilarity = 0; String evolvedWord = evolvingWord; for (EvolvedString s : strings) { double similarity = s.similarity(targetWord); if (maxSimilarity < similarity) { maxSimilarity = similarity; evolvedWord = s.getEvolvedWord(); } } evolvingWord = evolvedWord; generations.add(evolvedWord); }
public void add(final Object item) { buffer.add(item); }
private void updateLastModifiedTime() { lastModifiedTimesMillis.add(now()); }
private void initLastModifiedTimesMillis() { long nowCached = now(); for (int i = 0; i < lastModifiedTimesMillis.maxSize(); i++) { lastModifiedTimesMillis.add(Long.valueOf(nowCached)); } }
/** * A stream with one sentence per line is read in, sentences containing a query token (or alias * thereof) are retained and printed out, formatted. * * @param args * @throws IOException */ public static void main(String[] args) throws IOException { if (args.length < 1 || args.length > 2) { System.err.println("Format <query_expanded.xml> [extraction_window:int]"); System.err.println("A stream with one sentece per line is read in, sentences containing"); System.err.println("a query token are retained and printed out, formatted."); System.err.println( "The optional extraction_window parameter must be an integer between -1" + " and inf. It specifies the number of sentences printed out before and " + " after a sentence containing a query. E.g., extraction_window=1 will" + " print sentences immediately preceding and following query sentences." + " If set to -1, all sentences will be printed. Default is 0."); return; } BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out, "UTF-8")); BufferedReader br = new BufferedReader(new InputStreamReader(System.in, "UTF-8")); QueryList ql = new QueryList(args[0]); int extractionWin = 0; if (args.length > 1) { try { extractionWin = Integer.parseInt(args[1]); if (extractionWin < -1) { throw new NumberFormatException(); } } catch (NumberFormatException e) { System.err.println("Extraction window must be an integer between -1" + " and inf."); return; } } // look behind CircularFifoBuffer lastSentences = null; if (extractionWin > 0) { lastSentences = new CircularFifoBuffer(extractionWin); } // look forward int currentExtractionWin = 0; int sNr = 1; TextIdentifier docQueryId = null; for (String line; (line = br.readLine()) != null; ) { if (line.startsWith("#")) { docQueryId = TextIdentifier.fromDelimited(line.substring(1)); if (docQueryId.getQueryId() == null || docQueryId.getQueryId().isEmpty()) { throw new IllegalArgumentException("TextIdentifier without query id:" + line); } sNr = 1; if (lastSentences != null) { lastSentences.clear(); } currentExtractionWin = 0; } else { assert (docQueryId != null); TextIdentifier docQuerySnrId = new TextIdentifier(docQueryId).setSentNr(sNr); try { if (extractionWin == -1 || hasQueryMatch(ql.getQueryById(docQuerySnrId.getQueryId()), line)) { // old sentences in buffer if (lastSentences != null) { int oldSNr = sNr - lastSentences.size(); while (lastSentences.size() > 0) { Object lineStrObject = lastSentences.remove(); writeLine( out, (String) lineStrObject, new TextIdentifier(docQueryId).setSentNr(oldSNr++)); } } // current sentence writeLine(out, line, new TextIdentifier(docQueryId).setSentNr(sNr)); currentExtractionWin = extractionWin; } else { // no match in sentence // within look-forward window of last query-containing sentence if (currentExtractionWin > 0) { writeLine(out, line, new TextIdentifier(docQueryId).setSentNr(sNr)); --currentExtractionWin; } else { // possibly in look-behind window if (lastSentences != null) { lastSentences.add(line); } } } } catch (IllegalArgumentException e) { throw new IllegalArgumentException( "Error processing sentence with id:" + " \n" + docQueryId + "\n" + docQuerySnrId + "\n" + e); } sNr += 1; } } out.flush(); br.close(); }