Example usage for java.math BigInteger BigInteger

List of usage examples for java.math BigInteger BigInteger

Introduction

In this page you can find the example usage for java.math BigInteger BigInteger.

Prototype

private BigInteger(long val) 

Source Link

Document

Constructs a BigInteger with the specified value, which may not be zero.

Usage

From source file:io.github.benas.jpopulator.randomizers.validation.MaxValueRandomizer.java

/**
 * Generate a random value for the given type.
 *
 * @param type the type for which a random value will be generated
 * @param maxValue the maximum threshold for the generated value
 * @return a random value (lower than maxValue) for the given type or null if the type is not supported
 *//*from   ww  w  . j a v  a  2s  .  c  o m*/
public static Object getRandomValue(final Class type, final long maxValue) {

    if (type.equals(Byte.TYPE) || type.equals(Byte.class)) {
        return (byte) randomDataGenerator.nextLong(Byte.MIN_VALUE, maxValue);
    }
    if (type.equals(Short.TYPE) || type.equals(Short.class)) {
        return (short) randomDataGenerator.nextLong(Short.MIN_VALUE, maxValue);
    }
    if (type.equals(Integer.TYPE) || type.equals(Integer.class)) {
        return (int) randomDataGenerator.nextLong(Integer.MIN_VALUE, maxValue);
    }
    if (type.equals(Long.TYPE) || type.equals(Long.class)) {
        return randomDataGenerator.nextLong(Long.MIN_VALUE, maxValue);
    }
    if (type.equals(BigInteger.class)) {
        return new BigInteger(String.valueOf(randomDataGenerator.nextLong(Long.MIN_VALUE, maxValue)));
    }
    if (type.equals(BigDecimal.class)) {
        return new BigDecimal(randomDataGenerator.nextLong(Long.MIN_VALUE, maxValue));
    }
    return null;
}

From source file:io.github.benas.jpopulator.randomizers.validation.MinValueRandomizer.java

/**
 * Generate a random value for the given type.
 *
 * @param type the type for which a random value will be generated
 * @param minValue the minimum threshold for the generated value
 * @return a random value (greater than maxValue) for the given type or null if the type is not supported
 *//* w  w w. j  ava 2s. c om*/
public static Object getRandomValue(final Class type, final long minValue) {

    if (type.equals(Byte.TYPE) || type.equals(Byte.class)) {
        return (byte) randomDataGenerator.nextLong(minValue, Byte.MAX_VALUE);
    }
    if (type.equals(Short.TYPE) || type.equals(Short.class)) {
        return (short) randomDataGenerator.nextLong(minValue, Short.MAX_VALUE);
    }
    if (type.equals(Integer.TYPE) || type.equals(Integer.class)) {
        return (int) randomDataGenerator.nextLong(minValue, Integer.MAX_VALUE);
    }
    if (type.equals(Long.TYPE) || type.equals(Long.class)) {
        return randomDataGenerator.nextLong(minValue, Long.MAX_VALUE);
    }
    if (type.equals(BigInteger.class)) {
        return new BigInteger(String.valueOf(randomDataGenerator.nextLong(minValue, Long.MAX_VALUE)));
    }
    if (type.equals(BigDecimal.class)) {
        return new BigDecimal(randomDataGenerator.nextLong(minValue, Long.MAX_VALUE));
    }
    return null;
}

From source file:me.yanaga.querydsl.args.core.single.SingleBigIntegerArgumentTest.java

@BeforeMethod
public void setUp() {
    Person person = new Person();
    person.setOneBigInteger(new BigInteger("123"));
    person.setAnotherBigInteger(new BigInteger("321"));
    person.setOneCustomNumberType(CustomNumberType.of(new BigDecimal(222)));
    entityManager.persist(person);/*  w  ww. j a  v  a  2  s  . c o  m*/
}

From source file:RandomUtil.java

public static BigInteger randomBitFlip(BigInteger n) {
    if (n.equals(BigInteger.ONE))
        return BigInteger.ONE;
    if (n.equals(BigInteger.ZERO))
        return BigInteger.ZERO;

    BigInteger toRet = BigInteger.ONE;
    while (toRet.equals(BigInteger.ONE) || toRet.equals(BigInteger.ZERO)) {
        byte[] bytes = n.toByteArray();
        getInstance().random.nextBytes(bytes);
        // could allow up to maxExclusive by converting and checking value
        // but this is faster, even if it doesn't give us full range
        bytes[0] = 0;//  w ww .  j a v a2s.  co m
        toRet = new BigInteger(bytes);
    }
    return toRet;
}

From source file:com.ok2c.lightmtp.util.InetAddressRange.java

public boolean contains(final InetAddress ip) {
    BigInteger bigint2 = new BigInteger(ip.getAddress());
    if (this.shiftBy > 0) {
        bigint2 = bigint2.shiftRight(this.shiftBy);
    }// w  w  w .  ja  v a2s  .  c om
    return this.bigint.compareTo(bigint2) == 0;
}

From source file:com.streamreduce.util.CAGenerator.java

public static X509Certificate generateCACert(KeyPair keyPair) throws Exception {
    Date startDate = new Date(System.currentTimeMillis()); // time from which certificate is valid
    Calendar expiry = Calendar.getInstance();
    expiry.add(Calendar.DAY_OF_YEAR, 1000 * 365);
    Date expiryDate = expiry.getTime(); // time after which certificate is not valid
    BigInteger serialNumber = new BigInteger(Long.toString(System.currentTimeMillis())); // serial number for certificate

    X509V3CertificateGenerator certGen = new X509V3CertificateGenerator();
    X500Principal dnName = new X500Principal("CN=Nodeable Client");

    certGen.setSerialNumber(serialNumber);
    certGen.setIssuerDN(dnName);// w w  w  .  ja va  2 s .c om
    certGen.setNotBefore(startDate);
    certGen.setNotAfter(expiryDate);
    certGen.setSubjectDN(dnName);
    certGen.setPublicKey(keyPair.getPublic());
    certGen.setSignatureAlgorithm("MD5withRSA");

    certGen.addExtension(X509Extensions.AuthorityKeyIdentifier, false,
            new AuthorityKeyIdentifierStructure(keyPair.getPublic()));
    certGen.addExtension(X509Extensions.SubjectKeyIdentifier, false,
            new SubjectKeyIdentifierStructure(keyPair.getPublic()));

    return certGen.generate(keyPair.getPrivate()); // note: private key of CA
}

From source file:localSPs.CubbyAPI.java

public double getStorageSize() throws IOException {
    BigInteger num = new BigInteger("1024");
    String ans = "";
    BigInteger totalMemory = new BigInteger("5368709120");
    BigInteger used;// www . j  av  a 2  s .  co m
    File dir = new File(ROOT_PATH);
    long totalUsed = 0; // total bytes
    List<File> files = (List<File>) FileUtils.listFiles(dir, TrueFileFilter.INSTANCE, TrueFileFilter.INSTANCE);
    for (File file : files) {
        totalUsed += file.length();
    }
    used = new BigInteger(String.valueOf(totalUsed));
    ans = totalMemory.subtract(used).divide(num).divide(num).toString();//MB
    return (Double.parseDouble(ans));
}

From source file:dk.alexandra.fresco.suite.bgw.configuration.BgwConfiguration.java

public static BgwConfiguration fromCmdLine(SCEConfiguration sceConf, CommandLine cmd) throws ParseException {
    // Validate BGW specific arguments.
    Properties p = cmd.getOptionProperties("D");
    if (!p.containsKey("bgw.threshold")) {
        throw new ParseException("BGW requires setting -Dbgw.threshold=[int]");
    }// w  ww  .j av  a2  s  .  c  o  m

    try {
        final int threshold = Integer.parseInt(p.getProperty("bgw.threshold"));
        if (threshold < 1)
            throw new ParseException("bgw.threshold must be > 0");
        if (threshold > sceConf.getParties().size() / 2)
            throw new ParseException("bgw.threshold must be < n/2");

        final BigInteger modulus = new BigInteger(p.getProperty("bgw.modulus", "618970019642690137449562111"));
        if (!modulus.isProbablePrime(40)) {
            throw new ParseException("BGW Modulus must be a prime number");
        }

        return new BgwConfiguration() {

            @Override
            public int getThreshold() {
                return threshold;
            }

            @Override
            public BigInteger getModulus() {
                return modulus;
            }

        };
    } catch (NumberFormatException e) {
        throw new ParseException("Invalid bgw.threshold value: '" + p.getProperty("bgw.threshold") + "'");
    }
}

From source file:localSPs.BearDataShareAPI.java

public double getStorageSize() throws IOException {
    BigInteger num = new BigInteger("1024");
    String ans = "";
    BigInteger totalMemory = new BigInteger("21474836480");
    BigInteger used;/*  w  w  w .  j  a  v  a2 s. c  om*/
    File dir = new File(ROOT_PATH);
    long totalUsed = 0; // total bytes
    List<File> files = (List<File>) FileUtils.listFiles(dir, TrueFileFilter.INSTANCE, TrueFileFilter.INSTANCE);
    for (File file : files) {
        totalUsed += file.length();
    }
    used = new BigInteger(String.valueOf(totalUsed));
    ans = totalMemory.subtract(used).divide(num).divide(num).toString();//MB
    return (Double.parseDouble(ans));
}

From source file:me.j360.trace.server.brave.BraveConfiguration.java

/** This gets the lanIP without trying to lookup its name. */
// http://stackoverflow.com/questions/8765578/get-local-ip-address-without-connecting-to-the-internet
@Bean/* w  w w.  j ava 2s .  c  o m*/
@Scope
Endpoint local(@Value("${server.port:9411}") int port) {
    int ipv4;
    try {
        ipv4 = Collections.list(NetworkInterface.getNetworkInterfaces()).stream()
                .flatMap(i -> Collections.list(i.getInetAddresses()).stream())
                .filter(ip -> ip instanceof Inet4Address && ip.isSiteLocalAddress())
                .map(InetAddress::getAddress).map(bytes -> new BigInteger(bytes).intValue()).findAny().get();
    } catch (Exception ignored) {
        ipv4 = 127 << 24 | 1;
    }
    return Endpoint.create("zipkin-server", ipv4, port);
}