use of java.math.BigDecimal in project swagger-core by swagger-api.
the class PropertyDeserializerTest method deserializeParameterWithMinimumMaximumValues.
@Test
public void deserializeParameterWithMinimumMaximumValues() throws Exception {
String json = "{\n" + " \"in\": \"query\",\n" + " \"type\": \"integer\",\n" + " \"format\": \"int32\",\n" + " \"minimum\": 32,\n" + " \"maximum\": 100\n" + "}";
Property property = Json.mapper().readValue(json, Property.class);
assertTrue(property instanceof IntegerProperty);
IntegerProperty ip = (IntegerProperty) property;
assertEquals(ip.getMinimum(), new BigDecimal("32"));
assertEquals(ip.getMaximum(), new BigDecimal("100"));
}
use of java.math.BigDecimal in project CoreNLP by stanfordnlp.
the class NumberNormalizer method wordToNumber.
/**
* Fairly generous utility function to convert a string representing
* a number (hopefully) to a Number.
* Assumes that something else has somehow determined that the string
* makes ONE suitable number.
* The value of the number is determined by:
* 0. Breaking up the string into pieces using whitespace
* (stuff like "and", "-", "," is turned into whitespace);
* 1. Determining the numeric value of the pieces;
* 2. Finding the numeric value of each piece;
* 3. Combining the pieces together to form the overall value:
* a. Find the largest component and its value (X),
* b. Let B = overall value of pieces to the left (recursive),
* c. Let C = overall value of pieces to the right recursive),
* d. The overall value = B*X + C.
*
* @param str The String to convert
* @return numeric value of string
*/
public static Number wordToNumber(String str) {
if (str.trim().equals("")) {
return null;
}
boolean neg = false;
String originalString = str;
// Trims and lowercases stuff
str = str.trim();
str = str.toLowerCase();
if (str.startsWith("-")) {
neg = true;
}
// eliminate hyphens, commas, and the word "and"
str = str.replaceAll("\\band\\b", " ");
str = str.replaceAll("-", " ");
// Maybe something like 4,233,000 ??
str = str.replaceAll("(\\d),(\\d)", "$1$2");
str = str.replaceAll(",", " ");
// str = str.replaceAll("(\\d)(\\w)","$1 $2");
// Trims again (do we need this?)
str = str.trim();
//if string starts with "a ", as in "a hundred", replace it with "one"
if (str.startsWith("a ")) {
str = str.replace("a", "one");
}
// cut off some trailing s
if (str.endsWith("sands")) {
// thousands
str = str.substring(0, str.length() - 1);
} else if (str.endsWith("ions")) {
// millions, billions, etc
str = str.substring(0, str.length() - 1);
}
// now count words
String[] fields = wsPattern.split(str);
Number[] numFields = new Number[fields.length];
int numWords = fields.length;
// get numeric value of each word piece
for (int curIndex = 0; curIndex < numWords; curIndex++) {
String curPart = fields[curIndex];
Matcher m = alphaPattern.matcher(curPart);
if (m.find()) {
// Some part of the word has alpha characters
Number curNum;
if (word2NumMap.containsKey(curPart)) {
curNum = word2NumMap.get(curPart);
} else if (ordWord2NumMap.containsKey(curPart)) {
if (curIndex == numWords - 1) {
curNum = ordWord2NumMap.get(curPart);
} else {
throw new NumberFormatException("Error in wordToNumber function.");
}
} else if (curIndex > 0 && (curPart.endsWith("ths") || curPart.endsWith("rds"))) {
// Fractions?
curNum = ordWord2NumMap.get(curPart.substring(0, curPart.length() - 1));
if (curNum != null) {
curNum = 1 / curNum.doubleValue();
} else {
throw new NumberFormatException("Bad number put into wordToNumber. Word is: \"" + curPart + "\", originally part of \"" + originalString + "\", piece # " + curIndex);
}
} else if (Character.isDigit(curPart.charAt(0))) {
if (curPart.endsWith("th") || curPart.endsWith("rd") || curPart.endsWith("nd") || curPart.endsWith("st")) {
curPart = curPart.substring(0, curPart.length() - 2);
}
if (digitsPattern.matcher(curPart).matches()) {
curNum = Long.parseLong(curPart);
} else {
throw new NumberFormatException("Bad number put into wordToNumber. Word is: \"" + curPart + "\", originally part of \"" + originalString + "\", piece # " + curIndex);
}
} else {
throw new NumberFormatException("Bad number put into wordToNumber. Word is: \"" + curPart + "\", originally part of \"" + originalString + "\", piece # " + curIndex);
}
numFields[curIndex] = curNum;
} else {
// Word is all numeric
if (digitsPattern.matcher(curPart).matches()) {
numFields[curIndex] = Long.parseLong(curPart);
} else if (numPattern.matcher(curPart).matches()) {
numFields[curIndex] = new BigDecimal(curPart);
} else {
// Hmm, strange number
throw new NumberFormatException("Bad number put into wordToNumber. Word is: \"" + curPart + "\", originally part of \"" + originalString + "\", piece # " + curIndex);
}
}
}
Number n = wordToNumberRecurse(numFields);
return (neg) ? -n.doubleValue() : n;
}
use of java.math.BigDecimal in project honeycomb by altamiracorp.
the class FieldParser method extractDecimal.
private static ByteBuffer extractDecimal(String val, int precision, int right_scale) {
int left_scale = precision - 2;
BigDecimal x = new BigDecimal(val);
boolean is_negative = x.compareTo(BigDecimal.ZERO) == -1;
x = x.abs();
BigDecimal left = x.setScale(0, RoundingMode.DOWN);
BigDecimal right = x.subtract(left).movePointRight(right_scale);
int right_bytes_len = bytesFromDigits(right_scale);
int left_bytes_len = bytesFromDigits(left_scale);
byte[] left_bytes = left.toBigInteger().toByteArray();
byte[] right_bytes = right.toBigInteger().toByteArray();
// Bit twiddling is fun
byte[] buff = new byte[left_bytes_len + right_bytes_len];
System.arraycopy(left_bytes, 0, buff, left_bytes_len - left_bytes.length, left_bytes.length);
System.arraycopy(right_bytes, 0, buff, right_bytes_len - right_bytes.length + left_bytes_len, right_bytes.length);
// Flip first bit, 0x80
buff[0] ^= -128;
if (is_negative) {
// Flip all bits
for (int i = 0; i < buff.length; i++) {
// 0xff
buff[i] ^= -1;
}
}
return ByteBuffer.wrap(buff);
}
use of java.math.BigDecimal in project dbeaver by serge-rider.
the class SearchDataQuery method findRows.
private DBCStatistics findRows(@NotNull DBCSession session, @NotNull DBSDataContainer dataContainer, @NotNull TestDataReceiver dataReceiver) throws DBCException {
DBSEntity entity;
if (dataContainer instanceof DBSEntity) {
entity = (DBSEntity) dataContainer;
} else {
log.warn("Data container " + dataContainer + " isn't entity");
return null;
}
try {
List<DBDAttributeConstraint> constraints = new ArrayList<>();
for (DBSEntityAttribute attribute : CommonUtils.safeCollection(entity.getAttributes(session.getProgressMonitor()))) {
if (params.fastSearch) {
if (!DBUtils.isIndexedAttribute(session.getProgressMonitor(), attribute)) {
continue;
}
}
if (DBUtils.isPseudoAttribute(attribute) || DBUtils.isHiddenObject(attribute)) {
continue;
}
DBCLogicalOperator[] supportedOperators = DBUtils.getAttributeOperators(attribute);
DBCLogicalOperator operator;
Object value;
switch(attribute.getDataKind()) {
case BOOLEAN:
continue;
case NUMERIC:
if (!params.searchNumbers) {
continue;
}
if (!ArrayUtils.contains(supportedOperators, DBCLogicalOperator.EQUALS)) {
continue;
}
operator = DBCLogicalOperator.EQUALS;
try {
value = new Integer(params.searchString);
} catch (NumberFormatException e) {
try {
value = new Long(params.searchString);
} catch (NumberFormatException e1) {
try {
value = new Double(params.searchString);
} catch (NumberFormatException e2) {
try {
value = new BigDecimal(params.searchString);
} catch (Exception e3) {
// Not a number
continue;
}
}
}
}
break;
case CONTENT:
case BINARY:
if (!params.searchLOBs) {
continue;
}
case STRING:
if (attribute.getMaxLength() > 0 && attribute.getMaxLength() < params.searchString.length()) {
continue;
}
if (ArrayUtils.contains(supportedOperators, DBCLogicalOperator.LIKE)) {
operator = DBCLogicalOperator.LIKE;
value = "%" + params.searchString + "%";
} else if (ArrayUtils.contains(supportedOperators, DBCLogicalOperator.EQUALS)) {
operator = DBCLogicalOperator.EQUALS;
value = params.searchString;
} else {
continue;
}
break;
default:
{
// On success search by exact match
if (!ArrayUtils.contains(supportedOperators, DBCLogicalOperator.EQUALS)) {
continue;
}
String typeName = attribute.getTypeName();
if (typeName.equals(DBConstants.TYPE_NAME_UUID) || typeName.equals(DBConstants.TYPE_NAME_UUID2)) {
try {
UUID uuid = UUID.fromString(params.searchString);
operator = DBCLogicalOperator.EQUALS;
value = "'" + uuid.toString() + "'";
} catch (Exception e) {
// No a UUID
continue;
}
} else {
continue;
}
}
}
DBDAttributeConstraint constraint = new DBDAttributeConstraint(attribute, constraints.size());
constraint.setOperator(operator);
constraint.setValue(value);
constraint.setVisible(true);
constraints.add(constraint);
}
if (constraints.isEmpty()) {
return null;
}
dataReceiver.filter = new DBDDataFilter(constraints);
dataReceiver.filter.setAnyConstraint(true);
DBCExecutionSource searchSource = new AbstractExecutionSource(dataContainer, session.getExecutionContext(), this);
return dataContainer.readData(searchSource, session, dataReceiver, dataReceiver.filter, -1, -1, 0);
} catch (DBException e) {
throw new DBCException("Error finding rows", e);
}
}
use of java.math.BigDecimal in project jodd by oblac.
the class TypeConverterManagerBean method registerDefaults.
/**
* Registers default set of converters.
*/
@SuppressWarnings({ "UnnecessaryFullyQualifiedName" })
public void registerDefaults() {
register(String.class, new StringConverter());
register(String[].class, new StringArrayConverter(this));
IntegerConverter integerConverter = new IntegerConverter();
register(Integer.class, integerConverter);
register(int.class, integerConverter);
register(MutableInteger.class, new MutableIntegerConverter(this));
ShortConverter shortConverter = new ShortConverter();
register(Short.class, shortConverter);
register(short.class, shortConverter);
register(MutableShort.class, new MutableShortConverter(this));
LongConverter longConverter = new LongConverter();
register(Long.class, longConverter);
register(long.class, longConverter);
register(MutableLong.class, new MutableLongConverter(this));
ByteConverter byteConverter = new ByteConverter();
register(Byte.class, byteConverter);
register(byte.class, byteConverter);
register(MutableByte.class, new MutableByteConverter(this));
FloatConverter floatConverter = new FloatConverter();
register(Float.class, floatConverter);
register(float.class, floatConverter);
register(MutableFloat.class, new MutableFloatConverter(this));
DoubleConverter doubleConverter = new DoubleConverter();
register(Double.class, doubleConverter);
register(double.class, doubleConverter);
register(MutableDouble.class, new MutableDoubleConverter(this));
BooleanConverter booleanConverter = new BooleanConverter();
register(Boolean.class, booleanConverter);
register(boolean.class, booleanConverter);
CharacterConverter characterConverter = new CharacterConverter();
register(Character.class, characterConverter);
register(char.class, characterConverter);
register(byte[].class, new ByteArrayConverter(this));
register(short[].class, new ShortArrayConverter(this));
register(int[].class, new IntegerArrayConverter(this));
register(long[].class, new LongArrayConverter(this));
register(float[].class, new FloatArrayConverter(this));
register(double[].class, new DoubleArrayConverter(this));
register(boolean[].class, new BooleanArrayConverter(this));
register(char[].class, new CharacterArrayConverter(this));
// we don't really need these, but converters will be cached and not created every time
register(Integer[].class, new ArrayConverter<Integer>(this, Integer.class) {
@Override
protected Integer[] createArray(int length) {
return new Integer[length];
}
});
register(Long[].class, new ArrayConverter<Long>(this, Long.class) {
@Override
protected Long[] createArray(int length) {
return new Long[length];
}
});
register(Byte[].class, new ArrayConverter<Byte>(this, Byte.class) {
@Override
protected Byte[] createArray(int length) {
return new Byte[length];
}
});
register(Short[].class, new ArrayConverter<Short>(this, Short.class) {
@Override
protected Short[] createArray(int length) {
return new Short[length];
}
});
register(Float[].class, new ArrayConverter<Float>(this, Float.class) {
@Override
protected Float[] createArray(int length) {
return new Float[length];
}
});
register(Double[].class, new ArrayConverter<Double>(this, Double.class) {
@Override
protected Double[] createArray(int length) {
return new Double[length];
}
});
register(Boolean[].class, new ArrayConverter<Boolean>(this, Boolean.class) {
@Override
protected Boolean[] createArray(int length) {
return new Boolean[length];
}
});
register(Character[].class, new ArrayConverter<Character>(this, Character.class) {
@Override
protected Character[] createArray(int length) {
return new Character[length];
}
});
register(MutableInteger[].class, new ArrayConverter<>(this, MutableInteger.class));
register(MutableLong[].class, new ArrayConverter<>(this, MutableLong.class));
register(MutableByte[].class, new ArrayConverter<>(this, MutableByte.class));
register(MutableShort[].class, new ArrayConverter<>(this, MutableShort.class));
register(MutableFloat[].class, new ArrayConverter<>(this, MutableFloat.class));
register(MutableDouble[].class, new ArrayConverter<>(this, MutableDouble.class));
register(BigDecimal.class, new BigDecimalConverter());
register(BigInteger.class, new BigIntegerConverter());
register(BigDecimal[].class, new ArrayConverter<>(this, BigDecimal.class));
register(BigInteger[].class, new ArrayConverter<>(this, BigInteger.class));
register(java.util.Date.class, new DateConverter());
register(java.sql.Date.class, new SqlDateConverter());
register(Time.class, new SqlTimeConverter());
register(Timestamp.class, new SqlTimestampConverter());
register(Calendar.class, new CalendarConverter());
register(GregorianCalendar.class, new CalendarConverter());
register(JDateTime.class, new JDateTimeConverter());
register(File.class, new FileConverter());
register(Class.class, new ClassConverter());
register(Class[].class, new ClassArrayConverter(this));
register(URI.class, new URIConverter());
register(URL.class, new URLConverter());
register(Locale.class, new LocaleConverter());
register(TimeZone.class, new TimeZoneConverter());
}
Aggregations