List of usage examples for java.lang Number getClass
@HotSpotIntrinsicCandidate public final native Class<?> getClass();
From source file:com.addthis.codec.config.ConfigTraversingParser.java
protected JsonNode currentNumericNode() throws JsonParseException { ConfigValue configValue = currentNode(); if ((configValue == null) || (configValue.valueType() != ConfigValueType.NUMBER)) { JsonToken t = (configValue == null) ? null : ConfigNodeCursor.forConfigValue(configValue); throw _constructError("Current token (" + t + ") not numeric, can not use numeric value accessors"); }//from w ww . ja va 2s .c o m Number value = (Number) configValue.unwrapped(); if (value instanceof Double) { return JsonNodeFactory.instance.numberNode((Double) value); } if (value instanceof Long) { return JsonNodeFactory.instance.numberNode((Long) value); } if (value instanceof Integer) { return JsonNodeFactory.instance.numberNode((Integer) value); } // only possible if Config has since added more numeric types throw _constructError(value.getClass() + " is not a supported numeric config type"); }
From source file:com.haulmont.cuba.gui.export.ExcelExporter.java
protected void formatValueCell(HSSFCell cell, @Nullable Object cellValue, @Nullable MetaPropertyPath metaPropertyPath, int sizersIndex, int notificationRequired, int level, @Nullable Integer groupChildCount) { if (cellValue == null) { return;//from w w w . j ava 2 s. c om } String childCountValue = ""; if (groupChildCount != null) { childCountValue = " (" + groupChildCount + ")"; } if (cellValue instanceof IdProxy) { cellValue = ((IdProxy) cellValue).get(); } if (cellValue instanceof Number) { Number n = (Number) cellValue; final Datatype datatype = Datatypes.getNN(n.getClass()); String str; if (sizersIndex == 0) { str = createSpaceString(level) + datatype.format(n); cell.setCellValue(str); } else { try { str = datatype.format(n); Number result = (Number) datatype.parse(str); if (result != null) { if (n instanceof Integer || n instanceof Long || n instanceof Byte || n instanceof Short) { cell.setCellValue(result.longValue()); cell.setCellStyle(integerFormatCellStyle); } else { cell.setCellValue(result.doubleValue()); cell.setCellStyle(doubleFormatCellStyle); } } } catch (ParseException e) { throw new RuntimeException("Unable to parse numeric value", e); } cell.setCellType(CellType.NUMERIC); } if (sizers[sizersIndex].isNotificationRequired(notificationRequired)) { sizers[sizersIndex].notifyCellValue(str, stdFont); } } else if (cellValue instanceof Date) { Class javaClass = null; boolean supportTimezones = false; TimeZone timeZone = userSessionSource.getUserSession().getTimeZone(); if (metaPropertyPath != null) { MetaProperty metaProperty = metaPropertyPath.getMetaProperty(); if (metaProperty.getRange().isDatatype()) { javaClass = metaProperty.getRange().asDatatype().getJavaClass(); } Boolean ignoreUserTimeZone = metadataTools.getMetaAnnotationValue(metaProperty, IgnoreUserTimeZone.class); supportTimezones = timeZone != null && Objects.equals(Date.class, javaClass) && !Boolean.TRUE.equals(ignoreUserTimeZone); } Date date = (Date) cellValue; if (supportTimezones) { TimeZone currentTimeZone = LocaleUtil.getUserTimeZone(); try { LocaleUtil.setUserTimeZone(timeZone); cell.setCellValue(date); } finally { if (Objects.equals(currentTimeZone, TimeZone.getDefault())) { LocaleUtil.resetUserTimeZone(); } else { LocaleUtil.setUserTimeZone(currentTimeZone); } } } else { cell.setCellValue(date); } if (Objects.equals(java.sql.Time.class, javaClass)) { cell.setCellStyle(timeFormatCellStyle); } else if (Objects.equals(java.sql.Date.class, javaClass)) { cell.setCellStyle(dateFormatCellStyle); } else { cell.setCellStyle(dateTimeFormatCellStyle); } if (sizers[sizersIndex].isNotificationRequired(notificationRequired)) { String str = Datatypes.getNN(Date.class).format(date); sizers[sizersIndex].notifyCellValue(str, stdFont); } } else if (cellValue instanceof Boolean) { String str = ""; if (sizersIndex == 0) { str += createSpaceString(level); } str += ((Boolean) cellValue) ? trueStr : falseStr; cell.setCellValue(new HSSFRichTextString(str)); if (sizers[sizersIndex].isNotificationRequired(notificationRequired)) { sizers[sizersIndex].notifyCellValue(str, stdFont); } } else if (cellValue instanceof EnumClass) { String nameKey = cellValue.getClass().getSimpleName() + "." + cellValue.toString(); final String message = sizersIndex == 0 ? createSpaceString(level) + messages.getMessage(cellValue.getClass(), nameKey) : messages.getMessage(cellValue.getClass(), nameKey); cell.setCellValue(message + childCountValue); if (sizers[sizersIndex].isNotificationRequired(notificationRequired)) { sizers[sizersIndex].notifyCellValue(message, stdFont); } } else if (cellValue instanceof Entity) { Entity entityVal = (Entity) cellValue; String instanceName = entityVal.getInstanceName(); String str = sizersIndex == 0 ? createSpaceString(level) + instanceName : instanceName; str = str + childCountValue; cell.setCellValue(new HSSFRichTextString(str)); if (sizers[sizersIndex].isNotificationRequired(notificationRequired)) { sizers[sizersIndex].notifyCellValue(str, stdFont); } } else if (cellValue instanceof Collection) { String str = ""; cell.setCellValue(new HSSFRichTextString(str)); if (sizers[sizersIndex].isNotificationRequired(notificationRequired)) { sizers[sizersIndex].notifyCellValue(str, stdFont); } } else { String strValue = cellValue == null ? "" : cellValue.toString(); String str = sizersIndex == 0 ? createSpaceString(level) + strValue : strValue; str = str + childCountValue; cell.setCellValue(new HSSFRichTextString(str)); if (sizers[sizersIndex].isNotificationRequired(notificationRequired)) { sizers[sizersIndex].notifyCellValue(str, stdFont); } } }
From source file:NumberRange.java
/** * <p>Constructs a new <code>NumberRange</code> with the specified * minimum and maximum numbers (both inclusive).</p> * //from w w w. java 2s .c o m * <p>The arguments may be passed in the order (min,max) or (max,min). The * {@link #getMinimumNumber()} and {@link #getMaximumNumber()} methods will return the * correct value.</p> * * <p>This constructor is designed to be used with two <code>Number</code> * objects of the same type. If two objects of different types are passed in, * an exception is thrown.</p> * * @param num1 first number that defines the edge of the range, inclusive * @param num2 second number that defines the edge of the range, inclusive * @throws IllegalArgumentException if either number is <code>null</code> * @throws IllegalArgumentException if the numbers are of different types * @throws IllegalArgumentException if the numbers don't implement <code>Comparable</code> */ public NumberRange(Number num1, Number num2) { if (num1 == null || num2 == null) { throw new IllegalArgumentException("The numbers must not be null"); } if (num1.getClass() != num2.getClass()) { throw new IllegalArgumentException("The numbers must be of the same type"); } if (num1 instanceof Comparable == false) { throw new IllegalArgumentException("The numbers must implement Comparable"); } if (num1 instanceof Double) { if (((Double) num1).isNaN() || ((Double) num2).isNaN()) { throw new IllegalArgumentException("The number must not be NaN"); } } else if (num1 instanceof Float) { if (((Float) num1).isNaN() || ((Float) num2).isNaN()) { throw new IllegalArgumentException("The number must not be NaN"); } } int compare = ((Comparable) num1).compareTo(num2); if (compare == 0) { this.min = num1; this.max = num1; } else if (compare > 0) { this.min = num2; this.max = num1; } else { this.min = num1; this.max = num2; } }
From source file:com.sindicetech.siren.solr.facet.SirenFieldFacetExtractor.java
private void generateFacetsForCustomDatatypeLeaf(ObjectNode object, String fieldName, ExtendedJsonField sirenField, String path, List<SirenFacetEntry> facets) { Iterator<Entry<String, JsonNode>> iterator = object.getFields(); String datatype = null;//w ww . j av a 2s . co m String value = null; while (iterator.hasNext()) { Entry<String, JsonNode> entry = iterator.next(); if ("_datatype_".equals(entry.getKey())) { datatype = entry.getValue().asText(); } else if ("_value_".equals(entry.getKey())) { value = entry.getValue().asText(); } else { logger.warn("Unexpected field {} in custom datatype object: {}", entry.getKey(), object.asText()); continue; } } if (datatype == null || value == null) { logger.warn( "Unexpected form of custom datatype object: {}. Not generating facets for this nested object.", object.asText()); return; } SirenFacetEntry entry = new SirenFacetEntry(); entry.fieldName = fieldName; entry.path = path; Datatype customDatatype = sirenField.getDatatypes().get(datatype); if (customDatatype instanceof TrieDatatype) { NumericAnalyzer analyzer = (NumericAnalyzer) customDatatype.getAnalyzer(); NumericParser parser = analyzer.getNumericParser(); try { Number number = parser.parse(new StringReader(value)); if ((number instanceof Float) || (number instanceof Double)) { entry.datatype = FacetDatatype.DOUBLE; entry.value = number.doubleValue(); } else if ((number instanceof Integer) || (number instanceof Long)) { entry.datatype = FacetDatatype.LONG; entry.value = number.longValue(); } else { logger.warn( "Unknown number type {} in custom datatype in nested object {}. Not creating facet field.", number.getClass().getCanonicalName(), object.asText()); return; } } catch (IOException e) { logger.warn("Problem parsing custom datatype {} in nested object {}: " + e.getMessage(), datatype, object.asText()); return; } } else { entry.datatype = FacetDatatype.STRING; entry.value = value; } facets.add(entry); }
From source file:net.paoding.rose.jade.statement.UpdateQuerier.java
private Object executeSingle(StatementRuntime runtime, Class<?> returnType) { Number result; DataAccess dataAccess = dataAccessProvider.getDataAccess(// runtime.getMetaData(), runtime.getProperties()); if (returnGeneratedKeys) { ArrayList<Number> keys = new ArrayList<Number>(1); KeyHolder generatedKeyHolder = new GeneratedKeyHolder(keys); dataAccess.update(runtime.getSQL(), runtime.getArgs(), generatedKeyHolder); if (keys.size() > 0) { result = generatedKeyHolder.getKey(); } else {/*from w w w .j a va2 s . c om*/ result = null; } } else { result = new Integer(dataAccess.update(runtime.getSQL(), runtime.getArgs(), null)); } // if (result == null || returnType == void.class) { return null; } if (returnType == result.getClass()) { return result; } // ? if (returnType == Integer.class) { return result.intValue(); } else if (returnType == Long.class) { return result.longValue(); } else if (returnType == Boolean.class) { return result.intValue() > 0 ? Boolean.TRUE : Boolean.FALSE; } else if (returnType == Double.class) { return result.doubleValue(); } else if (returnType == Float.class) { return result.floatValue(); } else if (Number.class.isAssignableFrom(returnType)) { return result; } else { throw new DataRetrievalFailureException("The generated key is not of a supported numeric type. " + "Unable to cast [" + result.getClass().getName() + "] to [" + Number.class.getName() + "]"); } }
From source file:NumberUtils.java
/** * Convert the given number into an instance of the given target class. * * @param number the number to convert * @param targetClass the target class to convert to * @return the converted number// w w w. j a va2 s . c o m * @throws IllegalArgumentException if the target class is not supported * (i.e. not a standard Number subclass as included in the JDK) * @see java.lang.Byte * @see java.lang.Short * @see java.lang.Integer * @see java.lang.Long * @see java.math.BigInteger * @see java.lang.Float * @see java.lang.Double * @see java.math.BigDecimal */ public static Number convertNumberToTargetClass(Number number, Class targetClass) throws IllegalArgumentException { if (targetClass.isInstance(number)) { return number; } else if (targetClass.equals(Byte.class)) { long value = number.longValue(); if (value < Byte.MIN_VALUE || value > Byte.MAX_VALUE) { raiseOverflowException(number, targetClass); } return number.byteValue(); } else if (targetClass.equals(Short.class)) { long value = number.longValue(); if (value < Short.MIN_VALUE || value > Short.MAX_VALUE) { raiseOverflowException(number, targetClass); } return number.shortValue(); } else if (targetClass.equals(Integer.class)) { long value = number.longValue(); if (value < Integer.MIN_VALUE || value > Integer.MAX_VALUE) { raiseOverflowException(number, targetClass); } return number.intValue(); } else if (targetClass.equals(Long.class)) { return number.longValue(); } else if (targetClass.equals(Float.class)) { return number.floatValue(); } else if (targetClass.equals(Double.class)) { return number.doubleValue(); } else if (targetClass.equals(BigInteger.class)) { return BigInteger.valueOf(number.longValue()); } else if (targetClass.equals(BigDecimal.class)) { // using BigDecimal(String) here, to avoid unpredictability of BigDecimal(double) // (see BigDecimal javadoc for details) return new BigDecimal(number.toString()); } else { throw new IllegalArgumentException("Could not convert number [" + number + "] of type [" + number.getClass().getName() + "] to unknown target class [" + targetClass.getName() + "]"); } }
From source file:com.sinosoft.one.data.jade.statement.UpdateQuerier.java
private Object executeSingle(StatementRuntime runtime, Class<?> returnType) { // DAOIdentity?1/20 if (returnType == Identity.class) { if (new Random().nextInt(20) == 1) { new IllegalArgumentException( "message by zhiliang.wang: change the deprecated Identity to @ReturnGeneratedKeys please: " + runtime.getMetaData()).printStackTrace(); }/* ww w . j av a 2s . c o m*/ } Number result; DataAccess dataAccess = new DataAccessImpl(em); if (returnGeneratedKeys) { //ArrayList<Number> keys = new ArrayList<Number>(1); List<Map<String, Object>> keys = new ArrayList<Map<String, Object>>(); KeyHolder generatedKeyHolder = new GeneratedKeyHolder(keys); dataAccess.update(runtime.getSQL(), runtime.getArgs(), generatedKeyHolder); if (keys.size() > 0) { result = generatedKeyHolder.getKey(); } else { result = null; } } else { result = new Integer(dataAccess.update(runtime.getSQL(), runtime.getArgs(), null)); } // if (result == null || returnType == void.class) { return null; } if (returnType == result.getClass()) { return result; } // ? if (returnType == Integer.class) { return result.intValue(); } else if (returnType == Long.class) { return result.longValue(); } else if (returnType == Boolean.class) { return result.intValue() > 0 ? Boolean.TRUE : Boolean.FALSE; } else if (returnType == Double.class) { return result.doubleValue(); } else if (returnType == Float.class) { return result.floatValue(); } else if (returnType == Identity.class) { return new Identity((Number) result); } else if (Number.class.isAssignableFrom(returnType)) { return result; } else { throw new DataRetrievalFailureException("The generated key is not of a supported numeric type. " + "Unable to cast [" + result.getClass().getName() + "] to [" + Number.class.getName() + "]"); } }
From source file:com.gzj.tulip.jade.statement.UpdateQuerier.java
private Object executeSingle(StatementRuntime runtime) { Number result; DataAccess dataAccess = dataAccessFactory.getDataAccess(// runtime.getMetaData(), runtime.getAttributes()); if (returnGeneratedKeys.shouldReturnGerneratedKeys(runtime)) { ArrayList<Map<String, Object>> keys = new ArrayList<Map<String, Object>>(1); KeyHolder generatedKeyHolder = new GeneratedKeyHolder(keys); dataAccess.update(runtime.getSQL(), runtime.getArgs(), generatedKeyHolder); if (keys.size() > 0) { result = generatedKeyHolder.getKey(); } else {/*ww w.j av a2s . c om*/ result = null; } } else { result = new Integer(dataAccess.update(runtime.getSQL(), runtime.getArgs(), null)); } // if (result == null || returnType == void.class) { return null; } if (returnType == result.getClass()) { return result; } // ? if (returnType == Integer.class) { return result.intValue(); } else if (returnType == Long.class) { return result.longValue(); } else if (returnType == Boolean.class) { return result.intValue() > 0 ? Boolean.TRUE : Boolean.FALSE; } else if (returnType == Double.class) { return result.doubleValue(); } else if (returnType == Float.class) { return result.floatValue(); } else if (returnType == Number.class) { return result; } else if (returnType == String.class || returnType == CharSequence.class) { return String.valueOf(result); } else { throw new DataRetrievalFailureException( "The generated key is not of a supported numeric type: " + returnType.getName()); } }
From source file:NumberUtils.java
/** * Convert the given number into an instance of the given target class. * @param number the number to convert/*ww w . j a va 2s. c o m*/ * @param targetClass the target class to convert to * @return the converted number * @throws IllegalArgumentException if the target class is not supported * (i.e. not a standard Number subclass as included in the JDK) * @see java.lang.Byte * @see java.lang.Short * @see java.lang.Integer * @see java.lang.Long * @see java.math.BigInteger * @see java.lang.Float * @see java.lang.Double * @see java.math.BigDecimal */ public static Number convertNumberToTargetClass(Number number, Class targetClass) throws IllegalArgumentException { if (targetClass.isInstance(number)) { return number; } else if (targetClass.equals(Byte.class)) { long value = number.longValue(); if (value < Byte.MIN_VALUE || value > Byte.MAX_VALUE) { raiseOverflowException(number, targetClass); } return new Byte(number.byteValue()); } else if (targetClass.equals(Short.class)) { long value = number.longValue(); if (value < Short.MIN_VALUE || value > Short.MAX_VALUE) { raiseOverflowException(number, targetClass); } return new Short(number.shortValue()); } else if (targetClass.equals(Integer.class)) { long value = number.longValue(); if (value < Integer.MIN_VALUE || value > Integer.MAX_VALUE) { raiseOverflowException(number, targetClass); } return new Integer(number.intValue()); } else if (targetClass.equals(Long.class)) { return new Long(number.longValue()); } else if (targetClass.equals(Float.class)) { return new Float(number.floatValue()); } else if (targetClass.equals(Double.class)) { return new Double(number.doubleValue()); } else if (targetClass.equals(BigInteger.class)) { return BigInteger.valueOf(number.longValue()); } else if (targetClass.equals(BigDecimal.class)) { // using BigDecimal(String) here, to avoid unpredictability of BigDecimal(double) // (see BigDecimal javadoc for details) return new BigDecimal(number.toString()); } else { throw new IllegalArgumentException("Could not convert number [" + number + "] of type [" + number.getClass().getName() + "] to unknown target class [" + targetClass.getName() + "]"); } }
From source file:cz.cvut.kbss.jsonld.jackson.serialization.JacksonJsonWriter.java
@Override public void writeNumber(Number number) throws IOException { if (number instanceof Integer) { jsonGenerator.writeNumber(number.intValue()); } else if (number instanceof Long) { jsonGenerator.writeNumber(number.longValue()); } else if (number instanceof Float) { jsonGenerator.writeNumber(number.floatValue()); } else if (number instanceof Double) { jsonGenerator.writeNumber(number.doubleValue()); } else if (number instanceof BigInteger) { jsonGenerator.writeNumber((BigInteger) number); } else if (number instanceof BigDecimal) { jsonGenerator.writeNumber((BigDecimal) number); } else if (number instanceof Short) { jsonGenerator.writeNumber(number.shortValue()); } else if (number instanceof Byte) { jsonGenerator.writeNumber(number.byteValue()); } else {//w ww. j a v a 2s .c o m throw new IllegalArgumentException( "Unable to write number " + number + " of type " + number.getClass()); } }