Return-Path: X-Original-To: apmail-commons-commits-archive@minotaur.apache.org Delivered-To: apmail-commons-commits-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id D0C13DBAE for ; Tue, 13 Nov 2012 16:53:38 +0000 (UTC) Received: (qmail 84900 invoked by uid 500); 13 Nov 2012 16:53:38 -0000 Delivered-To: apmail-commons-commits-archive@commons.apache.org Received: (qmail 84659 invoked by uid 500); 13 Nov 2012 16:53:37 -0000 Mailing-List: contact commits-help@commons.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@commons.apache.org Delivered-To: mailing list commits@commons.apache.org Received: (qmail 84628 invoked by uid 99); 13 Nov 2012 16:53:36 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 13 Nov 2012 16:53:36 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 13 Nov 2012 16:53:34 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id 585122388980 for ; Tue, 13 Nov 2012 16:53:14 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1408830 - /commons/proper/math/trunk/src/main/java/org/apache/commons/math3/util/ResizableDoubleArray.java Date: Tue, 13 Nov 2012 16:53:14 -0000 To: commits@commons.apache.org From: erans@apache.org X-Mailer: svnmailer-1.0.8-patched Message-Id: <20121113165314.585122388980@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: erans Date: Tue Nov 13 16:53:13 2012 New Revision: 1408830 URL: http://svn.apache.org/viewvc?rev=1408830&view=rev Log: Use constructor chaining (in place of duplicate code). Fixed Javadoc typos (expansion factor and contraction criterion default values). Javadoc formatting. Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/util/ResizableDoubleArray.java Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/util/ResizableDoubleArray.java URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/util/ResizableDoubleArray.java?rev=1408830&r1=1408829&r2=1408830&view=diff ============================================================================== --- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/util/ResizableDoubleArray.java (original) +++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/util/ResizableDoubleArray.java Tue Nov 13 16:53:13 2012 @@ -23,6 +23,7 @@ import org.apache.commons.math3.exceptio 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 implem /** 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 implem } /** - * 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 implem } /** - * 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 implem * @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 implem 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;