Java Code Examples for org.apache.commons.lang3.math.NumberUtils#createNumber()
The following examples show how to use
org.apache.commons.lang3.math.NumberUtils#createNumber() .
These examples are extracted from open source projects.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: incubator-pinot File: LiteralTransformFunction.java License: Apache License 2.0 | 6 votes |
public static FieldSpec.DataType inferLiteralDataType(LiteralTransformFunction transformFunction) { String literal = transformFunction.getLiteral(); try { Number literalNum = NumberUtils.createNumber(literal); if (literalNum instanceof Integer) { return FieldSpec.DataType.INT; } else if (literalNum instanceof Long) { return FieldSpec.DataType.LONG; } else if (literalNum instanceof Float) { return FieldSpec.DataType.FLOAT; } else if (literalNum instanceof Double) { return FieldSpec.DataType.DOUBLE; } } catch (Exception e) { } return FieldSpec.DataType.STRING; }
Example 2
Source Project: metrics-cloudwatch File: CloudWatchReporter.java License: Apache License 2.0 | 6 votes |
void reportGauge(Map.Entry<String, Gauge> gaugeEntry, String typeDimValue, List<MetricDatum> data) { Gauge gauge = gaugeEntry.getValue(); Object valueObj = gauge.getValue(); if (valueObj == null) { return; } String valueStr = valueObj.toString(); if (NumberUtils.isNumber(valueStr)) { final Number value = NumberUtils.createNumber(valueStr); DemuxedKey key = new DemuxedKey(appendGlobalDimensions(gaugeEntry.getKey())); Iterables.addAll(data, key.newDatums(typeDimName, typeDimValue, new Function<MetricDatum, MetricDatum>() { @Override public MetricDatum apply(MetricDatum datum) { return datum.withValue(value.doubleValue()); } })); } }
Example 3
Source Project: gyro File: NodeEvaluator.java License: Apache License 2.0 | 5 votes |
private static Object doArithmetic( Object left, Object right, DoubleBinaryOperator doubleOperator, LongBinaryOperator longOperator) { if (left == null || right == null) { throw new GyroException("Can't do arithmetic with a null!"); } Number leftNumber = NumberUtils.createNumber(left.toString()); if (leftNumber == null) { throw new GyroException(String.format( "Can't do arithmetic on @|bold %s|@, an instance of @|bold %s|@, because it's not a number!", left, left.getClass().getName())); } Number rightNumber = NumberUtils.createNumber(right.toString()); if (rightNumber == null) { throw new GyroException(String.format( "Can't do arithmetic on @|bold %s|@, an instance of @|bold %s|@, because it's not a number!", right, right.getClass().getName())); } if (leftNumber instanceof Float || leftNumber instanceof Double || rightNumber instanceof Float || rightNumber instanceof Double) { return doubleOperator.applyAsDouble(leftNumber.doubleValue(), rightNumber.doubleValue()); } else { return longOperator.applyAsLong(leftNumber.longValue(), rightNumber.longValue()); } }
Example 4
Source Project: incubator-pinot File: InbuiltFunctionEvaluator.java License: Apache License 2.0 | 5 votes |
public ConstantExecutionNode(String value) { if (NumberUtils.isCreatable(value)) { _value = NumberUtils.createNumber(value); _returnType = Number.class; } else { _value = value; _returnType = String.class; } }
Example 5
Source Project: molgenis File: OneClickImporterServiceImpl.java License: GNU Lesser General Public License v3.0 | 5 votes |
private Object getPartValue(String part) { if (isNullOrEmpty(part)) { return null; } if (part.equalsIgnoreCase("true") || part.equalsIgnoreCase("false")) { return parseBoolean(part); } if (isNumber(part)) { return NumberUtils.createNumber(part); } return part; }
Example 6
Source Project: o2oa File: IntegerDeserializer.java License: GNU Affero General Public License v3.0 | 4 votes |
@Override public Integer deserialize(JsonElement json, Type type, JsonDeserializationContext context) throws JsonParseException { Number num = NumberUtils.createNumber(StringUtils.trimToNull(json.getAsString())); return (num == null) ? null : num.intValue(); }
Example 7
Source Project: o2oa File: DoubleDeserializer.java License: GNU Affero General Public License v3.0 | 4 votes |
@Override public Double deserialize(JsonElement json, Type type, JsonDeserializationContext context) throws JsonParseException { Number num = NumberUtils.createNumber(StringUtils.trimToNull(json.getAsString())); return (num == null) ? null : num.doubleValue(); }
Example 8
Source Project: o2oa File: LongDeserializer.java License: GNU Affero General Public License v3.0 | 4 votes |
@Override public Long deserialize(JsonElement json, Type type, JsonDeserializationContext context) throws JsonParseException { Number num = NumberUtils.createNumber(StringUtils.trimToNull(json.getAsString())); return (num == null) ? null : num.longValue(); }
Example 9
Source Project: o2oa File: FloatDeserializer.java License: GNU Affero General Public License v3.0 | 4 votes |
@Override public Float deserialize(JsonElement json, Type type, JsonDeserializationContext context) throws JsonParseException { Number num = NumberUtils.createNumber(StringUtils.trimToNull(json.getAsString())); return (num == null) ? null : num.floatValue(); }
Example 10
Source Project: JQF File: MathTest.java License: BSD 2-Clause "Simplified" License | 4 votes |
@Fuzz public void fuzzCreateNumber(@From(ArbitraryLengthStringGenerator.class) String input) throws NumberFormatException { NumberUtils.createNumber(input); }
Example 11
Source Project: Prism File: DataUtil.java License: MIT License | 4 votes |
/** * Attempts to convert a JsonElement to an a known type. * * @param element JsonElement * @return Optional<Object> */ private static Optional<Object> jsonElementToObject(JsonElement element) { if (element.isJsonArray()) { List<Object> list = Lists.newArrayList(); JsonArray jsonArray = element.getAsJsonArray(); jsonArray.forEach(entry -> jsonElementToObject(entry).ifPresent(list::add)); return Optional.of(list); } else if (element.isJsonObject()) { JsonObject jsonObject = element.getAsJsonObject(); PrimitiveArray primitiveArray = PrimitiveArray.of(jsonObject); if (primitiveArray != null) { return Optional.of(primitiveArray.getArray()); } return Optional.of(dataViewFromJson(jsonObject)); } else if (element.isJsonPrimitive()) { JsonPrimitive jsonPrimitive = element.getAsJsonPrimitive(); if (jsonPrimitive.isBoolean()) { return Optional.of(jsonPrimitive.getAsBoolean()); } else if (jsonPrimitive.isNumber()) { Number number = NumberUtils.createNumber(jsonPrimitive.getAsString()); if (number instanceof Byte) { return Optional.of(number.byteValue()); } else if (number instanceof Double) { return Optional.of(number.doubleValue()); } else if (number instanceof Float) { return Optional.of(number.floatValue()); } else if (number instanceof Integer) { return Optional.of(number.intValue()); } else if (number instanceof Long) { return Optional.of(number.longValue()); } else if (number instanceof Short) { return Optional.of(number.shortValue()); } } else if (jsonPrimitive.isString()) { return Optional.of(jsonPrimitive.getAsString()); } } return Optional.empty(); }
Example 12
Source Project: gvnix File: QuerydslUtils.java License: GNU General Public License v3.0 | 4 votes |
/** * Return an expression for {@code entityPath.fieldName} (for Numerics) with * the {@code operator} or "equal" by default. * <p/> * Expr: {@code entityPath.fieldName eq searchObj} * * @param entityPath * @param fieldName * @param searchObj * @param operator * @param fieldType * @return */ @SuppressWarnings({ "rawtypes", "unchecked" }) public static <T> BooleanExpression createNumericExpression( PathBuilder<T> entityPath, String fieldName, Object searchObj, String operator, Class<?> fieldType) { NumberPath numberExpression = null; if (BigDecimal.class.isAssignableFrom(fieldType)) { numberExpression = entityPath.getNumber(fieldName, (Class<BigDecimal>) fieldType); } else if (BigInteger.class.isAssignableFrom(fieldType)) { numberExpression = entityPath.getNumber(fieldName, (Class<BigInteger>) fieldType); } else if (Byte.class.isAssignableFrom(fieldType)) { numberExpression = entityPath.getNumber(fieldName, (Class<Byte>) fieldType); } else if (Double.class.isAssignableFrom(fieldType) || double.class == fieldType) { numberExpression = entityPath.getNumber(fieldName, (Class<Double>) fieldType); } else if (Float.class.isAssignableFrom(fieldType) || float.class == fieldType) { numberExpression = entityPath.getNumber(fieldName, (Class<Float>) fieldType); } else if (Integer.class.isAssignableFrom(fieldType) || int.class == fieldType) { numberExpression = entityPath.getNumber(fieldName, (Class<Integer>) fieldType); } else if (Long.class.isAssignableFrom(fieldType) || long.class == fieldType) { numberExpression = entityPath.getNumber(fieldName, (Class<Long>) fieldType); } else if (Short.class.isAssignableFrom(fieldType) || short.class == fieldType) { numberExpression = entityPath.getNumber(fieldName, (Class<Short>) fieldType); } if (numberExpression != null) { Number value = NumberUtils.createNumber((String) searchObj); if (StringUtils.equalsIgnoreCase(operator, OPERATOR_GOE)) { return numberExpression.goe(value); } else if (StringUtils.equalsIgnoreCase(operator, "gt")) { return numberExpression.gt(value); } else if (StringUtils.equalsIgnoreCase(operator, "like")) { return numberExpression.like((String) searchObj); } else if (StringUtils.equalsIgnoreCase(operator, OPERATOR_LOE)) { return numberExpression.loe(value); } else if (StringUtils.equalsIgnoreCase(operator, "lt")) { return numberExpression.lt(value); } } return entityPath.get(fieldName).eq(searchObj); }
Example 13
Source Project: gvnix File: QuerydslUtilsBeanImpl.java License: GNU General Public License v3.0 | 4 votes |
/** * {@inheritDoc} */ @Override @SuppressWarnings({ "rawtypes", "unchecked" }) public <T> BooleanExpression createNumericExpression( PathBuilder<T> entityPath, String fieldName, Object searchObj, String operator, Class<?> fieldType) { NumberPath numberExpression = null; if (BigDecimal.class.isAssignableFrom(fieldType)) { numberExpression = entityPath.getNumber(fieldName, (Class<BigDecimal>) fieldType); } else if (BigInteger.class.isAssignableFrom(fieldType)) { numberExpression = entityPath.getNumber(fieldName, (Class<BigInteger>) fieldType); } else if (Byte.class.isAssignableFrom(fieldType)) { numberExpression = entityPath.getNumber(fieldName, (Class<Byte>) fieldType); } else if (Double.class.isAssignableFrom(fieldType) || double.class == fieldType) { numberExpression = entityPath.getNumber(fieldName, (Class<Double>) fieldType); } else if (Float.class.isAssignableFrom(fieldType) || float.class == fieldType) { numberExpression = entityPath.getNumber(fieldName, (Class<Float>) fieldType); } else if (Integer.class.isAssignableFrom(fieldType) || int.class == fieldType) { numberExpression = entityPath.getNumber(fieldName, (Class<Integer>) fieldType); } else if (Long.class.isAssignableFrom(fieldType) || long.class == fieldType) { numberExpression = entityPath.getNumber(fieldName, (Class<Long>) fieldType); } else if (Short.class.isAssignableFrom(fieldType) || short.class == fieldType) { numberExpression = entityPath.getNumber(fieldName, (Class<Short>) fieldType); } if (numberExpression != null) { Number value = NumberUtils.createNumber((String) searchObj); if (StringUtils.equalsIgnoreCase(operator, OPERATOR_GOE)) { return numberExpression.goe(value); } else if (StringUtils.equalsIgnoreCase(operator, "gt")) { return numberExpression.gt(value); } else if (StringUtils.equalsIgnoreCase(operator, "like")) { return numberExpression.like((String) searchObj); } else if (StringUtils.equalsIgnoreCase(operator, OPERATOR_LOE)) { return numberExpression.loe(value); } else if (StringUtils.equalsIgnoreCase(operator, "lt")) { return numberExpression.lt(value); } } return entityPath.get(fieldName).eq(searchObj); }