Search in sources :

Example 16 with BigDecimal

use of java.math.BigDecimal in project groovy by apache.

the class DefaultGroovyMethods method step.

/**
     * Iterates from this number up to the given number using a step increment.
     * Each intermediate number is passed to the given closure.  Example:
     * <pre>0.step( 10, 2 ) {
     *   println it
     * }</pre>
     * Prints even numbers 0 through 8.
     *
     * @param self       a Number to start with
     * @param to         a Number to go up to, exclusive
     * @param stepNumber a Number representing the step increment
     * @param closure    the closure to call
     * @since 1.0
     */
public static void step(Number self, Number to, Number stepNumber, Closure closure) {
    if (self instanceof BigDecimal || to instanceof BigDecimal || stepNumber instanceof BigDecimal) {
        // Same as "0.0".
        final BigDecimal zero = BigDecimal.valueOf(0, 1);
        BigDecimal self1 = (self instanceof BigDecimal) ? (BigDecimal) self : new BigDecimal(self.toString());
        BigDecimal to1 = (to instanceof BigDecimal) ? (BigDecimal) to : new BigDecimal(to.toString());
        BigDecimal stepNumber1 = (stepNumber instanceof BigDecimal) ? (BigDecimal) stepNumber : new BigDecimal(stepNumber.toString());
        if (stepNumber1.compareTo(zero) > 0 && to1.compareTo(self1) > 0) {
            for (BigDecimal i = self1; i.compareTo(to1) < 0; i = i.add(stepNumber1)) {
                closure.call(i);
            }
        } else if (stepNumber1.compareTo(zero) < 0 && to1.compareTo(self1) < 0) {
            for (BigDecimal i = self1; i.compareTo(to1) > 0; i = i.add(stepNumber1)) {
                closure.call(i);
            }
        } else if (self1.compareTo(to1) != 0)
            throw new GroovyRuntimeException("Infinite loop in " + self1 + ".step(" + to1 + ", " + stepNumber1 + ")");
    } else if (self instanceof BigInteger || to instanceof BigInteger || stepNumber instanceof BigInteger) {
        final BigInteger zero = BigInteger.valueOf(0);
        BigInteger self1 = (self instanceof BigInteger) ? (BigInteger) self : new BigInteger(self.toString());
        BigInteger to1 = (to instanceof BigInteger) ? (BigInteger) to : new BigInteger(to.toString());
        BigInteger stepNumber1 = (stepNumber instanceof BigInteger) ? (BigInteger) stepNumber : new BigInteger(stepNumber.toString());
        if (stepNumber1.compareTo(zero) > 0 && to1.compareTo(self1) > 0) {
            for (BigInteger i = self1; i.compareTo(to1) < 0; i = i.add(stepNumber1)) {
                closure.call(i);
            }
        } else if (stepNumber1.compareTo(zero) < 0 && to1.compareTo(self1) < 0) {
            for (BigInteger i = self1; i.compareTo(to1) > 0; i = i.add(stepNumber1)) {
                closure.call(i);
            }
        } else if (self1.compareTo(to1) != 0)
            throw new GroovyRuntimeException("Infinite loop in " + self1 + ".step(" + to1 + ", " + stepNumber1 + ")");
    } else {
        int self1 = self.intValue();
        int to1 = to.intValue();
        int stepNumber1 = stepNumber.intValue();
        if (stepNumber1 > 0 && to1 > self1) {
            for (int i = self1; i < to1; i += stepNumber1) {
                closure.call(i);
            }
        } else if (stepNumber1 < 0 && to1 < self1) {
            for (int i = self1; i > to1; i += stepNumber1) {
                closure.call(i);
            }
        } else if (self1 != to1)
            throw new GroovyRuntimeException("Infinite loop in " + self1 + ".step(" + to1 + ", " + stepNumber1 + ")");
    }
}
Also used : BigInteger(java.math.BigInteger) BigDecimal(java.math.BigDecimal)

Example 17 with BigDecimal

use of java.math.BigDecimal in project groovy by apache.

the class ObjectRangeTest method testIteratorAndStep1.

public void testIteratorAndStep1() {
    Range r = createRange(5, 11);
    int i = 4;
    for (Iterator it = r.iterator(); it.hasNext(); ) {
        i++;
        assertEquals("equals to " + i, new Integer(i), (Integer) (it.next()));
    }
    assertEquals(11, i);
    i = 4;
    for (Iterator it = r.step(1).iterator(); it.hasNext(); ) {
        i++;
        assertEquals("equals to " + i, new Integer(i), (Integer) (it.next()));
    }
    assertEquals(11, i);
    r = createRange(new BigDecimal("5.0"), new BigDecimal("11.0"));
    BigDecimal one = new BigDecimal("1.0");
    BigDecimal val = new BigDecimal("5.0");
    for (Iterator it = r.iterator(); it.hasNext(); ) {
        assertEquals("equals to " + val, val, (BigDecimal) (it.next()));
        val = val.add(one);
    }
    assertEquals(11, i);
    val = new BigDecimal("5.0");
    for (Iterator it = r.step(1).iterator(); it.hasNext(); ) {
        assertEquals("equals to " + val, val, (BigDecimal) (it.next()));
        val = val.add(one);
    }
    assertEquals(11, i);
    r = createRange(new Character('a'), new Character('z'));
    char valChar = 'a';
    for (Iterator it = r.iterator(); it.hasNext(); ) {
        assertEquals("equals to " + valChar, valChar, ((Character) it.next()).charValue());
        if (it.hasNext()) {
            valChar = (char) (((int) valChar) + 1);
        }
    }
    assertEquals('z', valChar);
    valChar = 'a';
    for (Iterator it = r.step(1).iterator(); it.hasNext(); ) {
        assertEquals("equals to " + valChar, valChar, ((Character) it.next()).charValue());
        if (it.hasNext()) {
            valChar = (char) (((int) valChar) + 1);
        }
    }
    assertEquals('z', valChar);
}
Also used : BigInteger(java.math.BigInteger) Iterator(java.util.Iterator) BigDecimal(java.math.BigDecimal)

Example 18 with BigDecimal

use of java.math.BigDecimal in project groovy by apache.

the class ObjectRangeTest method testSize.

public void testSize() {
    Range r = createRange(0, 10);
    assertEquals("Size of " + r, 11, r.size());
    r = createRange(0, 1);
    assertEquals("Size of " + r, 2, r.size());
    r = createRange(1, 0);
    assertEquals("Size of " + r, 2, r.size());
    r = createRange(0, 0);
    assertEquals("Size of " + r, 1, r.size());
    r = createRange(new BigDecimal("2.1"), new BigDecimal("10.0"));
    assertEquals("Size of " + r, 8, r.size());
    r = createRange(new BigDecimal("10"), new BigDecimal("2.1"));
    assertEquals("Size of " + r, 8, r.size());
    r = createRange("a", "d");
    assertEquals("Size of " + r, 4, r.size());
    r = createRange("d", "a");
    assertEquals("Size of " + r, 4, r.size());
    r = createRange("aa1", "aa4");
    assertEquals("Size of " + r, 4, r.size());
    r = createRange("aa4", "aa1");
    assertEquals("Size of " + r, 4, r.size());
    r = createRange('7', ';');
    assertEquals(5, r.size());
    // '7', '8', '9', ':', ';'
    Range mixed = createRange('7', ';');
    assertEquals(5, mixed.size());
    mixed = createRange('7', 59.5);
    assertEquals(5, mixed.size());
    mixed = createRange('7', 59);
    assertEquals(5, mixed.size());
    mixed = createRange('7', new BigInteger("59"));
    assertEquals(5, mixed.size());
    mixed = createRange('7', new BigDecimal("59.5"));
    assertEquals(5, mixed.size());
    // integer overflow cases
    assertEquals(Integer.MAX_VALUE, new ObjectRange(0L, Integer.MAX_VALUE).size());
    assertEquals(Integer.MAX_VALUE, new ObjectRange(Long.MIN_VALUE, Long.MAX_VALUE).size());
    assertEquals(Integer.MAX_VALUE, new ObjectRange(new BigInteger("-10"), new BigInteger(Long.toString((long) Integer.MAX_VALUE) + 1L)).size());
}
Also used : BigInteger(java.math.BigInteger) BigDecimal(java.math.BigDecimal)

Example 19 with BigDecimal

use of java.math.BigDecimal in project groovy by apache.

the class ObjectRangeTest method testContainsWithLikeNumbers.

public void testContainsWithLikeNumbers() {
    Range r = new ObjectRange(new Integer(1), new Short((short) 3));
    assertTrue("contains 2", r.contains(new Integer(2)));
    r = new ObjectRange(new Float(1.0), new Double(3.0));
    assertTrue("contains 2.0d", r.contains(new Double(2.0)));
    assertTrue("contains 2.0g", r.contains(new BigDecimal(2.0)));
    r = new ObjectRange(new BigDecimal(1.0), new BigDecimal(3.0));
    assertTrue("contains 2.0d", r.contains(new Double(2.0)));
    assertTrue("contains 2.0f", r.contains(new Float(2.0)));
}
Also used : BigInteger(java.math.BigInteger) BigDecimal(java.math.BigDecimal)

Example 20 with BigDecimal

use of java.math.BigDecimal in project groovy by apache.

the class ObjectRangeTest method testSubList.

public void testSubList() {
    Range r = createRange(10, 20);
    assertEquals("from", 10, r.getFrom());
    assertEquals("to", 20, r.getTo());
    assertEquals("size", 11, r.size());
    List s = r.subList(2, 4);
    Range sr = (Range) s;
    assertEquals("from", 12, sr.getFrom());
    assertEquals("to", 13, sr.getTo());
    assertEquals("size", 2, sr.size());
    s = r.subList(0, 11);
    sr = (Range) s;
    assertEquals("from", 10, sr.getFrom());
    assertEquals("to", 20, sr.getTo());
    assertEquals("size", 11, sr.size());
    try {
        r.subList(-2, 4);
        fail();
    } catch (IndexOutOfBoundsException e) {
    // pass
    }
    try {
        r.subList(5, 12);
        fail();
    } catch (IndexOutOfBoundsException e) {
    // pass
    }
    r = createRange(new BigDecimal("0.5"), new BigDecimal("8.5"));
    assertEquals("size", 9, r.size());
    s = r.subList(2, 5);
    sr = (Range) s;
    assertEquals("from", new BigDecimal("2.5"), sr.getFrom());
    assertEquals("to", new BigDecimal("4.5"), sr.getTo());
    assertTrue("contains 4.5", sr.contains(new BigDecimal("4.5")));
    assertFalse("contains 5.5", sr.contains(new BigDecimal("5.5")));
    assertEquals("size", 3, sr.size());
}
Also used : List(java.util.List) BigDecimal(java.math.BigDecimal)

Aggregations

BigDecimal (java.math.BigDecimal)4643 Test (org.junit.Test)634 BigInteger (java.math.BigInteger)624 Test (org.testng.annotations.Test)573 LocalDate (org.joda.time.LocalDate)409 ArrayList (java.util.ArrayList)351 ResultSet (java.sql.ResultSet)222 Timestamp (java.sql.Timestamp)205 PreparedStatement (java.sql.PreparedStatement)201 SQLException (java.sql.SQLException)159 Invoice (org.killbill.billing.invoice.api.Invoice)148 Date (java.util.Date)146 UUID (java.util.UUID)144 MathContext (java.math.MathContext)140 DateTime (org.joda.time.DateTime)132 HashMap (java.util.HashMap)130 RoundingMode (java.math.RoundingMode)128 List (java.util.List)113 InvoiceItem (org.killbill.billing.invoice.api.InvoiceItem)104 Session (org.hibernate.Session)96