Search in sources :

Example 56 with BigInteger

use of java.math.BigInteger in project hazelcast by hazelcast.

the class MapMaxAggregationTest method testBigIntegerMaxWithExtractor.

@Test
public void testBigIntegerMaxWithExtractor() throws Exception {
    Value<BigInteger>[] values = buildValues(new ValueProvider<BigInteger>() {

        @Override
        public BigInteger provideRandom(Random random) {
            return BigInteger.valueOf(10000L + random(1000, 2000));
        }
    });
    BigInteger expectation = BigInteger.ZERO;
    for (int i = 0; i < values.length; i++) {
        Value<BigInteger> value = values[i];
        expectation = i == 0 ? value.value : expectation.max(value.value);
    }
    Aggregation<String, BigInteger, BigInteger> aggregation = Aggregations.bigIntegerMax();
    BigInteger result = testMaxWithExtractor(values, aggregation);
    assertEquals(expectation, result);
}
Also used : Random(java.util.Random) BigInteger(java.math.BigInteger) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 57 with BigInteger

use of java.math.BigInteger in project hazelcast by hazelcast.

the class SqlPredicateTest method testSql_withBigInteger.

@Test
public void testSql_withBigInteger() {
    SampleObjects.ObjectWithBigInteger value = new SampleObjects.ObjectWithBigInteger(new BigInteger("123"));
    assertSqlMatching("attribute > '" + new BigInteger("122") + "'", value);
    assertSqlMatching("attribute >= '" + new BigInteger("123") + "'", value);
    assertSqlMatching("attribute = '" + new BigInteger("123") + "'", value);
    assertSqlNotMatching("attribute = '" + new BigInteger("122") + "'", value);
    assertSqlMatching("attribute < '" + new BigInteger("124") + "'", value);
    assertSqlMatching("attribute <= '" + new BigInteger("123") + "'", value);
    assertSqlMatching("attribute = 123", value);
    assertSqlMatching("attribute = '123'", value);
    assertSqlMatching("attribute != 124", value);
    assertSqlMatching("attribute <> 124", value);
    assertSqlNotMatching("attribute = 124", value);
    assertSqlMatching("attribute between 122 and 124", value);
    assertSqlMatching("attribute in (122, 123, 124)", value);
}
Also used : BigInteger(java.math.BigInteger) QuickTest(com.hazelcast.test.annotation.QuickTest) DateHelperTest(com.hazelcast.query.impl.DateHelperTest) Test(org.junit.Test)

Example 58 with BigInteger

use of java.math.BigInteger in project hazelcast by hazelcast.

the class TypeConverterTest method testBigIntegerConvert_whenPassedBigIntegerValue_thenConvertToBigInteger.

@Test
public void testBigIntegerConvert_whenPassedBigIntegerValue_thenConvertToBigInteger() throws Exception {
    BigInteger value = BigInteger.ONE;
    Comparable expectedBigIntValue = BigInteger.valueOf(value.longValue());
    Comparable comparable = TypeConverters.BIG_INTEGER_CONVERTER.convert(value);
    assertThat(comparable, allOf(is(instanceOf(BigInteger.class)), is(equalTo(expectedBigIntValue))));
}
Also used : BigInteger(java.math.BigInteger) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 59 with BigInteger

use of java.math.BigInteger in project guava by hceylan.

the class DoubleUtils method bigToDouble.

static double bigToDouble(BigInteger x) {
    // This is an extremely fast implementation of BigInteger.doubleValue().  JDK patch pending.
    BigInteger absX = x.abs();
    int exponent = absX.bitLength() - 1;
    // exponent == floor(log2(abs(x)))
    if (exponent < Long.SIZE - 1) {
        return x.longValue();
    } else if (exponent > Double.MAX_EXPONENT) {
        return x.signum() * Double.POSITIVE_INFINITY;
    }
    /*
     * We need the top SIGNIFICAND_BITS + 1 bits, including the "implicit" one bit. To make
     * rounding easier, we pick out the top SIGNIFICAND_BITS + 2 bits, so we have one to help us
     * round up or down. twiceSignifFloor will contain the top SIGNIFICAND_BITS + 2 bits, and
     * signifFloor the top SIGNIFICAND_BITS + 1.
     * 
     * It helps to consider the real number signif = absX * 2^(SIGNIFICAND_BITS - exponent).
     */
    int shift = exponent - SIGNIFICAND_BITS - 1;
    long twiceSignifFloor = absX.shiftRight(shift).longValue();
    long signifFloor = twiceSignifFloor >> 1;
    // remove the implied bit
    signifFloor &= SIGNIFICAND_MASK;
    /*
     * We round up if either the fractional part of signif is strictly greater than 0.5 (which is
     * true if the 0.5 bit is set and any lower bit is set), or if the fractional part of signif is
     * >= 0.5 and signifFloor is odd (which is true if both the 0.5 bit and the 1 bit are set).
     */
    boolean increment = (twiceSignifFloor & 1) != 0 && ((signifFloor & 1) != 0 || absX.getLowestSetBit() < shift);
    long signifRounded = increment ? signifFloor + 1 : signifFloor;
    long bits = (long) ((exponent + EXPONENT_BIAS)) << SIGNIFICAND_BITS;
    bits += signifRounded;
    /*
     * If signifRounded == 2^53, we'd need to set all of the significand bits to zero and add 1 to
     * the exponent. This is exactly the behavior we get from just adding signifRounded to bits
     * directly.  If the exponent is MAX_DOUBLE_EXPONENT, we round up (correctly) to
     * Double.POSITIVE_INFINITY.
     */
    bits |= x.signum() & SIGN_MASK;
    return Double.longBitsToDouble(bits);
}
Also used : BigInteger(java.math.BigInteger)

Example 60 with BigInteger

use of java.math.BigInteger in project hibernate-orm by hibernate.

the class NamingHelper method hashedName.

/**
	 * Hash a constraint name using MD5. Convert the MD5 digest to base 35
	 * (full alphanumeric), guaranteeing
	 * that the length of the name will always be smaller than the 30
	 * character identifier restriction enforced by a few dialects.
	 *
	 * @param s The name to be hashed.
	 *
	 * @return String The hashed name.
	 */
public String hashedName(String s) {
    try {
        MessageDigest md = MessageDigest.getInstance("MD5");
        md.reset();
        md.update(s.getBytes());
        byte[] digest = md.digest();
        BigInteger bigInt = new BigInteger(1, digest);
        // character identifier restriction enforced by a few dialects.
        return bigInt.toString(35);
    } catch (NoSuchAlgorithmException e) {
        throw new HibernateException("Unable to generate a hashed name!", e);
    }
}
Also used : HibernateException(org.hibernate.HibernateException) BigInteger(java.math.BigInteger) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) MessageDigest(java.security.MessageDigest)

Aggregations

BigInteger (java.math.BigInteger)3500 BigDecimal (java.math.BigDecimal)635 Test (org.junit.Test)337 IOException (java.io.IOException)113 ArrayList (java.util.ArrayList)105 MessageDigest (java.security.MessageDigest)101 RoundingMode (java.math.RoundingMode)88 Date (java.util.Date)80 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)75 MathContext (java.math.MathContext)71 Random (java.util.Random)65 Test (org.junit.jupiter.api.Test)50 QuickTest (com.hazelcast.test.annotation.QuickTest)47 ParallelTest (com.hazelcast.test.annotation.ParallelTest)46 KeyFactory (java.security.KeyFactory)46 HashMap (java.util.HashMap)45 List (java.util.List)44 SecureRandom (java.security.SecureRandom)42 X509Certificate (java.security.cert.X509Certificate)42 EllipticCurve (java.security.spec.EllipticCurve)40