List of usage examples for java.math BigInteger compareTo
public int compareTo(BigInteger val)
From source file:Main.java
public static String BytesToBase58(byte[] value) { //Add 1 for each 00 byte. //From lowest base58 fill with division remainders. String returnValue = ""; boolean justStarted = true; BigInteger bigValue = new BigInteger(value); //TODO: Check that it works as it should. BigInteger base58 = new BigInteger("58"); BigInteger zero = new BigInteger("0"); BigInteger[] divisionResult;/*from w w w . j a v a 2 s .c om*/ while (bigValue.compareTo(zero) == 1) { //Means greater than. divisionResult = bigValue.divideAndRemainder(base58); bigValue = divisionResult[0]; returnValue = base58Array.toCharArray()[divisionResult[1].intValue()] + returnValue; } for (int i = 0; i < value.length; i++) { if (value[i] == 0 && justStarted) { returnValue = "1" + returnValue; } else { break; } justStarted = false; } return returnValue; }
From source file:cc.redberry.core.number.NumberUtils.java
private static boolean isSqrtXXX(BigInteger n, BigInteger root) { final BigInteger lowerBound = root.pow(2); final BigInteger upperBound = root.add(BigInteger.ONE).pow(2); return lowerBound.compareTo(n) <= 0 && n.compareTo(upperBound) < 0; }
From source file:edu.hku.sdb.udf.util.UDFHandler.java
/** * (1) value less than halfN means that a > b, return true * (2) value greater or equal to halfN means that a < b, return false * @param value//from w w w. ja v a 2 s . c om * @param halfN * @return */ public static boolean greatThan(BigInteger value, BigInteger halfN) { if (value.equals(BigInteger.ZERO)) return false; int result = value.compareTo(halfN); if (result < 0) return true; else return false; }
From source file:com.offbynull.peernetic.common.identification.Id.java
/** * Adds two IDs together. The limit of the IDs must match. * @param lhs right-hand side/*from www.j av a2s. c o m*/ * @param rhs right-hand side * @return {@code lhs + rhs}, wrapped if it exceeds the limit * @throws NullPointerException if any argument is {@code null} * @throws IllegalArgumentException if the limit from {@code lhs} doesn't match the limit from {@code rhs} */ public static Id add(Id lhs, Id rhs) { Validate.notNull(lhs); Validate.notNull(rhs); Validate.isTrue(lhs.limit.equals(rhs.limit)); BigInteger added = lhs.data.add(rhs.data); if (added.compareTo(lhs.limit) > 0) { added = added.subtract(lhs.limit).subtract(BigInteger.ONE); } Id addedId = new Id(added, lhs.limit); return addedId; }
From source file:com.offbynull.peernetic.common.identification.Id.java
/** * Compare the position of two IDs, using a base reference point. Another way to think of this is that this method compares two IDs from * the view of a certain reference point. * <p/>//w w w . j a v a2s.com * <b>Example 1:</b><br/> * The ID limit is 16<br/> * {@code lhs = 15}<br/> * {@code rhs = 2}<br/> * {@code base = 10}<br/> * In this case, {@code lhs > rhs}. From {@code base}'s view, {@code lhs} is only 5 nodes away, while {@code rhs} is 8 nodes away. * <p/> * <b>Example 2:</b><br/> * The ID limit is 16<br/> * {@code lhs = 9}<br/> * {@code rhs = 2}<br/> * {@code base = 10}<br/> * In this case, {@code rhs < lhs}. From {@code base}'s view, {@code lhs} is 15 nodes away, while {@code rhs} is 8 only nodes away. * @param base reference point * @param lhs left-hand side * @param rhs right-hand side * @return -1, 0 or 1 as {@lhs} is less than, equal to, or greater than {@code rhs}. * @throws NullPointerException if any argument is {@code null} * @throws IllegalArgumentException if the limit from {@code lhs} or {@code rhs} doesn't match the limit from {@code base} */ public static int comparePosition(Id base, Id lhs, Id rhs) { Validate.notNull(base); Validate.notNull(lhs); Validate.notNull(rhs); Validate.isTrue(base.limit.equals(lhs.limit) && base.limit.equals(rhs.limit)); Id lhsOffsetId = subtract(lhs, base); Id rhsOffsetId = subtract(rhs, base); BigInteger rhsOffsetIdNum = rhsOffsetId.getValueAsBigInteger(); BigInteger lhsOffsetIdNum = lhsOffsetId.getValueAsBigInteger(); return lhsOffsetIdNum.compareTo(rhsOffsetIdNum); }
From source file:org.apache.orc.tools.json.JsonSchemaFinder.java
static HiveType pickType(JsonElement json) { if (json.isJsonPrimitive()) { JsonPrimitive prim = (JsonPrimitive) json; if (prim.isBoolean()) { return new BooleanType(); } else if (prim.isNumber()) { Matcher matcher = DECIMAL_PATTERN.matcher(prim.getAsString()); if (matcher.matches()) { int intDigits = matcher.group("int").length(); String fraction = matcher.group("fraction"); int scale = fraction == null ? 0 : fraction.length(); if (scale == 0) { if (intDigits < 19) { long value = prim.getAsLong(); if (value >= -128 && value < 128) { return new NumericType(HiveType.Kind.BYTE, intDigits, scale); } else if (value >= -32768 && value < 32768) { return new NumericType(HiveType.Kind.SHORT, intDigits, scale); } else if (value >= -2147483648 && value < 2147483648L) { return new NumericType(HiveType.Kind.INT, intDigits, scale); } else { return new NumericType(HiveType.Kind.LONG, intDigits, scale); }//from w ww . j av a2s . c om } else if (intDigits == 19) { // at 19 digits, it may fit inside a long, but we need to check BigInteger val = prim.getAsBigInteger(); if (val.compareTo(MIN_LONG) >= 0 && val.compareTo(MAX_LONG) <= 0) { return new NumericType(HiveType.Kind.LONG, intDigits, scale); } } } if (intDigits + scale <= MAX_DECIMAL_DIGITS) { return new NumericType(HiveType.Kind.DECIMAL, intDigits, scale); } } double value = prim.getAsDouble(); if (value >= Float.MIN_VALUE && value <= Float.MAX_VALUE) { return new NumericType(HiveType.Kind.FLOAT, 0, 0); } else { return new NumericType(HiveType.Kind.DOUBLE, 0, 0); } } else { String str = prim.getAsString(); if (TIMESTAMP_PATTERN.matcher(str).matches()) { return new StringType(HiveType.Kind.TIMESTAMP); } else if (HEX_PATTERN.matcher(str).matches()) { return new StringType(HiveType.Kind.BINARY); } else { return new StringType(HiveType.Kind.STRING); } } } else if (json.isJsonNull()) { return new NullType(); } else if (json.isJsonArray()) { ListType result = new ListType(); result.elementType = new NullType(); for (JsonElement child : ((JsonArray) json)) { HiveType sub = pickType(child); if (result.elementType.subsumes(sub)) { result.elementType.merge(sub); } else if (sub.subsumes(result.elementType)) { sub.merge(result.elementType); result.elementType = sub; } else { result.elementType = new UnionType(result.elementType, sub); } } return result; } else { JsonObject obj = (JsonObject) json; StructType result = new StructType(); for (Map.Entry<String, JsonElement> field : obj.entrySet()) { String fieldName = field.getKey(); HiveType type = pickType(field.getValue()); result.fields.put(fieldName, type); } return result; } }
From source file:org.openestate.io.wis_it.WisItUtils.java
public static String printNonNegativeInteger(BigInteger value) { if (value == null || value.compareTo(BigInteger.ZERO) == -1) throw new IllegalArgumentException("Can't print integer value!"); else//from ww w . ja v a 2s . c o m return DatatypeConverter.printInteger(value); }
From source file:Main.java
public static String encodeBase58(byte[] input) { if (input == null) { return null; }/* w w w.j av a 2 s.c o m*/ StringBuilder str = new StringBuilder((input.length * 350) / 256 + 1); BigInteger bn = new BigInteger(1, input); long rem; while (true) { BigInteger[] divideAndRemainder = bn.divideAndRemainder(BASE58_CHUNK_MOD); bn = divideAndRemainder[0]; rem = divideAndRemainder[1].longValue(); if (bn.compareTo(BigInteger.ZERO) == 0) { break; } for (int i = 0; i < BASE58_CHUNK_DIGITS; i++) { str.append(BASE58[(int) (rem % 58)]); rem /= 58; } } while (rem != 0) { str.append(BASE58[(int) (rem % 58)]); rem /= 58; } str.reverse(); int nLeadingZeros = 0; while (nLeadingZeros < input.length && input[nLeadingZeros] == 0) { str.insert(0, BASE58[0]); nLeadingZeros++; } return str.toString(); }
From source file:org.openmrs.module.casereport.DocumentUtil.java
/** * Converts the specified uuid to its decimal representation * // w ww.j a va2 s . c o m * @param uuid the uuid to convert * @return a string representation of the decimal number */ public static String convertToDecimal(UUID uuid) { ByteBuffer bb = ByteBuffer.wrap(new byte[16]); bb.putLong(uuid.getMostSignificantBits()); bb.putLong(uuid.getLeastSignificantBits()); BigInteger bi = new BigInteger(bb.array()); //Get the unsigned representation for -ve numbers if (bi.compareTo(BigInteger.ZERO) < 0) { bi = DECIMAL_REP_COUNT.add(bi); } return bi.toString(); }
From source file:de.jfachwert.post.Postfach.java
/** * Validiert das uebergebene Postfach auf moegliche Fehler. * * @param nummer Postfach-Nummer (muss positiv sein) * @param ort Ort mit PLZ/*from w ww . j a v a2s .co m*/ */ public static void validate(BigInteger nummer, Ort ort) { if (nummer.compareTo(BigInteger.ONE) < 0) { throw new InvalidValueException(nummer, "number"); } validate(ort); }