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. * */ - 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: - * + *
* Throws IllegalArgumentException if the following conditions are * not met: *

+ *
  • {@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: *

    - * @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;