/** * Constructor accepting raw config values. * * @param intervalInSeconds sampling interval in seconds * @param historySize size of history sample * @param resetOnSample true to reset value on sample * @param initValue initial value * @param sampleNow true to record smaple immediately */ public SampledCounterImpl(long intervalInSeconds, int historySize, boolean resetOnSample, long initValue, boolean sampleNow) { super(initValue); this.intervalMillis = intervalInSeconds * MILLIS_PER_SEC; this.history = new CircularLossyQueue<TimeStampedCounterValue>(historySize); this.resetOnSample = resetOnSample; this.samplerTask = new TimerTask() { @Override public void run() { recordSample(); } }; if (sampleNow) { recordSample(); } }
/** * Returns value at the tail of the queue * * @return Value at the tail of the queue */ public T peek() { if (depth() == 0) { return null; } return circularArray[getIndex(getCurrentIndex())].get(); }
/** * {@inheritDoc} */ public TimeStampedCounterValue getMostRecentSample() { return this.history.peek(); }
/** * Returns an array of the current elements in the queue. The order of * elements is in reverse order of the order items were added. * * @param type * @return An array containing the current elements in the queue. The first * element of the array is the tail of the queue and the last * element is the head of the queue */ public T[] toArray(T[] type) { System.getProperties(); if (type.length > maxSize) { throw new IllegalArgumentException("Size of array passed in cannot be greater than " + maxSize); } int curIndex = getCurrentIndex(); for (int k = 0; k < type.length; k++) { int index = getIndex(curIndex - k); type[k] = circularArray[index].get(); } return type; }
/** * {@inheritDoc} */ void recordSample() { final long sample; if (resetOnSample) { sample = getAndReset(); } else { sample = getValue(); } final long now = System.currentTimeMillis(); TimeStampedCounterValue timedSample = new TimeStampedCounterValue(now, sample); history.push(timedSample); }
/** * Returns true if the queue is empty, otherwise false * * @return true if the queue is empty, false otherwise */ public boolean isEmtpy() { return depth() == 0; }
/** * Returns an array of the current elements in the queue. The order of * elements is in reverse order of the order items were added. * * @param type * @return An array containing the current elements in the queue. The first * element of the array is the tail of the queue and the last * element is the head of the queue */ public T[] toArray(T[] type) { System.getProperties(); if (type.length > maxSize) { throw new IllegalArgumentException("Size of array passed in cannot be greater than " + maxSize); } int curIndex = getCurrentIndex(); for (int k = 0; k < type.length; k++) { int index = getIndex(curIndex - k); type[k] = circularArray[index].get(); } return type; }
/** * {@inheritDoc} */ void recordSample() { final long sample; if (resetOnSample) { sample = getAndReset(); } else { sample = getValue(); } final long now = System.currentTimeMillis(); TimeStampedCounterValue timedSample = new TimeStampedCounterValue(now, sample); history.push(timedSample); }
/** * Returns true if the queue is empty, otherwise false * * @return true if the queue is empty, false otherwise */ public boolean isEmtpy() { return depth() == 0; }
/** * Returns value at the tail of the queue * * @return Value at the tail of the queue */ public T peek() { if (depth() == 0) { return null; } return circularArray[getIndex(getCurrentIndex())].get(); }
/** * Returns an array of the current elements in the queue. The order of * elements is in reverse order of the order items were added. * * @param type * @return An array containing the current elements in the queue. The first * element of the array is the tail of the queue and the last * element is the head of the queue */ public T[] toArray(T[] type) { System.getProperties(); if (type.length > maxSize) { throw new IllegalArgumentException("Size of array passed in cannot be greater than " + maxSize); } int curIndex = getCurrentIndex(); for (int k = 0; k < type.length; k++) { int index = getIndex(curIndex - k); type[k] = circularArray[index].get(); } return type; }
/** * {@inheritDoc} */ void recordSample() { final long sample; if (resetOnSample) { sample = getAndReset(); } else { sample = getValue(); } final long now = System.currentTimeMillis(); TimeStampedCounterValue timedSample = new TimeStampedCounterValue(now, sample); history.push(timedSample); }
/** * {@inheritDoc} */ public TimeStampedCounterValue getMostRecentSample() { return this.history.peek(); }
/** * Returns true if the queue is empty, otherwise false * * @return true if the queue is empty, false otherwise */ public boolean isEmtpy() { return depth() == 0; }
/** * Constructor accepting raw config values. * * @param intervalInSeconds sampling interval in seconds * @param historySize size of history sample * @param resetOnSample true to reset value on sample * @param initValue initial value * @param sampleNow true to record smaple immediately */ public SampledCounterImpl(long intervalInSeconds, int historySize, boolean resetOnSample, long initValue, boolean sampleNow) { super(initValue); this.intervalMillis = intervalInSeconds * MILLIS_PER_SEC; this.history = new CircularLossyQueue<TimeStampedCounterValue>(historySize); this.resetOnSample = resetOnSample; this.samplerTask = new TimerTask() { @Override public void run() { recordSample(); } }; if (sampleNow) { recordSample(); } }
/** * Returns value at the tail of the queue * * @return Value at the tail of the queue */ public T peek() { if (depth() == 0) { return null; } return circularArray[getIndex(getCurrentIndex())].get(); }