@SuppressWarnings("unchecked") @Override public <T> T convertTo(Serializable value, Class<T> toType) throws PropertyConversionException { if (value == null || Long.class == toType) { return (T) value; } Long v = (Long) value; if (toType == Integer.class) { return (T) new Integer(v.intValue()); } if (toType == String.class) { return (T) v.toString(); } if (toType == Double.class) { return (T) new Double(v.doubleValue()); } if (toType == Float.class) { return (T) new Float(v.floatValue()); } if (toType == Short.class) { return (T) new Short(v.shortValue()); } if (toType == Byte.class) { return (T) new Byte(v.byteValue()); } throw new PropertyConversionException(value.getClass(), toType); }
private static void printForHiszilla() { Map<String, Long> minutesByHiszillaId = new HashMap<String, Long>(); for (DayAtWork day : days) { for (WorkActivity activity : day.getActivities()) { if (activity.isAlreadyBookedInHiszilla()) continue; String hiszillaId = activity.getHiszillaId(); if (hiszillaId == null) continue; long time = activity.getWorktimeInMinutes(); Long total = minutesByHiszillaId.get(hiszillaId); if (total == null) { total = time; } else { total = total.longValue() + time; } minutesByHiszillaId.put(hiszillaId, total); } } for (Entry<String, Long> entry : minutesByHiszillaId.entrySet()) { Long minutes = entry.getValue(); String hiszillaId = entry.getKey(); String time = String.valueOf(minutes.floatValue() / 60f); System.out.println( time + " | " + hiszillaId + " | https://hiszilla.his.de/hiszilla/show_bug.cgi?id=" + hiszillaId); } }
public boolean mediaIsValid() { boolean returnValue = true; // check if file is too big FacesContext context = FacesContext.getCurrentInstance(); ExternalContext external = context.getExternalContext(); Long fileSize = (Long) ((ServletContext) external.getContext()).getAttribute("TEMP_FILEUPLOAD_SIZE"); Long maxSize = (Long) ((ServletContext) external.getContext()).getAttribute("FILEUPLOAD_SIZE_MAX"); // log.info("**** filesize is ="+fileSize); // log.info("**** maxsize is ="+maxSize); ((ServletContext) external.getContext()).removeAttribute("TEMP_FILEUPLOAD_SIZE"); if (fileSize != null) { float fileSize_float = fileSize.floatValue() / 1024; int tmp = Math.round(fileSize_float * 10.0f); fileSize_float = (float) tmp / 10.0f; float maxSize_float = maxSize.floatValue() / 1024; int tmp0 = Math.round(maxSize_float * 10.0f); maxSize_float = (float) tmp0 / 10.0f; String err1 = (String) ContextUtil.getLocalizedString( "org.sakaiproject.tool.assessment.bundle.DeliveryMessages", "file_upload_error"); String err2 = (String) ContextUtil.getLocalizedString( "org.sakaiproject.tool.assessment.bundle.DeliveryMessages", "file_uploaded"); String err3 = (String) ContextUtil.getLocalizedString( "org.sakaiproject.tool.assessment.bundle.DeliveryMessages", "max_size_allowed"); String err4 = (String) ContextUtil.getLocalizedString( "org.sakaiproject.tool.assessment.bundle.DeliveryMessages", "upload_again"); String err = err2 + fileSize_float + err3 + maxSize_float + err4; context.addMessage("file_upload_error", new FacesMessage(err1)); context.addMessage("file_upload_error", new FacesMessage(err)); returnValue = false; } return returnValue; }
public float floatValue(Long value) { return value.floatValue(); }
/** * 转换核心实现方法 * * @param obj * @param type * @param format * @return Object * @throws TypeCastException */ public static Object convert(Object obj, String type, String format) throws TypeCastException { Locale locale = new Locale("zh", "CN", ""); if (obj == null) return null; if (obj.getClass().getName().equals(type)) return obj; if ("Object".equals(type) || "java.lang.Object".equals(type)) return obj; String fromType = null; if (obj instanceof String) { fromType = "String"; String str = (String) obj; if ("String".equals(type) || "java.lang.String".equals(type)) return obj; if (str.length() == 0) return null; if ("Boolean".equals(type) || "java.lang.Boolean".equals(type)) { Boolean value = null; if (str.equalsIgnoreCase("TRUE")) value = new Boolean(true); else value = new Boolean(false); return value; } if ("Double".equals(type) || "java.lang.Double".equals(type)) try { Number tempNum = getNf(locale).parse(str); return new Double(tempNum.doubleValue()); } catch (ParseException e) { throw new TypeCastException("Could not convert " + str + " to " + type + ": ", e); } if ("BigDecimal".equals(type) || "java.math.BigDecimal".equals(type)) try { BigDecimal retBig = new BigDecimal(str); int iscale = str.indexOf("."); int keylen = str.length(); if (iscale > -1) { iscale = keylen - (iscale + 1); return retBig.setScale(iscale, 5); } else { return retBig.setScale(0, 5); } } catch (Exception e) { throw new TypeCastException("Could not convert " + str + " to " + type + ": ", e); } if ("Float".equals(type) || "java.lang.Float".equals(type)) try { Number tempNum = getNf(locale).parse(str); return new Float(tempNum.floatValue()); } catch (ParseException e) { throw new TypeCastException("Could not convert " + str + " to " + type + ": ", e); } if ("Long".equals(type) || "java.lang.Long".equals(type)) try { NumberFormat nf = getNf(locale); nf.setMaximumFractionDigits(0); Number tempNum = nf.parse(str); return new Long(tempNum.longValue()); } catch (ParseException e) { throw new TypeCastException("Could not convert " + str + " to " + type + ": ", e); } if ("Integer".equals(type) || "java.lang.Integer".equals(type)) try { NumberFormat nf = getNf(locale); nf.setMaximumFractionDigits(0); Number tempNum = nf.parse(str); return new Integer(tempNum.intValue()); } catch (ParseException e) { throw new TypeCastException("Could not convert " + str + " to " + type + ": ", e); } if ("Date".equals(type) || "java.sql.Date".equals(type)) { if (format == null || format.length() == 0) try { return Date.valueOf(str); } catch (Exception e) { try { DateFormat df = null; if (locale != null) df = DateFormat.getDateInstance(3, locale); else df = DateFormat.getDateInstance(3); java.util.Date fieldDate = df.parse(str); return new Date(fieldDate.getTime()); } catch (ParseException e1) { throw new TypeCastException("Could not convert " + str + " to " + type + ": ", e); } } try { SimpleDateFormat sdf = new SimpleDateFormat(format); java.util.Date fieldDate = sdf.parse(str); return new Date(fieldDate.getTime()); } catch (ParseException e) { throw new TypeCastException("Could not convert " + str + " to " + type + ": ", e); } } if ("Timestamp".equals(type) || "java.sql.Timestamp".equals(type)) { if (str.length() == 10) str = str + " 00:00:00"; if (format == null || format.length() == 0) try { return Timestamp.valueOf(str); } catch (Exception e) { try { DateFormat df = null; if (locale != null) df = DateFormat.getDateTimeInstance(3, 3, locale); else df = DateFormat.getDateTimeInstance(3, 3); java.util.Date fieldDate = df.parse(str); return new Timestamp(fieldDate.getTime()); } catch (ParseException e1) { throw new TypeCastException("Could not convert " + str + " to " + type + ": ", e); } } try { SimpleDateFormat sdf = new SimpleDateFormat(format); java.util.Date fieldDate = sdf.parse(str); return new Timestamp(fieldDate.getTime()); } catch (ParseException e) { throw new TypeCastException("Could not convert " + str + " to " + type + ": ", e); } } else { throw new TypeCastException( "Conversion from " + fromType + " to " + type + " not currently supported"); } } if (obj instanceof BigDecimal) { fromType = "BigDecimal"; BigDecimal bigD = (BigDecimal) obj; if ("String".equals(type)) return getNf(locale).format(bigD.doubleValue()); if ("BigDecimal".equals(type) || "java.math.BigDecimal".equals(type)) return obj; if ("Double".equals(type)) return new Double(bigD.doubleValue()); if ("Float".equals(type)) return new Float(bigD.floatValue()); if ("Long".equals(type)) return new Long(Math.round(bigD.doubleValue())); if ("Integer".equals(type)) return new Integer((int) Math.round(bigD.doubleValue())); else throw new TypeCastException( "Conversion from " + fromType + " to " + type + " not currently supported"); } if (obj instanceof Double) { fromType = "Double"; Double dbl = (Double) obj; if ("String".equals(type) || "java.lang.String".equals(type)) return getNf(locale).format(dbl.doubleValue()); if ("Double".equals(type) || "java.lang.Double".equals(type)) return obj; if ("Float".equals(type) || "java.lang.Float".equals(type)) return new Float(dbl.floatValue()); if ("Long".equals(type) || "java.lang.Long".equals(type)) return new Long(Math.round(dbl.doubleValue())); if ("Integer".equals(type) || "java.lang.Integer".equals(type)) return new Integer((int) Math.round(dbl.doubleValue())); if ("BigDecimal".equals(type) || "java.math.BigDecimal".equals(type)) return new BigDecimal(dbl.toString()); else throw new TypeCastException( "Conversion from " + fromType + " to " + type + " not currently supported"); } if (obj instanceof Float) { fromType = "Float"; Float flt = (Float) obj; if ("String".equals(type)) return getNf(locale).format(flt.doubleValue()); if ("BigDecimal".equals(type) || "java.math.BigDecimal".equals(type)) return new BigDecimal(flt.doubleValue()); if ("Double".equals(type)) return new Double(flt.doubleValue()); if ("Float".equals(type)) return obj; if ("Long".equals(type)) return new Long(Math.round(flt.doubleValue())); if ("Integer".equals(type)) return new Integer((int) Math.round(flt.doubleValue())); else throw new TypeCastException( "Conversion from " + fromType + " to " + type + " not currently supported"); } if (obj instanceof Long) { fromType = "Long"; Long lng = (Long) obj; if ("String".equals(type) || "java.lang.String".equals(type)) return getNf(locale).format(lng.longValue()); if ("Double".equals(type) || "java.lang.Double".equals(type)) return new Double(lng.doubleValue()); if ("Float".equals(type) || "java.lang.Float".equals(type)) return new Float(lng.floatValue()); if ("BigDecimal".equals(type) || "java.math.BigDecimal".equals(type)) return new BigDecimal(lng.toString()); if ("Long".equals(type) || "java.lang.Long".equals(type)) return obj; if ("Integer".equals(type) || "java.lang.Integer".equals(type)) return new Integer(lng.intValue()); else throw new TypeCastException( "Conversion from " + fromType + " to " + type + " not currently supported"); } if (obj instanceof Integer) { fromType = "Integer"; Integer intgr = (Integer) obj; if ("String".equals(type) || "java.lang.String".equals(type)) return getNf(locale).format(intgr.longValue()); if ("Double".equals(type) || "java.lang.Double".equals(type)) return new Double(intgr.doubleValue()); if ("Float".equals(type) || "java.lang.Float".equals(type)) return new Float(intgr.floatValue()); if ("BigDecimal".equals(type) || "java.math.BigDecimal".equals(type)) { String str = intgr.toString(); BigDecimal retBig = new BigDecimal(intgr.doubleValue()); int iscale = str.indexOf("."); int keylen = str.length(); if (iscale > -1) { iscale = keylen - (iscale + 1); return retBig.setScale(iscale, 5); } else { return retBig.setScale(0, 5); } } if ("Long".equals(type) || "java.lang.Long".equals(type)) return new Long(intgr.longValue()); if ("Integer".equals(type) || "java.lang.Integer".equals(type)) return obj; else throw new TypeCastException( "Conversion from " + fromType + " to " + type + " not currently supported"); } if (obj instanceof Date) { fromType = "Date"; Date dte = (Date) obj; if ("String".equals(type) || "java.lang.String".equals(type)) if (format == null || format.length() == 0) { return dte.toString(); } else { SimpleDateFormat sdf = new SimpleDateFormat(format); return sdf.format(new java.util.Date(dte.getTime())); } if ("Date".equals(type) || "java.sql.Date".equals(type)) return obj; if ("Time".equals(type) || "java.sql.Time".equals(type)) throw new TypeCastException( "Conversion from " + fromType + " to " + type + " not currently supported"); if ("Timestamp".equals(type) || "java.sql.Timestamp".equals(type)) return new Timestamp(dte.getTime()); else throw new TypeCastException( "Conversion from " + fromType + " to " + type + " not currently supported"); } if (obj instanceof Timestamp) { fromType = "Timestamp"; Timestamp tme = (Timestamp) obj; if ("String".equals(type) || "java.lang.String".equals(type)) if (format == null || format.length() == 0) { return (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(tme).toString(); } else { SimpleDateFormat sdf = new SimpleDateFormat(format); return sdf.format(new java.util.Date(tme.getTime())); } if ("Date".equals(type) || "java.sql.Date".equals(type)) return new Date(tme.getTime()); if ("Time".equals(type) || "java.sql.Time".equals(type)) return new Time(tme.getTime()); if ("Timestamp".equals(type) || "java.sql.Timestamp".equals(type)) return obj; else throw new TypeCastException( "Conversion from " + fromType + " to " + type + " not currently supported"); } if (obj instanceof Boolean) { fromType = "Boolean"; Boolean bol = (Boolean) obj; if ("Boolean".equals(type) || "java.lang.Boolean".equals(type)) return bol; if ("String".equals(type) || "java.lang.String".equals(type)) return bol.toString(); if ("Integer".equals(type) || "java.lang.Integer".equals(type)) { if (bol.booleanValue()) return new Integer(1); else return new Integer(0); } else { throw new TypeCastException( "Conversion from " + fromType + " to " + type + " not currently supported"); } } if ("String".equals(type) || "java.lang.String".equals(type)) return obj.toString(); else throw new TypeCastException( "Conversion from " + obj.getClass().getName() + " to " + type + " not currently supported"); }
/** * @param type May be null if the type is unknown. * @param elementType May be null if the type is unknown. * @return May be null. */ public <T> T readValue(Class<T> type, Class elementType, Object jsonData) { if (jsonData == null) return null; if (jsonData instanceof OrderedMap) { OrderedMap<String, Object> jsonMap = (OrderedMap) jsonData; String className = typeName == null ? null : (String) jsonMap.remove(typeName); if (className != null) { try { type = (Class<T>) Class.forName(className); } catch (ClassNotFoundException ex) { type = tagToClass.get(className); if (type == null) throw new SerializationException(ex); } } if (type == String.class || type == Integer.class || type == Boolean.class || type == Float.class || type == Long.class || type == Double.class || type == Short.class || type == Byte.class || type == Character.class) { return readValue("value", type, jsonData); } Object object; if (type != null) { Serializer serializer = classToSerializer.get(type); if (serializer != null) return (T) serializer.read(this, jsonMap, type); object = newInstance(type); if (object instanceof Serializable) { ((Serializable) object).read(this, jsonMap); return (T) object; } if (object instanceof HashMap) { HashMap result = (HashMap) object; for (Entry entry : jsonMap.entries()) result.put(entry.key, readValue(elementType, null, entry.value)); return (T) result; } } else object = new OrderedMap(); if (object instanceof ObjectMap) { ObjectMap result = (ObjectMap) object; for (String key : jsonMap.orderedKeys()) result.put(key, readValue(elementType, null, jsonMap.get(key))); return (T) result; } readFields(object, jsonMap); return (T) object; } if (type != null) { Serializer serializer = classToSerializer.get(type); if (serializer != null) return (T) serializer.read(this, jsonData, type); } if (jsonData instanceof Array) { Array array = (Array) jsonData; if (type == null || Array.class.isAssignableFrom(type)) { Array newArray = type == null ? new Array() : (Array) newInstance(type); newArray.ensureCapacity(array.size); for (int i = 0, n = array.size; i < n; i++) newArray.add(readValue(elementType, null, array.get(i))); return (T) newArray; } if (List.class.isAssignableFrom(type)) { List newArray = type == null ? new ArrayList(array.size) : (List) newInstance(type); for (int i = 0, n = array.size; i < n; i++) newArray.add(readValue(elementType, null, array.get(i))); return (T) newArray; } if (type.isArray()) { Class componentType = type.getComponentType(); if (elementType == null) elementType = componentType; Object newArray = java.lang.reflect.Array.newInstance(componentType, array.size); for (int i = 0, n = array.size; i < n; i++) java.lang.reflect.Array.set(newArray, i, readValue(elementType, null, array.get(i))); return (T) newArray; } throw new SerializationException( "Unable to convert value to required type: " + jsonData + " (" + type.getName() + ")"); } if (jsonData instanceof Float) { Float floatValue = (Float) jsonData; try { if (type == null || type == float.class || type == Float.class) return (T) floatValue; if (type == int.class || type == Integer.class) return (T) (Integer) floatValue.intValue(); if (type == long.class || type == Long.class) return (T) (Long) floatValue.longValue(); if (type == double.class || type == Double.class) return (T) (Double) floatValue.doubleValue(); if (type == short.class || type == Short.class) return (T) (Short) floatValue.shortValue(); if (type == byte.class || type == Byte.class) return (T) (Byte) floatValue.byteValue(); } catch (NumberFormatException ignored) { } jsonData = String.valueOf(jsonData); } if (jsonData instanceof Long) { Long longValue = (Long) jsonData; try { if (type == null || type == long.class || type == Long.class) return (T) longValue; if (type == int.class || type == Integer.class) return (T) (Integer) longValue.intValue(); if (type == float.class || type == Float.class) return (T) (Float) longValue.floatValue(); if (type == double.class || type == Double.class) return (T) (Double) longValue.doubleValue(); if (type == short.class || type == Short.class) return (T) (Short) longValue.shortValue(); if (type == byte.class || type == Byte.class) return (T) (Byte) longValue.byteValue(); } catch (NumberFormatException ignored) { } jsonData = String.valueOf(jsonData); } if (jsonData instanceof Boolean) jsonData = String.valueOf(jsonData); if (jsonData instanceof String) { String string = (String) jsonData; if (type == null || type == String.class) return (T) jsonData; try { if (type == int.class || type == Integer.class) return (T) Integer.valueOf(string); if (type == float.class || type == Float.class) return (T) Float.valueOf(string); if (type == long.class || type == Long.class) return (T) Long.valueOf(string); if (type == double.class || type == Double.class) return (T) Double.valueOf(string); if (type == short.class || type == Short.class) return (T) Short.valueOf(string); if (type == byte.class || type == Byte.class) return (T) Byte.valueOf(string); } catch (NumberFormatException ignored) { } if (type == boolean.class || type == Boolean.class) return (T) Boolean.valueOf(string); if (type == char.class || type == Character.class) return (T) (Character) string.charAt(0); if (Enum.class.isAssignableFrom(type)) { Object[] constants = type.getEnumConstants(); for (int i = 0, n = constants.length; i < n; i++) if (string.equals(constants[i].toString())) return (T) constants[i]; } if (type == CharSequence.class) return (T) string; throw new SerializationException( "Unable to convert value to required type: " + jsonData + " (" + type.getName() + ")"); } return null; }
void getNetCmd(String s) { String cmd = null; String cmd1 = null; String args = null; String arg1 = null; String arg2 = null; int sp1 = 0; if (s.length() <= 0) return; s = s.trim(); if (s.length() > 4) { cmd = s; sp1 = s.indexOf(" "); if (sp1 >= 0) { cmd1 = cmd.substring(0, sp1).trim(); args = cmd.substring(sp1).trim(); sp1 = args.indexOf(" "); if (sp1 <= 0) { arg1 = args.trim(); arg2 = null; } else { arg1 = args.substring(0, sp1).trim(); arg2 = args.substring(sp1).trim(); } } else { cmd1 = s; arg1 = null; arg2 = null; } } else { cmd1 = cmd = s; } // delay(100); if (cmd1.equals("0108")) { sendNetCmd("0109"); } else if (s.equals("Active")) { fActive(); } else if (s.equals("Passive")) { fPassive(); } else if (cmd1.equals("5001")) { imageCanvas.switchMode(2); sendNetCmd("9001 9001"); } else if (cmd1.equals("5002")) { imageCanvas.switchMode(4); sendNetCmd("9001 9001"); } else if (cmd1.equals("5003")) { imageCanvas.switchMode(5); sendNetCmd("9001 9001"); } else if (cmd1.equals("9999")) { add_in("Closing"); halt(); } else if (cmd1.equals("9001")) { // delay(1000); imageCanvas.updateScreen(); add_in("Ready"); } else if (cmd1.equals("1009") || cmd1.equals("1010") || cmd1.equals("1011") || cmd1.equals("1012")) { xValue = xValue.valueOf(arg1); yValue = yValue.valueOf(arg2); xField.setText("" + ((float) xValue.floatValue() / 100.0f)); yField.setText("" + ((float) yValue.floatValue() / 100.0f)); sendNetCmd("9001 9001"); } else if (cmd1.substring(0, 2).equals("20")) { magValue = magValue.valueOf(arg1); sendNetCmd("9001 9001"); } else if (cmd1.equals("4011")) { if (arg1.equals("0")) { secBack.select(0); sendNetCmd("4009"); } else if (arg1.equals("1")) { secBack.select(1); sendNetCmd("4010"); } if (arg2.equals("1")) { dispMode.select(0); if (imageCanvas.screenMode != 1 && imageCanvas.screenMode != 2) sendNetCmd("5001"); } else if (arg2.equals("2")) { dispMode.select(1); if (imageCanvas.screenMode != 4) sendNetCmd("5002"); } else if (arg2.equals("3")) { dispMode.select(2); if (imageCanvas.screenMode != 5) sendNetCmd("5003"); } sendNetCmd("9001 9001"); } else if (cmd1.equals("3003")) { sendNetCmd("9001 9001"); } }
/** * Returns the value of this unsigned 32-bit integer object as a float * * @return float value of this unsigned 32-bit integer object as a float */ public float floatValue() { return value.floatValue(); }