List of usage examples for org.apache.commons.lang3.math NumberUtils createNumber
public static Number createNumber(final String str) throws NumberFormatException
Turns a string value into a java.lang.Number.
If the string starts with 0x or -0x (lower or upper case) or # or -# , it will be interpreted as a hexadecimal Integer - or Long, if the number of digits after the prefix is more than 8 - or BigInteger if there are more than 16 digits.
From source file:clientapi.command.executor.parser.impl.NumberParser.java
@Override public final Number parse(ExecutionContext context, Type type, String raw) { if (NumberUtils.isCreatable(raw)) { return NumberUtils.createNumber(raw); }//ww w .ja v a 2 s .c o m return null; }
From source file:com.nextdoor.bender.deserializer.regex.RegexDeserializer.java
@Override public DeserializedEvent deserialize(String raw) { Matcher m = this.pattern.matcher(raw); if (!m.matches()) { throw new DeserializationException("raw event does not match string"); }//from ww w. j a v a 2s. co m int groups = m.groupCount(); JsonObject obj = new JsonObject(); for (int i = 0; i < groups && i < fields.size(); i++) { String str = m.group(i + 1); ReFieldConfig field = this.fields.get(i); switch (field.getType()) { case BOOLEAN: obj.addProperty(field.getName(), Boolean.parseBoolean(str)); break; case NUMBER: obj.addProperty(field.getName(), NumberUtils.createNumber(str)); break; case STRING: obj.addProperty(field.getName(), str); break; default: obj.addProperty(field.getName(), str); break; } } return new GenericJsonEvent(obj); }
From source file:com.nextdoor.bender.deserializer.regex.Re2jRegexDeserializer.java
@Override public DeserializedEvent deserialize(String raw) { Matcher m = this.pattern.matcher(raw); if (!m.matches()) { throw new DeserializationException("raw event does not match string"); }// ww w. ja v a 2 s .com int groups = m.groupCount(); JsonObject obj = new JsonObject(); for (int i = 0; i < groups && i < fields.size(); i++) { String str = m.group(i + 1); ReFieldConfig field = this.fields.get(i); switch (field.getType()) { case BOOLEAN: obj.addProperty(field.getName(), Boolean.parseBoolean(str)); break; case NUMBER: obj.addProperty(field.getName(), NumberUtils.createNumber(str)); break; case STRING: obj.addProperty(field.getName(), str); break; default: obj.addProperty(field.getName(), str); break; } } return new GenericJsonEvent(obj); }
From source file:com.synopsys.integration.blackduck.service.model.ComponentVersionPolicyViolationCount.java
public ComponentVersionPolicyViolationCount(final NameValuePairView nameValuePair) { final Set<PolicySeverityType> policySeverityTypes = EnumSet.allOf(PolicySeverityType.class); final Set<String> policyStatusTypeValues = policySeverityTypes.stream().map(Object::toString) .collect(Collectors.toSet()); if (policyStatusTypeValues.contains(nameValuePair.getName())) { name = PolicySeverityType.valueOf(nameValuePair.getName()); }/*from ww w . ja v a2 s . c om*/ if (nameValuePair.getValue() != null) { final String valueString = nameValuePair.getValue().toString(); if (NumberUtils.isCreatable(valueString)) { value = NumberUtils.createNumber(valueString).intValue(); } } }
From source file:com.synopsys.integration.blackduck.service.model.ComponentVersionStatusCount.java
public ComponentVersionStatusCount(final NameValuePairView nameValuePair) { final Set<PolicySummaryStatusType> policyStatusTypes = EnumSet.allOf(PolicySummaryStatusType.class); final Set<String> policyStatusTypeValues = policyStatusTypes.stream().map(Object::toString) .collect(Collectors.toSet()); if (policyStatusTypeValues.contains(nameValuePair.getName())) { name = PolicySummaryStatusType.valueOf(nameValuePair.getName()); }/*from w w w. jav a2 s .com*/ if (nameValuePair.getValue() != null) { final String valueString = nameValuePair.getValue().toString(); if (NumberUtils.isCreatable(valueString)) { value = NumberUtils.createNumber(valueString).intValue(); } } }
From source file:com.github.nlloyd.hornofmongo.adaptor.NumberLong.java
@JSConstructor public NumberLong(final Object obj) { super();/*ww w . ja v a2 s . c o m*/ if (obj instanceof Number) { realLong = ((Number) obj).longValue(); } else if (!(obj instanceof Undefined)) { String str = Context.toString(obj); Number num = NumberUtils.createNumber(str); if ((num instanceof BigInteger) || (num instanceof BigDecimal)) throw new NumberFormatException( String.format("Error: could not convert %s to NumberLong, too big.", str)); realLong = num.longValue(); } put("floatApprox", this, realLong); }
From source file:com.github.nlloyd.hornofmongo.adaptor.Timestamp.java
@JSConstructor public Timestamp(Object t, Object i) { super();/*from www . j a va 2 s . com*/ if ((t instanceof Undefined) && (i instanceof Undefined)) { this.t = 0l; this.i = 0l; } else if ((t instanceof Undefined) || (i instanceof Undefined)) { Context.throwAsScriptRuntimeEx(new IllegalArgumentException("Error: Timestamp needs 0 or 2 arguments")); } else { Number tNum = null; Number iNum = null; try { tNum = NumberUtils.createNumber(Context.toString(t)); } catch (NumberFormatException nfe) { Context.throwAsScriptRuntimeEx( new IllegalArgumentException("Error: Timestamp time must be a number")); } try { iNum = NumberUtils.createNumber(Context.toString(i)); } catch (NumberFormatException nfe) { Context.throwAsScriptRuntimeEx( new IllegalArgumentException("Error: Timestamp increment must be a number")); } if (tNum.longValue() > largestVal) throw new IllegalArgumentException("The first argument must be in seconds;" + t.toString() + " is too large (max " + largestVal + ")"); this.t = tNum.longValue(); this.i = iNum.longValue(); } put("t", this, this.t); put("i", this, this.i); }
From source file:jp.furplag.spring.booster.validation.tuple.PairNumbers.java
/** * * * @param o the object to try parsing to a number. * @param classes {@code Class<? extends Number>[]}. * @return returns true if specified classes contains the parsed number class. *//*from w w w. j a va 2 s. com*/ private static boolean isNumberOf(Object o, Class<?>... classes) { // @formatter:off return isCollectable(o) ? ObjectUtils.isAny(NumberUtils.createNumber(Objects.toString(o)).getClass(), classes) : false; // @formatter:on }
From source file:com.nextdoor.bender.operation.substitution.regex.RegexSubstitution.java
/** * Matches a regex against a field and extracts matching groups. * //from w w w. java 2s . c om * @param devent * @param config * @return * @throws FieldNotFoundException */ private Pair<String, Map<String, Object>> getRegexMatches(DeserializedEvent devent) throws FieldNotFoundException { String foundSourceField = null; Matcher matcher = null; for (String sourceField : this.srcFields) { String sourceValue; try { sourceValue = devent.getFieldAsString(sourceField); } catch (FieldNotFoundException e) { continue; } matcher = pattern.matcher(sourceValue); if (matcher.find()) { /* * Keep track of the field name that we use so it can be removed later. */ foundSourceField = sourceField; break; } } if (foundSourceField == null) { throw new FieldNotFoundException("unable to find field in: " + this.srcFields); } /* * Go through each match group in the field config and attempt to add that match group from the * regex match. If field type coercion does not succeed then field is skipped. */ Map<String, Object> matchedGroups = new HashMap<String, Object>(matcher.groupCount()); try { for (RegexSubField field : this.fields) { String matchStrVal = matcher.group(field.getRegexGroupName()); if (matchStrVal == null) { continue; } switch (field.getType()) { case BOOLEAN: matchedGroups.put(field.getKey(), Boolean.parseBoolean(matchStrVal)); break; case NUMBER: matchedGroups.put(field.getKey(), NumberUtils.createNumber(matchStrVal)); break; case STRING: matchedGroups.put(field.getKey(), matchStrVal); break; default: matchedGroups.put(field.getKey(), matchStrVal); break; } } } catch (NumberFormatException e) { throw new FieldNotFoundException("matched field is not a number"); } return new ImmutablePair<String, Map<String, Object>>(foundSourceField, matchedGroups); }
From source file:com.willwinder.universalgcodesender.firmware.grbl.GrblFirmwareSettings.java
private int getValueAsInteger(String key) throws FirmwareSettingsException { FirmwareSetting firmwareSetting = getSetting(key) .orElseThrow(() -> new FirmwareSettingsException("Couldn't find setting with key: " + key)); if (!NumberUtils.isNumber(firmwareSetting.getValue())) { throw new FirmwareSettingsException("Expected the key " + key + " to contain a numeric value but was " + firmwareSetting.getValue()); }/* w ww . j av a2 s . c o m*/ return NumberUtils.createNumber(firmwareSetting.getValue()).intValue(); }