@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); }
/** * If the type was not initialized, we can still figure it out at runtime. * * @param value */ private void dynamicallyInitIfNeeded(Object value) { /* Check to see if this class was already initialized, * if not, initialize it based on the type of the value. */ if (!isInitialized()) { if (value instanceof Integer) { init(new Integer(min.intValue()), new Integer(max.intValue())); } else if (value instanceof Byte) { init(new Byte(min.byteValue()), new Byte(max.byteValue())); } else if (value instanceof Short) { init(new Short(min.shortValue()), new Short(max.shortValue())); } else { init(min, max); } } }
public static DeviceOracle createDeviceOracle( String macStr, Long vlan, String entityClassName, String ipStr, String dpidStr, Short switchPortNumber) throws BigDBException { if (macStr == null) { throw new BigDBException("Mac Address cannot be null."); } Short vlanShort = vlan == null ? null : Short.valueOf(vlan.shortValue()); Integer port = switchPortNumber == null ? null : Integer.valueOf(switchPortNumber); Integer ipv4 = ipStr == null ? null : IPv4.toIPv4Address(ipStr); Long dpid = dpidStr == null ? null : HexString.toLong(dpidStr); IEntityClass entityClass = getDeviceService().getEntityClassifier().getEntityClassByName(entityClassName); if (entityClass == null) { throw new BigDBException( "Invalid entity class name, config the " + "entity class first: " + entityClassName); } Entity en = new Entity(HexString.toLong(macStr), vlanShort, ipv4, dpid, port, null); String deviceId = IndexedEntity.getKeyString(entityClassName, en, entityClass.getKeyFields()); return new DeviceOracle(deviceId, macStr, vlanShort, entityClassName, ipStr, dpidStr, port); }
@Override protected <T> void setNotNullValueFromDatabaseToEntity( final Cursor cursor, final int columnIndex, final T entity, final Field field, final Map<String, String> options) { Long value = cursor.getLong(columnIndex); if (field.getType().equals(Integer.TYPE) || field.getType().equals(Integer.class)) { checkRange(value, Integer.MIN_VALUE, Integer.MAX_VALUE); setValue(field, entity, value.intValue()); } else if (field.getType().equals(Short.TYPE) || field.getType().equals(Short.class)) { checkRange(value, Short.MIN_VALUE, Short.MAX_VALUE); setValue(field, entity, value.shortValue()); } else if (field.getType().equals(Long.TYPE) || field.getType().equals(Long.class)) { setValue(field, entity, value); } else if (field.getType().equals(Byte.TYPE) || field.getType().equals(Byte.class)) { checkRange(value, Byte.MIN_VALUE, Byte.MAX_VALUE); setValue(field, entity, value.byteValue()); } else if (field.getType().equals(BigInteger.class)) { setValue(field, entity, BigInteger.valueOf(value)); } else { throw new IllegalStateException( "Cannot get integer from '" + field.getType().getCanonicalName() + "'."); } }
public static Short toShort(Long l) { if (l == null) { return null; } if (l > Short.MAX_VALUE || l < Short.MIN_VALUE) { throw new IllegalArgumentException("Couldn't cast value to short " + l); } return l.shortValue(); }
public short shortValue(Long value) { return value.shortValue(); }
/** * @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; }
/** * Returns the value of this unsigned 32-bit integer object as a short This method returns the * least significant 16 bits. * * @return short value of this unsigned 32-bit integer object as a short */ public short shortValue() { return value.shortValue(); }