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

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

Introduction

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

Prototype

public int size() 

Source Link

Document

Returns the number of elements stored in the buffer.

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 .  ja v  a2 s .  co m*/
                .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.neophob.sematrix.core.jmx.PixelControllerStatus.java

/**
 * Gets the average buffer value.//from ww  w . j  a v a2s .  co  m
 *
 * @param circularFifoBuffer the circular fifo buffer
 * @return returns average value of all buffer entries
 */
private static float getAverageBufferValue(CircularFifoBuffer circularFifoBuffer) {
    // handle null instance
    if (circularFifoBuffer == null) {
        return 0f;
    }
    // calculate sum of all buffer values
    float bufferSum = 0f;
    @SuppressWarnings("rawtypes")
    Iterator iterator = circularFifoBuffer.iterator();
    while (iterator.hasNext()) {
        bufferSum += (Long) iterator.next();
    }
    // return average value
    float result = bufferSum / circularFifoBuffer.size();
    if (Float.isNaN(result)) {
        result = 0f;
    }
    return result;
}

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

@Override
public Entry[] getRecentFiles() {
    CircularFifoBuffer buffer = restoreState();

    Entry[] result = new Entry[buffer.size()];
    int i = 0;//w  w w .  j  a  v a2  s . c o  m
    for (Object o : buffer)
        result[i++] = (Entry) o;
    return result;
}

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  ww.  j  a va 2 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:uk.ac.ebi.jmzml.xml.io.MzMLUnmarshaller.java

/**
 * TODO: Javadoc missing// ww  w  . j a  v  a  2s  .c o m
 *
 * @param bBuf
 * @return
 */
private String convert2String(CircularFifoBuffer bBuf) {
    byte[] tmp = new byte[bBuf.size()];
    int tmpCnt = 0;
    for (Object aBBuf : bBuf) {
        tmp[tmpCnt++] = (Byte) aBBuf;
    }
    return new String(tmp);
}