/** * Resets all statistics and storage */ public void clear() { eDA.clear(); }
/** * Returns the current set of values in an array of double primitives. * The order of addition is preserved. The returned array is a fresh * copy of the underlying data -- i.e., it is not a reference to the * stored data. * * @return returns the current set of numbers in the order in which they * were added to this set */ public double[] getValues() { double[] copiedArray = new double[eDA.getNumElements()]; System.arraycopy(eDA.getElements(), 0, copiedArray, 0, eDA.getNumElements()); return copiedArray; }
/** * Sets the contraction criteria for this ExpandContractDoubleArray. * * @param contractionCriteria contraction criteria */ public void setContractionCriteria(float contractionCriteria) { checkContractExpand(contractionCriteria, getExpansionFactor()); this.contractionCriteria = contractionCriteria; }
/** * Adds an element to the end of this expandable array. * * @param value to be added to end of array */ public synchronized void addElement(double value) { numElements++; if ((startIndex + numElements) > internalArray.length) { expand(); } internalArray[startIndex + (numElements - 1)] = value; if (shouldContract()) { contract(); } }
/** * Sets the expansionFactor. Throws IllegalArgumentException if the * the following conditions are not met: * <ul> * <li><code>expansionFactor > 1</code></li> * <li><code>contractionFactor >= expansionFactor</code></li> * </ul> * @param expansionFactor the new expansion factor value. * @throws IllegalArgumentException if expansionFactor is <= 1 or greater * than contractionFactor */ public void setExpansionFactor(float expansionFactor) { checkContractExpand(getContractionCriteria(), expansionFactor); // The check above verifies that the expansion factor is > 1.0; this.expansionFactor = expansionFactor; }
/** * Adds the value to the dataset. If the dataset is at the maximum size * (i.e., the number of stored elements equals the currently configured * windowSize), the first (oldest) element in the dataset is discarded * to make room for the new value. * * @param v the value to be added */ public void addValue(double v) { if (windowSize != INFINITE_WINDOW) { if (getN() == windowSize) { eDA.addElementRolling(v); } else if (getN() < windowSize) { eDA.addElement(v); } } else { eDA.addElement(v); } }
/** * WindowSize controls the number of values which contribute * to the reported statistics. For example, if * windowSize is set to 3 and the values {1,2,3,4,5} * have been added <strong> in that order</strong> * then the <i>available values</i> are {3,4,5} and all * reported statistics will be based on these values * @param windowSize sets the size of the window. */ public void setWindowSize(int windowSize) { if (windowSize < 1) { if (windowSize != INFINITE_WINDOW) { throw new IllegalArgumentException("window size must be positive."); } } this.windowSize = windowSize; // We need to check to see if we need to discard elements // from the front of the array. If the windowSize is less than // the current number of elements. if (windowSize != INFINITE_WINDOW && windowSize < eDA.getNumElements()) { eDA.discardFrontElements(eDA.getNumElements() - windowSize); } }
/** * Discards the <code>i<code> initial elements of the array. For example, * if the array contains the elements 1,2,3,4, invoking * <code>discardFrontElements(2)</code> will cause the first two elements * to be discarded, leaving 3,4 in the array. Throws illegalArgumentException * if i exceeds numElements. * * @param i the number of elements to discard from the front of the array * @throws IllegalArgumentException if i is greater than numElements. */ public synchronized void discardFrontElements(int i) { if (i > numElements) { String msg = "Cannot discard more elements than are" + "contained in this array."; throw new IllegalArgumentException(msg); } else if (i < 0) { String msg = "Cannot discard a negative number of elements."; throw new IllegalArgumentException(msg); } else { // "Subtract" this number of discarded from numElements numElements -= i; startIndex += i; } if (shouldContract()) { contract(); } }
/** * This function allows you to control the number of elements contained * in this array, and can be used to "throw out" the last n values in an * array. This function will also expand the internal array as needed. * * @param i a new number of elements * @throws IllegalArgumentException if <code>i</code> is negative. */ public synchronized void setNumElements(int i) { // If index is negative thrown an error if (i < 0) { String msg = "Number of elements must be zero or a positive " + "integer"; throw new IllegalArgumentException(msg); } // Test the new num elements, check to see if the array needs to be // expanded to accomodate this new number of elements if ((startIndex + i) > internalArray.length) { expandTo(startIndex + i); } // Set the new number of elements to new value numElements = i; }
/** * Returns the current set of values in an array of double primitives. * The order of addition is preserved. The returned array is a fresh * copy of the underlying data -- i.e., it is not a reference to the * stored data. * * @return returns the current set of numbers in the order in which they * were added to this set */ public double[] getValues() { return eDA.getElements(); }
/** * Removes the most recent value from the dataset. */ public void removeMostRecentValue() { eDA.discardMostRecentElements(1); }
/** * Adds an element to the end of this expandable array. * * @param value to be added to end of array */ public synchronized void addElement(double value) { numElements++; if ((startIndex + numElements) > internalArray.length) { expand(); } internalArray[startIndex + (numElements - 1)] = value; if (shouldContract()) { contract(); } }
/** * Adds the value to the dataset. If the dataset is at the maximum size * (i.e., the number of stored elements equals the currently configured * windowSize), the first (oldest) element in the dataset is discarded * to make room for the new value. * * @param v the value to be added */ public void addValue(double v) { if (windowSize != INFINITE_WINDOW) { if (getN() == windowSize) { eDA.addElementRolling(v); } else if (getN() < windowSize) { eDA.addElement(v); } } else { eDA.addElement(v); } }
/** * Sets the expansionFactor. Throws IllegalArgumentException if the * the following conditions are not met: * <ul> * <li><code>expansionFactor > 1</code></li> * <li><code>contractionFactor >= expansionFactor</code></li> * </ul> * @param expansionFactor the new expansion factor value. * @throws IllegalArgumentException if expansionFactor is <= 1 or greater * than contractionFactor */ public void setExpansionFactor(float expansionFactor) { checkContractExpand(getContractionCriteria(), expansionFactor); // The check above verifies that the expansion factor is > 1.0; synchronized(this) { this.expansionFactor = expansionFactor; } }
/** * WindowSize controls the number of values which contribute * to the reported statistics. For example, if * windowSize is set to 3 and the values {1,2,3,4,5} * have been added <strong> in that order</strong> * then the <i>available values</i> are {3,4,5} and all * reported statistics will be based on these values * @param windowSize sets the size of the window. */ public void setWindowSize(int windowSize) { if (windowSize < 1) { if (windowSize != INFINITE_WINDOW) { throw MathRuntimeException.createIllegalArgumentException( LocalizedFormats.NOT_POSITIVE_WINDOW_SIZE, windowSize); } } this.windowSize = windowSize; // We need to check to see if we need to discard elements // from the front of the array. If the windowSize is less than // the current number of elements. if (windowSize != INFINITE_WINDOW && windowSize < eDA.getNumElements()) { eDA.discardFrontElements(eDA.getNumElements() - windowSize); } }
if (front) startIndex += i; if (shouldContract()) { contract();
/** * Sets the element at the specified index. If the specified index is greater than * <code>getNumElements() - 1</code>, the <code>numElements</code> property * is increased to <code>index +1</code> and additional storage is allocated * (if necessary) for the new element and all (uninitialized) elements * between the new element and the previous end of the array). * * @param index index to store a value in * @param value value to store at the specified index * @throws ArrayIndexOutOfBoundsException if <code>index</code> is less than * zero. */ public synchronized void setElement(int index, double value) { if (index < 0) { String msg = "Cannot set an element at a negative index"; throw new ArrayIndexOutOfBoundsException(msg); } if (index + 1 > numElements) { numElements = index + 1; } if ((startIndex + index) >= internalArray.length) { expandTo(startIndex + (index + 1)); } internalArray[startIndex + index] = value; }
/** * Returns the current set of values in an array of double primitives. * The order of addition is preserved. The returned array is a fresh * copy of the underlying data -- i.e., it is not a reference to the * stored data. * * @return returns the current set of numbers in the order in which they * were added to this set */ public double[] getValues() { return eDA.getElements(); }
/** * Removes the most recent value from the dataset. */ public void removeMostRecentValue() { eDA.discardMostRecentElements(1); }
/** * Adds an element to the end of this expandable array. * * @param value to be added to end of array */ public synchronized void addElement(double value) { numElements++; if ((startIndex + numElements) > internalArray.length) { expand(); } internalArray[startIndex + (numElements - 1)] = value; if (shouldContract()) { contract(); } }