You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by md...@apache.org on 2003/07/05 20:19:20 UTC
cvs commit: jakarta-commons-sandbox/math/src/java/org/apache/commons/math/util ContractableDoubleArray.java ExpandableDoubleArray.java
mdiggory 2003/07/05 11:19:20
Modified: math/src/java/org/apache/commons/math/util
ContractableDoubleArray.java
ExpandableDoubleArray.java
Log:
This patch exposes the start index, internal double array of DOubleArray Implementations.
It adds a NumberTransformer framework for mapping Objects and Bean Properties to double primitives
It also corrects some checkstyle and javadoc errors.
Revision Changes Path
1.2 +41 -34 jakarta-commons-sandbox/math/src/java/org/apache/commons/math/util/ContractableDoubleArray.java
Index: ContractableDoubleArray.java
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/math/src/java/org/apache/commons/math/util/ContractableDoubleArray.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- ContractableDoubleArray.java 22 Jun 2003 03:57:57 -0000 1.1
+++ ContractableDoubleArray.java 5 Jul 2003 18:19:20 -0000 1.2
@@ -90,14 +90,15 @@
*
* @author <a href="mailto:tobrien@apache.org">Tim O'Brien</a>
*/
-public class ContractableDoubleArray
- extends ExpandableDoubleArray
+public class ContractableDoubleArray
+ extends ExpandableDoubleArray
implements Serializable {
- // The contraction criteria defines the conditions under which this
- // object will "resize" the internal array to the number of elements
- // contained in the element array + 1
- protected float contractionCriteria = 2.5f;
+ /** The contraction criteria defines the conditions under which this
+ * object will "resize" the internal array to the number of elements
+ * contained in the element array + 1
+ */
+ private float contractionCriteria = 2.5f;
/**
* Create an expandable double array with the default initial capacity of
@@ -125,8 +126,9 @@
* @param expansionFactor the array will be expanded based on this
* parameter
*/
- public ContractableDoubleArray(int initialCapacity,
- float expansionFactor) {
+ public ContractableDoubleArray(
+ int initialCapacity,
+ float expansionFactor) {
this.expansionFactor = expansionFactor;
setInitialCapacity(initialCapacity);
internalArray = new double[initialCapacity];
@@ -140,10 +142,12 @@
* @param initialCapacity The initial size of the internal storage array
* @param expansionFactor the array will be expanded based on this
* parameter
+ * @param contractionCriteria The contraction Criteria.
*/
- public ContractableDoubleArray(int initialCapacity,
- float expansionFactor,
- float contractionCriteria) {
+ public ContractableDoubleArray(
+ int initialCapacity,
+ float expansionFactor,
+ float contractionCriteria) {
this.contractionCriteria = contractionCriteria;
this.expansionFactor = expansionFactor;
setInitialCapacity(initialCapacity);
@@ -160,7 +164,7 @@
double[] tempArray = new double[numElements + 1];
// Copy and swap - copy only the element array from the src array.
- System.arraycopy(internalArray,startIndex,tempArray,0,numElements);
+ System.arraycopy(internalArray, startIndex, tempArray, 0, numElements);
internalArray = tempArray;
// Reset the start index to zero
@@ -198,8 +202,8 @@
* gradually push the internal storage vs. element storage to
* the contractionCriteria.
* </p>
- *
- * @return value to be added to end of array
+ * @param value to be added to end of array
+ * @return value added
*/
public synchronized double addElementRolling(double value) {
double discarded = super.addElementRolling(value);
@@ -227,8 +231,8 @@
return shouldContract;
}
- /* (non-Javadoc)
- * @see org.apache.commons.math.ExpandableDoubleArray#setElement(int, double)
+ /**
+ * @see org.apache.commons.math.util.DoubleArray#setElement(int, double)
*/
public synchronized void setElement(int index, double value) {
super.setElement(index, value);
@@ -279,38 +283,41 @@
* IllegalArgumentException if the contractionCriteria is less than the
* expansionCriteria
*
- * @param expansionFactor
- * @param contractionCritera
+ * @param expansionFactor factor to be checked
+ * @param contractionCritera critera to be checked
*/
- protected void checkContractExpand(float contractionCritera,
- float expansionFactor) {
+ protected void checkContractExpand(
+ float contractionCritera,
+ float expansionFactor) {
if (contractionCritera < expansionFactor) {
- String msg = "Contraction criteria can never be smaller than " +
- "the expansion factor. This would lead to a never ending " +
- "loop of expansion and contraction as a newly expanded " +
- "internal storage array would immediately satisfy the " +
- "criteria for contraction";
+ String msg =
+ "Contraction criteria can never be smaller than "
+ + "the expansion factor. This would lead to a never "
+ + "ending loop of expansion and contraction as a newly "
+ + "expanded internal storage array would immediately "
+ + "satisfy the criteria for contraction";
throw new IllegalArgumentException(msg);
}
if (contractionCriteria <= 1.0) {
- String msg = "The contraction criteria must be a number larger " +
- "than one. If the contractionCriteria is less than or " +
- "equal to one an endless loop of contraction and expansion " +
- "would ensue as an internalArray.length == numElements " +
- "would satisfy the contraction criteria";
- throw new IllegalArgumentException(msg);
+ String msg =
+ "The contraction criteria must be a number larger "
+ + "than one. If the contractionCriteria is less than or "
+ + "equal to one an endless loop of contraction and "
+ + "expansion would ensue as an internalArray.length "
+ + "== numElements would satisfy the contraction criteria";
+ throw new IllegalArgumentException(msg);
}
if (expansionFactor < 1.0) {
- String msg = "The expansion factor must be a number greater " +
- "than 1.0";
+ String msg =
+ "The expansion factor must be a number greater " + "than 1.0";
throw new IllegalArgumentException(msg);
}
}
- /* (non-Javadoc)
+ /**
* @see org.apache.commons.math.ExpandableDoubleArray#discardFrontElements(int)
*/
public synchronized void discardFrontElements(int i) {
1.3 +61 -37 jakarta-commons-sandbox/math/src/java/org/apache/commons/math/util/ExpandableDoubleArray.java
Index: ExpandableDoubleArray.java
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/math/src/java/org/apache/commons/math/util/ExpandableDoubleArray.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- ExpandableDoubleArray.java 27 Jun 2003 20:58:27 -0000 1.2
+++ ExpandableDoubleArray.java 5 Jul 2003 18:19:20 -0000 1.3
@@ -99,22 +99,30 @@
// should have flags for incremental growth - (i.e. when expanding, only
// increase storage by a constant size - 100, 200 ) ?
- // This is the internal storage array.
+ /**
+ * This is the internal storage array.
+ */
protected double[] internalArray;
- // Number of elements in the array
+ /**
+ * Number of elements in the array
+ */
protected int numElements = 0;
- // Keeps track of a starting index
+ /**
+ * Keeps track of a starting index
+ */
protected int startIndex = 0;
- // The initial capacity of the array.
- // Initial capacity is not exposed as a property as it is only meaningful
- // when passed to a constructor.
+ /**The initial capacity of the array.
+ * Initial capacity is not exposed as a property as it is only meaningful
+ * when passed to a constructor.
+ */
protected int initialCapacity = 16;
- // The expand factor of the array. When the array need to be expanded,
- // the new array size will be internalArray.length * expandFactor
+ /** The expand factor of the array. When the array need to be expanded,
+ * the new array size will be internalArray.length * expandFactor
+ */
protected float expansionFactor = 2.0f;
/**
@@ -177,23 +185,24 @@
if (expansionFactor > 1.0) {
this.expansionFactor = expansionFactor;
} else {
- String msg = "The expansion factor must be a number greater " +
- "than 1.0";
+ String msg =
+ "The expansion factor must be a number greater " + "than 1.0";
throw new IllegalArgumentException(msg);
}
}
/**
* Sets the initial capacity
- *
- * @param initialCapacity
+ * @param initialCapacity of the array
*/
public void setInitialCapacity(int initialCapacity) {
if (initialCapacity > 0) {
this.initialCapacity = initialCapacity;
} else {
- String msg = "The initial capacity supplied: " + initialCapacity +
- "must be a positive integer";
+ String msg =
+ "The initial capacity supplied: "
+ + initialCapacity
+ + "must be a positive integer";
throw new IllegalArgumentException(msg);
}
}
@@ -203,11 +212,19 @@
*
* @return the internal storage array used by this object
*/
- protected double[] getValues() {
+ public double[] getValues() {
return (internalArray);
}
/**
+ * Returns the starting index of the internal array.
+ * @return starting index
+ */
+ public int start() {
+ return startIndex;
+ }
+
+ /**
* Returns the number of elements currently in the array. Please note
* that this is different from the length of the internal storage array.
* @return number of elements
@@ -228,9 +245,9 @@
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";
+ if (i < 0) {
+ String msg =
+ "Number of elements must be zero or a positive " + "integer";
throw new IllegalArgumentException(msg);
}
@@ -253,14 +270,16 @@
public double getElement(int index) {
double value = Double.NaN;
if (index >= numElements) {
- String msg = "The index specified: " + index +
- " is larger than the current number of elements";
+ String msg =
+ "The index specified: "
+ + index
+ + " is larger than the current number of elements";
throw new ArrayIndexOutOfBoundsException(msg);
} else if (index >= 0) {
value = internalArray[startIndex + index];
} else {
- String msg = "Elements cannot be retrieved from a negative " +
- "array index";
+ String msg =
+ "Elements cannot be retrieved from a negative " + "array index";
throw new ArrayIndexOutOfBoundsException(msg);
}
return value;
@@ -275,8 +294,8 @@
* @param value value to store at the specified index
*/
public synchronized void setElement(int index, double value) {
-
- if (index < 0) {
+
+ if (index < 0) {
String msg = "Cannot set an element at a negative index";
throw new ArrayIndexOutOfBoundsException(msg);
}
@@ -296,7 +315,7 @@
private synchronized void expandTo(int size) {
double[] tempArray = new double[size];
// Copy and swap
- System.arraycopy(internalArray,0,tempArray,0,internalArray.length);
+ System.arraycopy(internalArray, 0, tempArray, 0, internalArray.length);
internalArray = tempArray;
}
@@ -305,10 +324,10 @@
*/
protected synchronized void expand() {
- // notice the use of Math.ceil(), this gaurantees that we will always
+ // notice the use of Math.ceil(), this gaurantees that we will always
// have an array of at least currentSize + 1. Assume that the
// current initial capacity is 1 and the expansion factor
- // is 1.000000000000000001. The newly calculated size will be
+ // is 1.000000000000000001. The newly calculated size will be
// rounded up to 2 after the multiplication is performed.
int newSize = (int) Math.ceil(internalArray.length * expansionFactor);
double[] tempArray = new double[newSize];
@@ -336,14 +355,14 @@
* has the effect of a FIFO. when you "roll" the array an element is
* removed from the array. The return value of this function is the
* discarded double.
- *
- * @return the value which has been discarded or "pushed" out of the array
- * by this rolling insert.
+ * @param value the value to add
+ * @return the value which has been discarded or "pushed" out of the array
+ * by this rolling insert.
*/
public synchronized double addElementRolling(double value) {
double discarded = internalArray[startIndex];
- if ((startIndex + (numElements+1)) > internalArray.length) {
+ if ((startIndex + (numElements + 1)) > internalArray.length) {
expand();
}
// Increment the start index
@@ -366,7 +385,7 @@
int getInternalLength() {
return (internalArray.length);
}
-
+
/**
* Clear the array, reset the size to the initialCapacity and the number
* of elements to zero.
@@ -385,8 +404,9 @@
public synchronized void discardFrontElements(int i) {
if (i > numElements) {
- String msg = "Cannot discard more elements than are" +
- "contained in this array.";
+ 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.";
@@ -398,13 +418,17 @@
}
}
- /* (non-Javadoc)
+ /**
* @see org.apache.commons.math.DoubleArray#getElements()
*/
public double[] getElements() {
double[] elementArray = new double[numElements];
- System.arraycopy(internalArray, startIndex,
- elementArray, 0, numElements);
+ System.arraycopy(
+ internalArray,
+ startIndex,
+ elementArray,
+ 0,
+ numElements);
return elementArray;
}
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org