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
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
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 vote down vote up
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
public ConstantExecutionNode(String value) {
  if (NumberUtils.isCreatable(value)) {
    _value = NumberUtils.createNumber(value);
    _returnType = Number.class;
  } else {
    _value = value;
    _returnType = String.class;
  }
}
 
Example 5
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
@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
@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
@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
@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 vote down vote up
@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 vote down vote up
/**
 * 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
/**
 * 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
/**
 * {@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);
}