From 32d47dd08750c603eac2f41ace3b13c79cec3c4a Mon Sep 17 00:00:00 2001
From: Gilles Sadowski
Date: Tue, 13 Nov 2012 16:53:13 +0000
Subject: [PATCH] Use constructor chaining (in place of duplicate code). Fixed
Javadoc typos (expansion factor and contraction criterion default values).
Javadoc formatting.
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@1408830 13f79535-47bb-0310-9956-ffa450edef68
---
.../math3/util/ResizableDoubleArray.java | 171 ++++++++++--------
1 file changed, 92 insertions(+), 79 deletions(-)
diff --git a/src/main/java/org/apache/commons/math3/util/ResizableDoubleArray.java b/src/main/java/org/apache/commons/math3/util/ResizableDoubleArray.java
index 2dcf8d959..0369d1326 100644
--- a/src/main/java/org/apache/commons/math3/util/ResizableDoubleArray.java
+++ b/src/main/java/org/apache/commons/math3/util/ResizableDoubleArray.java
@@ -23,6 +23,7 @@ import org.apache.commons.math3.exception.MathIllegalArgumentException;
import org.apache.commons.math3.exception.MathIllegalStateException;
import org.apache.commons.math3.exception.MathInternalError;
import org.apache.commons.math3.exception.NullArgumentException;
+import org.apache.commons.math3.exception.NotStrictlyPositiveException;
import org.apache.commons.math3.exception.util.LocalizedFormats;
/**
@@ -89,6 +90,11 @@ public class ResizableDoubleArray implements DoubleArray, Serializable {
/** Serializable version identifier. */
private static final long serialVersionUID = -3485529955529426875L;
+ /** Default value for initial capacity. */
+ private static final int DEFAULT_INITIAL_CAPACITY = 16;
+ /** Default value for initial capacity. */
+ private static final float DEFAULT_EXPANSION_FACTOR = 2.0f;
+
/**
* The contraction criteria determines when the internal array will be
* contracted to fit the number of elements contained in the element
@@ -148,116 +154,118 @@ public class ResizableDoubleArray implements DoubleArray, Serializable {
}
/**
- * Create a ResizableArray with default properties.
+ * Creates an instance with default properties.
*
- * initialCapacity = 16
- * expansionMode = MULTIPLICATIVE_MODE
- * expansionFactor = 2.5
- * contractionFactor = 2.0
+ * - {@code initialCapacity = 16}
+ * - {@code expansionMode = MULTIPLICATIVE}
+ * - {@code expansionFactor = 2.0}
+ * - {@code contractionFactor = 2.5}
*
*/
- public ResizableDoubleArray() {
- internalArray = new double[initialCapacity];
+ public ResizableDoubleArray()
+ throws MathIllegalArgumentException {
+ this(DEFAULT_INITIAL_CAPACITY);
}
/**
- * Create a ResizableArray with the specified initial capacity. Other
- * properties take default values:
- *
- * expansionMode = MULTIPLICATIVE_MODE
- * expansionFactor = 2.5
- * contractionFactor = 2.0
+ * Creates an instance with the specified initial capacity.
+ * Other properties take default values:
+ *
+ * - {@code expansionMode = MULTIPLICATIVE}
+ * - {@code expansionFactor = 2.0}
+ * - {@code contractionFactor = 2.5}
*
- * @param initialCapacity The initial size of the internal storage array
- * @throws MathIllegalArgumentException if initialCapacity is not > 0
+ * @param initialCapacity Initial size of the internal storage array.
+ * @throws MathIllegalArgumentException if {@code initialCapacity <= 0}.
*/
- public ResizableDoubleArray(int initialCapacity) throws MathIllegalArgumentException {
- setInitialCapacity(initialCapacity);
- internalArray = new double[this.initialCapacity];
+ public ResizableDoubleArray(int initialCapacity)
+ throws MathIllegalArgumentException {
+ this(initialCapacity, DEFAULT_EXPANSION_FACTOR);
}
/**
- * Create a ResizableArray from an existing double[] with the
+ * Creates an instance from an existing {@code double[]} with the
* initial capacity and numElements corresponding to the size of
- * the supplied double[] array. If the supplied array is null, a
- * new empty array with the default initial capacity will be created.
+ * the supplied {@code double[]} array.
+ * If the supplied array is null, a new empty array with the default
+ * initial capacity will be created.
* The input array is copied, not referenced.
* Other properties take default values:
*
- * initialCapacity = 16
- * expansionMode = MULTIPLICATIVE_MODE
- * expansionFactor = 2.5
- * contractionFactor = 2.0
+ * - {@code initialCapacity = 16}
+ * - {@code expansionMode = MULTIPLICATIVE}
+ * - {@code expansionFactor = 2.0}
+ * - {@code contractionFactor = 2.5}
*
*
* @param initialArray initial array
* @since 2.2
*/
public ResizableDoubleArray(double[] initialArray) {
- if (initialArray == null) {
- this.internalArray = new double[initialCapacity];
- } else {
- this.internalArray = new double[initialArray.length];
- System.arraycopy(initialArray, 0, this.internalArray, 0, initialArray.length);
- initialCapacity = initialArray.length;
- numElements = initialArray.length;
- }
+ this(DEFAULT_INITIAL_CAPACITY,
+ DEFAULT_EXPANSION_FACTOR,
+ 0.5f + DEFAULT_EXPANSION_FACTOR,
+ ExpansionMode.MULTIPLICATIVE,
+ initialArray);
}
/**
- *
- * Create a ResizableArray with the specified initial capacity
- * and expansion factor. The remaining properties take default
- * values:
+ * Creates an instance with the specified initial capacity
+ * and expansion factor.
+ * The remaining properties take default values:
*
- * expansionMode = MULTIPLICATIVE_MODE
- * contractionFactor = 0.5 + expansionFactor
- *
- *
+ *
{@code expansionMode = MULTIPLICATIVE}
+ * {@code contractionFactor = 0.5 + expansionFactor}
+ *
+ *
* Throws IllegalArgumentException if the following conditions are
* not met:
*
- * initialCapacity > 0
- * expansionFactor > 1
- *
+ * {@code initialCapacity > 0}
+ * {@code expansionFactor > 1}
+ *
*
- * @param initialCapacity The initial size of the internal storage array
- * @param expansionFactor the array will be expanded based on this
- * parameter
- * @throws MathIllegalArgumentException if parameters are not valid
+ * @param initialCapacity Initial size of the internal storage array.
+ * @param expansionFactor The array will be expanded based on this
+ * parameter.
+ * @throws MathIllegalArgumentException if parameters are not valid.
*/
- public ResizableDoubleArray(int initialCapacity, float expansionFactor) throws MathIllegalArgumentException {
- this.expansionFactor = expansionFactor;
- setInitialCapacity(initialCapacity);
- internalArray = new double[initialCapacity];
- setContractionCriteria(expansionFactor +0.5f);
+ public ResizableDoubleArray(int initialCapacity,
+ float expansionFactor)
+ throws MathIllegalArgumentException {
+ this(initialCapacity,
+ expansionFactor,
+ 0.5f + expansionFactor);
}
/**
- *
- * Create a ResizableArray with the specified initialCapacity,
- * expansionFactor, and contractionCriteria. The expansionMode
- * will default to MULTIPLICATIVE_MODE.
- *
+ * Creates an instance with the specified initialCapacity,
+ * expansionFactor, and contractionCriteria.
+ * The expansion mode will default to {@code MULTIPLICATIVE}.
+ *
* Throws IllegalArgumentException if the following conditions are
* not met:
*
- * initialCapacity > 0
- * expansionFactor > 1
- * contractionFactor >= expansionFactor
- *
- * @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.
- * @throws MathIllegalArgumentException if parameters are not valid
+ * {@code initialCapacity > 0}
+ * {@code expansionFactor > 1}
+ * {@code contractionFactor >= expansionFactor}
+ *
+ *
+ * @param initialCapacity Initial size of the internal storage array..
+ * @param expansionFactor The array will be expanded based on this
+ * parameter.
+ * @param contractionCriteria Contraction criteria.
+ * @throws MathIllegalArgumentException if parameters are not valid.
*/
- public ResizableDoubleArray(int initialCapacity, float expansionFactor,
- float contractionCriteria) throws MathIllegalArgumentException {
- this.expansionFactor = expansionFactor;
- setContractionCriteria(contractionCriteria);
- setInitialCapacity(initialCapacity);
- internalArray = new double[initialCapacity];
+ public ResizableDoubleArray(int initialCapacity,
+ float expansionFactor,
+ float contractionCriteria)
+ throws MathIllegalArgumentException {
+ this(initialCapacity,
+ expansionFactor,
+ contractionCriteria,
+ ExpansionMode.MULTIPLICATIVE,
+ null);
}
/**
@@ -299,7 +307,7 @@ public class ResizableDoubleArray implements DoubleArray, Serializable {
}
/**
- * Create a ResizableArray with the specified properties.
+ * Creates an instance with the specified properties.
*
* Throws MathIllegalArgumentException if the following conditions are
* not met:
@@ -315,7 +323,7 @@ public class ResizableDoubleArray implements DoubleArray, Serializable {
* @param contractionCriteria Contraction criteria.
* @param expansionMode Expansion mode.
* @param data Initial contents of the array.
- * @throws MathIllegalArgumentException if parameters are not valid.
+ * @throws MathIllegalArgumentException if the parameters are not valid.
*/
public ResizableDoubleArray(int initialCapacity,
float expansionFactor,
@@ -323,11 +331,16 @@ public class ResizableDoubleArray implements DoubleArray, Serializable {
ExpansionMode expansionMode,
double ... data)
throws MathIllegalArgumentException {
+ if (initialCapacity <= 0) {
+ throw new NotStrictlyPositiveException(LocalizedFormats.INITIAL_CAPACITY_NOT_POSITIVE,
+ initialCapacity);
+ }
+ checkContractExpand(contractionCriteria, expansionFactor);
- setExpansionFactor(expansionFactor);
- setContractionCriteria(contractionCriteria);
- setExpansionMode(expansionMode);
- setInitialCapacity(initialCapacity);
+ this.expansionFactor = expansionFactor;
+ this.contractionCriteria = contractionCriteria;
+ this.expansionMode = expansionMode;
+ this.initialCapacity = initialCapacity;
internalArray = new double[initialCapacity];
numElements = 0;
startIndex = 0;