Java Code Examples for org.apache.commons.math.exception.util.LocalizedFormats#LENGTH

The following examples show how to use org.apache.commons.math.exception.util.LocalizedFormats#LENGTH . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AbstractUnivariateStatistic.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * This method is used by <code>evaluate(double[], int, int)</code> methods
 * to verify that the input parameters designate a subarray of positive length.
 * <p>
 * <ul>
 * <li>returns <code>true</code> iff the parameters designate a subarray of
 * non-negative length</li>
 * <li>throws <code>IllegalArgumentException</code> if the array is null or
 * or the indices are invalid</li>
 * <li>returns <code>false</li> if the array is non-null, but
 * <code>length</code> is 0 unless <code>allowEmpty</code> is <code>true</code>
 * </ul></p>
 *
 * @param values the input array
 * @param begin index of the first array element to include
 * @param length the number of elements to include
 * @param allowEmpty if <code>true</code> then zero length arrays are allowed
 * @return true if the parameters are valid
 * @throws IllegalArgumentException if the indices are invalid or the array is null
 * @since 3.0
 */
protected boolean test(final double[] values, final int begin, final int length, final boolean allowEmpty){

    if (values == null) {
        throw new NullArgumentException(LocalizedFormats.INPUT_ARRAY);
    }

    if (begin < 0) {
        throw new NotPositiveException(LocalizedFormats.START_POSITION, begin);
    }

    if (length < 0) {
        throw new NotPositiveException(LocalizedFormats.LENGTH, length);
    }

    if (begin + length > values.length) {
        throw new NumberIsTooLargeException(LocalizedFormats.SUBARRAY_ENDS_AFTER_ARRAY_END,
                                            begin + length, values.length, true);
    }

    if (length == 0 && !allowEmpty) {
        return false;
    }

    return true;

}
 
Example 2
Source File: RandomDataImpl.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 * <p>
 * <strong>Algorithm Description:</strong> hex strings are generated using a
 * 2-step process.
 * <ol>
 * <li>
 * len/2+1 binary bytes are generated using the underlying Random</li>
 * <li>
 * Each binary byte is translated into 2 hex digits</li>
 * </ol>
 * </p>
 *
 * @param len
 *            the desired string length.
 * @return the random string.
 * @throws NotStrictlyPositiveException if {@code len <= 0}.
 */
public String nextHexString(int len) {
    if (len <= 0) {
        throw new NotStrictlyPositiveException(LocalizedFormats.LENGTH, len);
    }

    // Get a random number generator
    RandomGenerator ran = getRan();

    // Initialize output buffer
    StringBuilder outBuffer = new StringBuilder();

    // Get int(len/2)+1 random bytes
    byte[] randomBytes = new byte[(len / 2) + 1];
    ran.nextBytes(randomBytes);

    // Convert each byte to 2 hex digits
    for (int i = 0; i < randomBytes.length; i++) {
        Integer c = Integer.valueOf(randomBytes[i]);

        /*
         * Add 128 to byte value to make interval 0-255 before doing hex
         * conversion. This guarantees <= 2 hex digits from toHexString()
         * toHexString would otherwise add 2^32 to negative arguments.
         */
        String hex = Integer.toHexString(c.intValue() + 128);

        // Make sure we add 2 hex digits for each byte
        if (hex.length() == 1) {
            hex = "0" + hex;
        }
        outBuffer.append(hex);
    }
    return outBuffer.toString().substring(0, len);
}
 
Example 3
Source File: AbstractUnivariateStatistic.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * This method is used by <code>evaluate(double[], int, int)</code> methods
 * to verify that the input parameters designate a subarray of positive length.
 * <p>
 * <ul>
 * <li>returns <code>true</code> iff the parameters designate a subarray of
 * positive length</li>
 * <li>throws <code>IllegalArgumentException</code> if the array is null or
 * or the indices are invalid</li>
 * <li>returns <code>false</li> if the array is non-null, but
 * <code>length</code> is 0.
 * </ul></p>
 *
 * @param values the input array
 * @param begin index of the first array element to include
 * @param length the number of elements to include
 * @return true if the parameters are valid and designate a subarray of positive length
 * @throws IllegalArgumentException if the indices are invalid or the array is null
 */
protected boolean test(
    final double[] values,
    final int begin,
    final int length) {

    if (values == null) {
        throw new NullArgumentException(LocalizedFormats.INPUT_ARRAY);
    }

    if (begin < 0) {
        throw new NotPositiveException(LocalizedFormats.START_POSITION, begin);
    }

    if (length < 0) {
        throw new NotPositiveException(LocalizedFormats.LENGTH, length);
    }

    if (begin + length > values.length) {
        throw MathRuntimeException.createIllegalArgumentException(
              LocalizedFormats.SUBARRAY_ENDS_AFTER_ARRAY_END);
    }

    if (length == 0) {
        return false;
    }

    return true;

}
 
Example 4
Source File: RandomDataImpl.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 * <p>
 * <strong>Algorithm Description:</strong> hex strings are generated using a
 * 2-step process.
 * <ol>
 * <li>
 * len/2+1 binary bytes are generated using the underlying Random</li>
 * <li>
 * Each binary byte is translated into 2 hex digits</li>
 * </ol>
 * </p>
 *
 * @param len
 *            the desired string length.
 * @return the random string.
 * @throws NotStrictlyPositiveException if {@code len <= 0}.
 */
public String nextHexString(int len) {
    if (len <= 0) {
        throw new NotStrictlyPositiveException(LocalizedFormats.LENGTH, len);
    }

    // Get a random number generator
    RandomGenerator ran = getRan();

    // Initialize output buffer
    StringBuffer outBuffer = new StringBuffer();

    // Get int(len/2)+1 random bytes
    byte[] randomBytes = new byte[(len / 2) + 1];
    ran.nextBytes(randomBytes);

    // Convert each byte to 2 hex digits
    for (int i = 0; i < randomBytes.length; i++) {
        Integer c = Integer.valueOf(randomBytes[i]);

        /*
         * Add 128 to byte value to make interval 0-255 before doing hex
         * conversion. This guarantees <= 2 hex digits from toHexString()
         * toHexString would otherwise add 2^32 to negative arguments.
         */
        String hex = Integer.toHexString(c.intValue() + 128);

        // Make sure we add 2 hex digits for each byte
        if (hex.length() == 1) {
            hex = "0" + hex;
        }
        outBuffer.append(hex);
    }
    return outBuffer.toString().substring(0, len);
}
 
Example 5
Source File: AbstractUnivariateStatistic.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * This method is used by <code>evaluate(double[], int, int)</code> methods
 * to verify that the input parameters designate a subarray of positive length.
 * <p>
 * <ul>
 * <li>returns <code>true</code> iff the parameters designate a subarray of
 * non-negative length</li>
 * <li>throws <code>IllegalArgumentException</code> if the array is null or
 * or the indices are invalid</li>
 * <li>returns <code>false</li> if the array is non-null, but
 * <code>length</code> is 0 unless <code>allowEmpty</code> is <code>true</code>
 * </ul></p>
 *
 * @param values the input array
 * @param begin index of the first array element to include
 * @param length the number of elements to include
 * @param allowEmpty if <code>true</code> then zero length arrays are allowed
 * @return true if the parameters are valid
 * @throws IllegalArgumentException if the indices are invalid or the array is null
 * @since 3.0
 */
protected boolean test(final double[] values, final int begin, final int length, final boolean allowEmpty){

    if (values == null) {
        throw new NullArgumentException(LocalizedFormats.INPUT_ARRAY);
    }

    if (begin < 0) {
        throw new NotPositiveException(LocalizedFormats.START_POSITION, begin);
    }

    if (length < 0) {
        throw new NotPositiveException(LocalizedFormats.LENGTH, length);
    }

    if (begin + length > values.length) {
        throw new NumberIsTooLargeException(LocalizedFormats.SUBARRAY_ENDS_AFTER_ARRAY_END,
                                            begin + length, values.length, true);
    }

    if (length == 0 && !allowEmpty) {
        return false;
    }

    return true;

}
 
Example 6
Source File: RandomDataImpl.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 * <p>
 * <strong>Algorithm Description:</strong> hex strings are generated using a
 * 2-step process.
 * <ol>
 * <li>
 * len/2+1 binary bytes are generated using the underlying Random</li>
 * <li>
 * Each binary byte is translated into 2 hex digits</li>
 * </ol>
 * </p>
 *
 * @param len
 *            the desired string length.
 * @return the random string.
 * @throws NotStrictlyPositiveException if {@code len <= 0}.
 */
public String nextHexString(int len) {
    if (len <= 0) {
        throw new NotStrictlyPositiveException(LocalizedFormats.LENGTH, len);
    }

    // Get a random number generator
    RandomGenerator ran = getRan();

    // Initialize output buffer
    StringBuilder outBuffer = new StringBuilder();

    // Get int(len/2)+1 random bytes
    byte[] randomBytes = new byte[(len / 2) + 1];
    ran.nextBytes(randomBytes);

    // Convert each byte to 2 hex digits
    for (int i = 0; i < randomBytes.length; i++) {
        Integer c = Integer.valueOf(randomBytes[i]);

        /*
         * Add 128 to byte value to make interval 0-255 before doing hex
         * conversion. This guarantees <= 2 hex digits from toHexString()
         * toHexString would otherwise add 2^32 to negative arguments.
         */
        String hex = Integer.toHexString(c.intValue() + 128);

        // Make sure we add 2 hex digits for each byte
        if (hex.length() == 1) {
            hex = "0" + hex;
        }
        outBuffer.append(hex);
    }
    return outBuffer.toString().substring(0, len);
}
 
Example 7
Source File: AbstractUnivariateStatistic.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * This method is used by <code>evaluate(double[], int, int)</code> methods
 * to verify that the input parameters designate a subarray of positive length.
 * <p>
 * <ul>
 * <li>returns <code>true</code> iff the parameters designate a subarray of
 * non-negative length</li>
 * <li>throws <code>IllegalArgumentException</code> if the array is null or
 * or the indices are invalid</li>
 * <li>returns <code>false</li> if the array is non-null, but
 * <code>length</code> is 0 unless <code>allowEmpty</code> is <code>true</code>
 * </ul></p>
 *
 * @param values the input array
 * @param begin index of the first array element to include
 * @param length the number of elements to include
 * @param allowEmpty if <code>true</code> then zero length arrays are allowed
 * @return true if the parameters are valid
 * @throws IllegalArgumentException if the indices are invalid or the array is null
 * @since 3.0
 */
protected boolean test(final double[] values, final int begin, final int length, final boolean allowEmpty){

    if (values == null) {
        throw new NullArgumentException(LocalizedFormats.INPUT_ARRAY);
    }

    if (begin < 0) {
        throw new NotPositiveException(LocalizedFormats.START_POSITION, begin);
    }

    if (length < 0) {
        throw new NotPositiveException(LocalizedFormats.LENGTH, length);
    }

    if (begin + length > values.length) {
        throw new NumberIsTooLargeException(LocalizedFormats.SUBARRAY_ENDS_AFTER_ARRAY_END,
                                            begin + length, values.length, true);
    }

    if (length == 0 && !allowEmpty) {
        return false;
    }

    return true;

}
 
Example 8
Source File: RandomDataImpl.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 * <p>
 * <strong>Algorithm Description:</strong> hex strings are generated using a
 * 2-step process.
 * <ol>
 * <li>
 * len/2+1 binary bytes are generated using the underlying Random</li>
 * <li>
 * Each binary byte is translated into 2 hex digits</li>
 * </ol>
 * </p>
 *
 * @param len
 *            the desired string length.
 * @return the random string.
 * @throws NotStrictlyPositiveException if {@code len <= 0}.
 */
public String nextHexString(int len) {
    if (len <= 0) {
        throw new NotStrictlyPositiveException(LocalizedFormats.LENGTH, len);
    }

    // Get a random number generator
    RandomGenerator ran = getRan();

    // Initialize output buffer
    StringBuilder outBuffer = new StringBuilder();

    // Get int(len/2)+1 random bytes
    byte[] randomBytes = new byte[(len / 2) + 1];
    ran.nextBytes(randomBytes);

    // Convert each byte to 2 hex digits
    for (int i = 0; i < randomBytes.length; i++) {
        Integer c = Integer.valueOf(randomBytes[i]);

        /*
         * Add 128 to byte value to make interval 0-255 before doing hex
         * conversion. This guarantees <= 2 hex digits from toHexString()
         * toHexString would otherwise add 2^32 to negative arguments.
         */
        String hex = Integer.toHexString(c.intValue() + 128);

        // Make sure we add 2 hex digits for each byte
        if (hex.length() == 1) {
            hex = "0" + hex;
        }
        outBuffer.append(hex);
    }
    return outBuffer.toString().substring(0, len);
}
 
Example 9
Source File: RandomDataImpl.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 * <p>
 * <strong>Algorithm Description:</strong> hex strings are generated in
 * 40-byte segments using a 3-step process.
 * <ol>
 * <li>
 * 20 random bytes are generated using the underlying
 * <code>SecureRandom</code>.</li>
 * <li>
 * SHA-1 hash is applied to yield a 20-byte binary digest.</li>
 * <li>
 * Each byte of the binary digest is converted to 2 hex digits.</li>
 * </ol>
 * </p>
 *
 * @param len
 *            the length of the generated string
 * @return the random string
 * @throws NotStrictlyPositiveException if {@code len <= 0}.
 */
public String nextSecureHexString(int len) {
    if (len <= 0) {
        throw new NotStrictlyPositiveException(LocalizedFormats.LENGTH, len);
    }

    // Get SecureRandom and setup Digest provider
    SecureRandom secRan = getSecRan();
    MessageDigest alg = null;
    try {
        alg = MessageDigest.getInstance("SHA-1");
    } catch (NoSuchAlgorithmException ex) {
        // this should never happen
        throw new MathInternalError(ex);
    }
    alg.reset();

    // Compute number of iterations required (40 bytes each)
    int numIter = (len / 40) + 1;

    StringBuilder outBuffer = new StringBuilder();
    for (int iter = 1; iter < numIter + 1; iter++) {
        byte[] randomBytes = new byte[40];
        secRan.nextBytes(randomBytes);
        alg.update(randomBytes);

        // Compute hash -- will create 20-byte binary hash
        byte hash[] = alg.digest();

        // Loop over the hash, converting each byte to 2 hex digits
        for (int i = 0; i < hash.length; i++) {
            Integer c = Integer.valueOf(hash[i]);

            /*
             * Add 128 to byte value to make interval 0-255 This guarantees
             * <= 2 hex digits from toHexString() toHexString would
             * otherwise add 2^32 to negative arguments
             */
            String hex = Integer.toHexString(c.intValue() + 128);

            // Keep strings uniform length -- guarantees 40 bytes
            if (hex.length() == 1) {
                hex = "0" + hex;
            }
            outBuffer.append(hex);
        }
    }
    return outBuffer.toString().substring(0, len);
}
 
Example 10
Source File: RandomDataImpl.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 * <p>
 * <strong>Algorithm Description:</strong> hex strings are generated in
 * 40-byte segments using a 3-step process.
 * <ol>
 * <li>
 * 20 random bytes are generated using the underlying
 * <code>SecureRandom</code>.</li>
 * <li>
 * SHA-1 hash is applied to yield a 20-byte binary digest.</li>
 * <li>
 * Each byte of the binary digest is converted to 2 hex digits.</li>
 * </ol>
 * </p>
 *
 * @param len
 *            the length of the generated string
 * @return the random string
 * @throws NotStrictlyPositiveException if {@code len <= 0}.
 */
public String nextSecureHexString(int len) {
    if (len <= 0) {
        throw new NotStrictlyPositiveException(LocalizedFormats.LENGTH, len);
    }

    // Get SecureRandom and setup Digest provider
    SecureRandom secRan = getSecRan();
    MessageDigest alg = null;
    try {
        alg = MessageDigest.getInstance("SHA-1");
    } catch (NoSuchAlgorithmException ex) {
        // this should never happen
        throw MathRuntimeException.createInternalError(ex);
    }
    alg.reset();

    // Compute number of iterations required (40 bytes each)
    int numIter = (len / 40) + 1;

    StringBuffer outBuffer = new StringBuffer();
    for (int iter = 1; iter < numIter + 1; iter++) {
        byte[] randomBytes = new byte[40];
        secRan.nextBytes(randomBytes);
        alg.update(randomBytes);

        // Compute hash -- will create 20-byte binary hash
        byte hash[] = alg.digest();

        // Loop over the hash, converting each byte to 2 hex digits
        for (int i = 0; i < hash.length; i++) {
            Integer c = Integer.valueOf(hash[i]);

            /*
             * Add 128 to byte value to make interval 0-255 This guarantees
             * <= 2 hex digits from toHexString() toHexString would
             * otherwise add 2^32 to negative arguments
             */
            String hex = Integer.toHexString(c.intValue() + 128);

            // Keep strings uniform length -- guarantees 40 bytes
            if (hex.length() == 1) {
                hex = "0" + hex;
            }
            outBuffer.append(hex);
        }
    }
    return outBuffer.toString().substring(0, len);
}
 
Example 11
Source File: RandomDataImpl.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 * <p>
 * <strong>Algorithm Description:</strong> hex strings are generated in
 * 40-byte segments using a 3-step process.
 * <ol>
 * <li>
 * 20 random bytes are generated using the underlying
 * <code>SecureRandom</code>.</li>
 * <li>
 * SHA-1 hash is applied to yield a 20-byte binary digest.</li>
 * <li>
 * Each byte of the binary digest is converted to 2 hex digits.</li>
 * </ol>
 * </p>
 *
 * @param len
 *            the length of the generated string
 * @return the random string
 * @throws NotStrictlyPositiveException if {@code len <= 0}.
 */
public String nextSecureHexString(int len) {
    if (len <= 0) {
        throw new NotStrictlyPositiveException(LocalizedFormats.LENGTH, len);
    }

    // Get SecureRandom and setup Digest provider
    SecureRandom secRan = getSecRan();
    MessageDigest alg = null;
    try {
        alg = MessageDigest.getInstance("SHA-1");
    } catch (NoSuchAlgorithmException ex) {
        // this should never happen
        throw new MathInternalError(ex);
    }
    alg.reset();

    // Compute number of iterations required (40 bytes each)
    int numIter = (len / 40) + 1;

    StringBuilder outBuffer = new StringBuilder();
    for (int iter = 1; iter < numIter + 1; iter++) {
        byte[] randomBytes = new byte[40];
        secRan.nextBytes(randomBytes);
        alg.update(randomBytes);

        // Compute hash -- will create 20-byte binary hash
        byte hash[] = alg.digest();

        // Loop over the hash, converting each byte to 2 hex digits
        for (int i = 0; i < hash.length; i++) {
            Integer c = Integer.valueOf(hash[i]);

            /*
             * Add 128 to byte value to make interval 0-255 This guarantees
             * <= 2 hex digits from toHexString() toHexString would
             * otherwise add 2^32 to negative arguments
             */
            String hex = Integer.toHexString(c.intValue() + 128);

            // Keep strings uniform length -- guarantees 40 bytes
            if (hex.length() == 1) {
                hex = "0" + hex;
            }
            outBuffer.append(hex);
        }
    }
    return outBuffer.toString().substring(0, len);
}
 
Example 12
Source File: RandomDataImpl.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 * <p>
 * <strong>Algorithm Description:</strong> hex strings are generated in
 * 40-byte segments using a 3-step process.
 * <ol>
 * <li>
 * 20 random bytes are generated using the underlying
 * <code>SecureRandom</code>.</li>
 * <li>
 * SHA-1 hash is applied to yield a 20-byte binary digest.</li>
 * <li>
 * Each byte of the binary digest is converted to 2 hex digits.</li>
 * </ol>
 * </p>
 *
 * @param len
 *            the length of the generated string
 * @return the random string
 * @throws NotStrictlyPositiveException if {@code len <= 0}.
 */
public String nextSecureHexString(int len) {
    if (len <= 0) {
        throw new NotStrictlyPositiveException(LocalizedFormats.LENGTH, len);
    }

    // Get SecureRandom and setup Digest provider
    SecureRandom secRan = getSecRan();
    MessageDigest alg = null;
    try {
        alg = MessageDigest.getInstance("SHA-1");
    } catch (NoSuchAlgorithmException ex) {
        // this should never happen
        throw new MathInternalError(ex);
    }
    alg.reset();

    // Compute number of iterations required (40 bytes each)
    int numIter = (len / 40) + 1;

    StringBuilder outBuffer = new StringBuilder();
    for (int iter = 1; iter < numIter + 1; iter++) {
        byte[] randomBytes = new byte[40];
        secRan.nextBytes(randomBytes);
        alg.update(randomBytes);

        // Compute hash -- will create 20-byte binary hash
        byte hash[] = alg.digest();

        // Loop over the hash, converting each byte to 2 hex digits
        for (int i = 0; i < hash.length; i++) {
            Integer c = Integer.valueOf(hash[i]);

            /*
             * Add 128 to byte value to make interval 0-255 This guarantees
             * <= 2 hex digits from toHexString() toHexString would
             * otherwise add 2^32 to negative arguments
             */
            String hex = Integer.toHexString(c.intValue() + 128);

            // Keep strings uniform length -- guarantees 40 bytes
            if (hex.length() == 1) {
                hex = "0" + hex;
            }
            outBuffer.append(hex);
        }
    }
    return outBuffer.toString().substring(0, len);
}