Example usage for org.apache.commons.collections.buffer CircularFifoBuffer add

List of usage examples for org.apache.commons.collections.buffer CircularFifoBuffer add

Introduction

In this page you can find the example usage for org.apache.commons.collections.buffer CircularFifoBuffer add.

Prototype

public boolean add(Object element) 

Source Link

Document

If the buffer is full, the least recently added element is discarded so that a new element can be inserted.

Usage

From source file:com.shapira.examples.zkconsumer.simplemovingavg.SimpleMovingAvgZkConsumer.java

public static void main(String[] args) {
    if (args.length == 0) {
        System.out/*from  ww w . j  a  v  a  2 s .c om*/
                .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);

}

From source file:com.redhat.rhn.common.util.FileUtils.java

/**
 * Reads and returns the last n lines from a given file as string.
 * @param pathToFile path to file//from   ww  w  .  j a  v  a  2 s  . c o m
 * @param lines size of the tail
 * @return tail of file as string
 */
public static String getTailOfFile(String pathToFile, Integer lines) {
    InputStream fileStream = null;
    CircularFifoBuffer buffer = new CircularFifoBuffer(lines);
    try {
        fileStream = new FileInputStream(pathToFile);
        LineIterator it = org.apache.commons.io.IOUtils.lineIterator(fileStream, (String) null);
        while (it.hasNext()) {
            buffer.add(it.nextLine());
        }
    } catch (FileNotFoundException e) {
        log.error("File not found: " + pathToFile);
        throw new RuntimeException(e);
    } catch (IOException e) {
        log.error("Could not read from: " + pathToFile);
        throw new RuntimeException(e);
    } finally {
        org.apache.commons.io.IOUtils.closeQuietly(fileStream);
    }
    // Construct a string from the buffered lines
    StringBuilder sb = new StringBuilder();
    for (Object s : buffer) {
        sb.append(s);
        sb.append(System.getProperty("line.separator"));
    }
    return sb.toString();
}

From source file:edu.umn.msi.tropix.webgui.server.messages.MessageManager.java

private void doPush(final String userId, final T message) {
    final Collection<String> sessionIds = this.getSessionIds(userId);
    for (final String sessionId : sessionIds) {
        final CircularFifoBuffer buffer = this.bufferMap.get(sessionId);
        if (buffer != null) {
            synchronized (buffer) {
                buffer.add(message);
            }//from   w ww.j a va  2s.  c  o  m
        }
    }
}

From source file:eu.esdihumboldt.hale.ui.service.project.internal.RecentProjectsServiceImpl.java

/**
 * @see RecentProjectsService#add(String, String)
 *///from   w w  w  . ja va 2s  . co m
@Override
public void add(String file, String projectName) {
    if (file != null) {
        CircularFifoBuffer buffer = restoreState();

        if (projectName == null)
            projectName = "";
        Entry entry = new EntryImpl(file, projectName);
        Iterator<?> i = buffer.iterator();
        while (i.hasNext()) {
            Entry rfe = (Entry) i.next();
            if (entry.equals(rfe)) {
                i.remove();
                break;
            }
        }
        buffer.add(entry);

        saveState(buffer);
    }
}

From source file:de.suse.swamp.modules.screens.SWAMPScreen.java

public void provideData(RunData data, Context context) throws Exception {
    SWAMPAPI swampapi = new SWAMPAPI();
    String uname = data.getUser().getName();
    // make stuff available to all pages:
    context.put("datastates", new FieldMethodizer("de.suse.swamp.core.data.Data"));
    context.put("taskstates", new FieldMethodizer("de.suse.swamp.core.tasks.WorkflowTask"));
    // provide Bugzilla URL
    context.put("bugzilla_url", swampapi.doGetProperty("BUGZILLA_BROWSERURL", uname));
    // provide Session id:
    context.put("sessionid", data.getRequest().getRequestedSessionId());
    // pointer to Turbine configuration
    context.put("turbineconf", org.apache.turbine.Turbine.getConfiguration());
    context.put("adminEmail", swampapi.doGetProperty("POSTMASTER", uname));
    context.put("swampVersion", swampapi.doGetProperty("SWAMP_VERSION", uname));
    context.put("data", data);
    context.put("swampuser", data.getUser());
    context.put("date", new DateTool());

    // if there is a helppage available, propagade it.
    // may be overridden by special help pages, set from the templates
    ArrayList helps = new ArrayList();
    // there may already be helplinks
    if (context.get("helps") != null) {
        helps = (ArrayList) context.get("helps");
    }//  www.  j  a v a 2s . com
    ContextHelp help = new DocumentationAPI().getContextHelp("help." + data.getScreen(), uname);
    if (help != null) {
        helps.add(help);
        context.put("helps", helps);
    }

    // switch for print-view
    if (data.getParameters().containsKey("printview")) {
        data.setLayoutTemplate("PrintLayout.vm");
        context.put("printview", data.getParameters().get("printview"));
    }

    // store last page parameters temporary
    CircularFifoBuffer pageBuffer = (CircularFifoBuffer) data.getUser().getTemp("pageBuffer",
            new CircularFifoBuffer(2));

    org.apache.turbine.util.parser.ParameterParser params = data.getParameters();
    SWAMPHashMap map = new SWAMPHashMap();
    for (Iterator it = params.keySet().iterator(); it.hasNext();) {
        String key = (String) it.next();
        if (!key.equals("action")) {
            map.put(key, params.getObject(key));
        }
    }
    pageBuffer.add(map);
    data.getUser().setTemp("pageBuffer", pageBuffer);

}

From source file:eu.esdihumboldt.hale.ui.service.project.internal.RecentProjectsServiceImpl.java

private CircularFifoBuffer restoreState() {
    PreferencesService prefs = OsgiUtils.getService(PreferencesService.class);

    CircularFifoBuffer buffer = new CircularFifoBuffer(MAX_FILES);
    String configString = prefs.getSystemPreferences().get(CONFIG_PROPERTY, "");

    List<String> parts = Splitter.on(' ').splitToList(configString);

    buffer.clear();//from w  w  w  . j a va 2  s .  co m
    for (int i = 0; i < parts.size() - 1; i += 2) {
        try {
            String name = URLDecoder.decode(parts.get(i), ENC);
            String filename = URLDecoder.decode(parts.get(i + 1), ENC);

            Entry entry = new EntryImpl(filename, name);
            buffer.add(entry);
        } catch (UnsupportedEncodingException e) {
            log.error(ENC + "? That's supposed to be an encoding?", e);
        }
    }
    return buffer;
}

From source file:edu.berkeley.compbio.sequtils.strings.MarkovTreeNode.java

/**
 * Computes the total log probability of generating the given sequence fragment under the model.  This differs from
 * {@link #totalProbability(byte[])} in that the sequence fragment is not given explicitly but only as metadata.  Thus
 * its probability may be computed from summary statistics that are already available in the given SequenceFragment
 * rather than from the raw sequence.  Also, because these probabilities are typically very small, the result is
 * returned in log space (indeed implementations will likely compute them in log space).
 *
 * @param sequenceFragment the SequenceFragment whose probability is to be computed
 * @return the natural logarithm of the conditional probability (a double value between 0 and 1, inclusive)
 *///w  w w.j  a v  a2 s.co  m
public double fragmentLogProbability(final SequenceFragment sequenceFragment, final boolean perSample)
        throws SequenceSpectrumException {
    // the RonPSA implementation uses backlinks and so is vastly more efficient.
    // We can't use backlinks here because they might point to nodes outside of this subtree

    synchronized (sequenceFragment.getReaderForSynchronizing()) // because of resetting the reader
    {
        final SequenceReader in;
        try {
            in = sequenceFragment.getResetReader();
        } catch (NotEnoughSequenceException e) {
            throw new SequenceSpectrumRuntimeException(e);
        }
        final int requiredPrefixLength = getMaxDepth() - 1;
        double logprob = 0;
        final CircularFifoBuffer prefix = new CircularFifoBuffer(requiredPrefixLength);

        int samples = 0;
        while (true) {
            try {
                final byte c = in.read();

                try {
                    // PERF converting array prefix from circularFifoBuffer to byte[] is terribly inefficient
                    final byte[] prefixAsBytes = DSArrayUtils
                            .toPrimitive((Byte[]) prefix.toArray(new Byte[prefix.size()]));

                    // these log probabilities could be cached, e.g. logConditionalProbability(c, prefix)
                    logprob += MathUtils.approximateLog(conditionalProbability(c, prefixAsBytes));

                    samples++;

                    prefix.add(c);
                } catch (SequenceSpectrumException e) {
                    // probably just an invalid character
                    logger.debug("Invalid character " + (char) c);
                    // ignore this character as far as the probability is concerned
                    prefix.clear();
                }
            } catch (NotEnoughSequenceException e) {
                break;
            } catch (IOException e) {
                logger.error("Error", e);
                throw new SequenceSpectrumException(e);
            } catch (FilterException e) {
                logger.error("Error", e);
                throw new SequenceSpectrumException(e);
            }
        }

        if (perSample) {
            // we have ln(product(p) == sum(ln(p)).
            // The geometric mean is exp(sum(ln(p))/n), so to get ln(geometric mean) we need only divide by n.
            logprob /= samples;
        }

        return logprob;
    }
}

From source file:de.suse.swamp.modules.actions.WorkflowActions.java

/**
 * Is called when a task ok comes in./*ww w .j a  v  a2 s  .  c  om*/
 */
public void doTaskok(RunData data, Context context) throws Exception {
    Logger.LOG("doTaskok() from webapp.");
    SWAMPUser user = ((SWAMPTurbineUser) data.getUser()).getSWAMPUser();
    WorkflowAPI wfapi = new WorkflowAPI();
    TaskAPI taskapi = new TaskAPI();
    ResultList history = new ResultList();

    if (data.getParameters().containsKey("taskid")) {
        // get the task we're working on
        int taskId = data.getParameters().getInt("taskid");
        WorkflowTask task = taskapi.doGetTask(taskId, user.getUserName());
        String taskType = null;
        ArrayList validationErrors = new ArrayList();

        // Check for availability of that Task:
        if (task != null && task.getState() == WorkflowTask.ACTIVE) {
            // get the action type of the task
            taskType = task.getActionType();

            // fill in the result for different
            if (taskType.equals("manualtask")) {
                ManualtaskResult result = (ManualtaskResult) task.getResult();
                result.setDone(true);

            } else if (taskType.equals("decision")) {
                int answer = -1;
                // get the answer given
                if (data.getParameters().containsKey("answer")) {
                    answer = data.getParameters().getInt("answer");
                    Logger.DEBUG("Answer #" + answer);
                    // if no answer selected, log error
                } else {
                    Logger.ERROR("in doTaskok: no answer on question given.");
                }
                // put selection into result
                DecisionResult result = (DecisionResult) task.getResult();
                result.setSelection(answer);
            } else if (taskType.equals("dataedit")) {
                DataeditResult result = (DataeditResult) task.getResult();
                context.put("result", result);

                DataeditActionTemplate action = (DataeditActionTemplate) task.getActionTemplate();
                HashMap actionFields = action.getAllFields(task.getWorkflowId());
                Workflow wf = wfapi.getWorkflow(task.getWorkflowId(), user.getUserName());

                // put all values in the result object
                for (Iterator iter = actionFields.keySet().iterator(); iter.hasNext();) {
                    ArrayList setField = (ArrayList) actionFields.get(iter.next());
                    for (Iterator it = setField.iterator(); it.hasNext();) {
                        Field f = (Field) it.next();
                        String fieldpath = f.getPath();
                        String field = "field_" + fieldpath;
                        if (data.getParameters().containsKey(field)) {
                            // binary data need extra storage
                            if (f.getDatatype().equals("fileref")) {
                                FileItem value = data.getParameters().getFileItem(field);
                                Logger.DEBUG("Value for key (file)" + field + ": " + value);
                                // need to store the file now
                                Databit dbit = wf.getDatabit(fieldpath);
                                if (DatapackActions.storeFile(dbit, true, value, user.getUserName())) {
                                    String fileName = value.getName();
                                    // fix for browsers setting complete path as name: 
                                    if (fileName.indexOf("\\") >= 0)
                                        fileName = fileName.substring(fileName.lastIndexOf("\\") + 1);
                                    if (fileName.indexOf("/") >= 0)
                                        fileName = fileName.substring(fileName.lastIndexOf("/") + 1);
                                    result.setValue(fieldpath, fileName);
                                }
                            } else if (f.getDatatype().equalsIgnoreCase("multienum")) {
                                SWAMPHashSet values = new SWAMPHashSet(data.getParameters().getStrings(field));
                                result.setValue(fieldpath, values.toString(", "));
                            } else if (f.getDatatype().equalsIgnoreCase("patchdocumd")) {
                                String value = data.getParameters().get(field);
                                Logger.DEBUG("Value for key " + field + ": " + value);
                                result.setValue(fieldpath, value);
                            } else {
                                String value = StringEscapeUtils.unescapeHtml(data.getParameters().get(field));
                                Logger.DEBUG("Value for key " + field + ": " + value);
                                result.setValue(fieldpath, value);
                            }
                        } else if (data.getParameters().containsKey("boolean_" + fieldpath)) {
                            result.setValue(fieldpath, "false");
                        } else if (!f.isMandatory()) {
                            // don't complain about missing, non-mandatory fields
                        } else {
                            Logger.ERROR("Mandatory field " + fieldpath + " not set.");
                        }
                    }
                }
            }
            // validate task result
            validationErrors = task.validate();

            // if everything is ok, try to finish the task
            if (validationErrors.size() == 0) {
                try {
                    taskapi.finishTask(task, user.getUserName(), history);
                } catch (Exception e) {
                    e.printStackTrace();
                    validationErrors.add(e.getMessage());
                }
            }

            if (validationErrors.size() == 0) {
                Logger.LOG("Webapp: Done with working on task with id " + task.getId());

                WorkflowTask wftask = task;
                Workflow wf = wfapi.getWorkflow(wftask.getWorkflowId(), user.getUserName());

                context.put("statusheader", "Success");
                context.put("statusmessage", "Task \"" + task.getReplacedDescription() + "\" done in workflow "
                        + wf.getName() + ".");

                context.put("statusclass", "success");
                context.put("icon", "ok");
                context.put("history", history);
                context.put("workflow", wf);

                // add general Workflow Help
                SWAMPScreen.addHelplink(wf.getTemplate(), context, user.getUserName());
                ArrayList helps = new ArrayList();
                if (context.get("helps") != null) {
                    helps = (ArrayList) context.get("helps");
                }

                // add helplinks if there are new Tasks:
                if (wf.getActiveTasks().size() > 0) {
                    List activeTasks = wf.getActiveTasks();
                    for (Iterator it = activeTasks.iterator(); it.hasNext();) {
                        WorkflowTask helptask = (WorkflowTask) it.next();
                        String helpConext = helptask.getActionTemplate().getHelpContext();
                        if (helpConext != null && !helpConext.equals("")) {
                            ContextHelp help = new DocumentationAPI().getContextHelp(helpConext,
                                    user.getUserName());
                            if (help != null && !helps.contains(help)) {
                                helps.add(help);
                            }
                        }
                    }
                    context.put("helps", helps);
                }

                if (user.getPerm("taskpage", "results").equals("workflow")) {
                    Logger.DEBUG("Doing redirect to workflow page after task for " + user.getUserName());
                    setTemplate(data, "DisplayWorkflow.vm");
                } else if (user.getPerm("taskpage", "results").equals("previous")) {
                    CircularFifoBuffer pageBuffer = (CircularFifoBuffer) data.getUser().getTemp("pageBuffer",
                            new CircularFifoBuffer(2));
                    SWAMPHashMap params = (SWAMPHashMap) pageBuffer.get();
                    if (params != null && params.containsKey("template")) {
                        Logger.DEBUG("Redirect to previous page (" + params.get("template") + ") for "
                                + user.getUserName());
                        data.getParameters().clear();
                        for (Iterator it = params.keySet().iterator(); it.hasNext();) {
                            String key = (String) it.next();
                            data.getParameters().add(key, (String) params.get(key));
                        }
                        setTemplate(data, (String) params.get("template"));
                    } else {
                        Logger.WARN("Desired redirect not possible, no pageBuffer");
                    }
                }

                // if there were errors during validation, log the error
            } else {
                // go back to the Task-Page
                context.put("taskerror", "true");
                setTemplate(data, "DisplayTask.vm");

                Iterator errIter = validationErrors.iterator();
                String message = "", error;
                while (errIter.hasNext()) {
                    error = (String) errIter.next();
                    message = message + "<br />" + error;
                    Logger.ERROR(error);
                }
                message = message + "<p />Please correct the above mistake!";
                context.put("statusclass", "error");
                context.put("statusheader", "Error validating task");
                context.put("statusmessage", message);
                context.put("icon", "error");

                // fix page buffer
                CircularFifoBuffer pageBuffer = (CircularFifoBuffer) data.getUser().getTemp("pageBuffer",
                        new CircularFifoBuffer(2));
                pageBuffer.add(pageBuffer.get());
            } // end validation

        } else {
            // illegal task requested, redirect
            setTemplate(data, "DisplayTask.vm");
        }

    } else {
        Logger.ERROR("in doTaskok: no task id.");
    } //end taskid
}

From source file:org.outerrim.snippad.service.config.Configuration.java

/**
 * Resizes the Recent Documents list./*ww  w. j  a v a  2s.  co m*/
 *
 * @param newSize
 *            the new size of the list
 */
private void resizeRecentDocuments(final int newSize) {
    CircularFifoBuffer newBuffer = new CircularFifoBuffer(newSize);

    for (Iterator it = recentDocuments.iterator(); it.hasNext();) {
        newBuffer.add(it.next());
    }

    recentDocuments = newBuffer;
}

From source file:uk.ac.ebi.jmzml.xml.io.MzMLUnmarshaller.java

/**
 * Calcultes the check sum./*from ww w .j  ava2  s .co m*/
 *
 * @return the check sum as hexidecimal
 */
private String calculateChecksum() {
    // we have to create the checksum for the mzML file (from its beginning to the
    // end of the fileChecksum start tag).
    // Since this stop location is very near the end of the file, we skip everything
    // until we come within a certain limit of the end of the file
    long limit = mzMLFile.length() - 200L;
    logger.debug("Looking for fileChecksum tag between byte " + limit + " and byte " + mzMLFile.length()
            + " (the end) of the mzML file.");

    // initialize the hash algorithm
    MessageDigest hash;
    try {
        hash = MessageDigest.getInstance("SHA-1");
    } catch (NoSuchAlgorithmException e) {
        throw new IllegalStateException("SHA-1 not recognized as Secure Hash Algorithm.", e);
    }

    // create the input stream that will calculate the checksum
    FileInputStream fis;
    try {
        fis = new FileInputStream(mzMLFile);
    } catch (FileNotFoundException e) {
        throw new IllegalStateException("File " + mzMLFile.getAbsoluteFile() + " could not be found!", e);
    }
    BufferedInputStream bis = new BufferedInputStream(fis);
    DigestInputStream dis = new DigestInputStream(bis, hash);

    // prepare for input stream processing
    // we read through the file until we reach a specified limit before the end of the file
    // from there we populate a buffer with the read bytes (characters) and check if we have
    // already reached the position up to where we have to calculate the hash.
    CircularFifoBuffer bBuf = new CircularFifoBuffer(15);
    long cnt = 0; // counter to keep track of our position
    byte[] b = new byte[1]; // we only read one byte at a time
    try {
        while (dis.read(b) >= 0) {
            bBuf.add(b[0]);
            cnt++;
            // check if we have already reached the last bit of the file, where we have
            // to find the right position to stop (after the 'fileChecksum' start tag)
            if (cnt > limit) {
                // we should have reached the start of the <fileChecksum> tag,
                // now we have to find the end
                String readBuffer = convert2String(bBuf);
                if (readBuffer.endsWith("<fileChecksum>")) {
                    // we have found the end of the fileChecksum start tag, we have to stop the hash
                    if (b[0] != '>') { // check that we are really at the last character of the tag
                        throw new IllegalStateException("We are not at the end of <fileChecksum> tag!");
                    }
                    break;
                }
            } // else if not yet near the end of the file, just keep on going
        }
        dis.close();
    } catch (IOException e) {
        throw new IllegalStateException(
                "Could not read from file '" + mzMLFile.getAbsolutePath() + "' while trying ot calculate hash.",
                e);
    }
    logger.debug("Read over " + cnt + " bytes while calculating the file hash.");

    byte[] bytesDigest = dis.getMessageDigest().digest();

    return asHex(bytesDigest);
}