public static int toInt(Object obj, int defaultValue) { if (obj.getClass() == int.class) { return int.class.cast(obj); } if (obj instanceof Number) { return ((Number) obj).intValue(); } else if (obj instanceof Boolean || obj.getClass() == Boolean.class) { boolean value = toBoolean(obj); return value ? 1 : 0; } else if (obj instanceof CharSequence) { try { return StringScanner.parseInt(Str.toString(obj)); } catch (Exception ex) { return defaultValue; } } return defaultValue; }
public static int toInt(Object obj, int defaultValue) { if (obj.getClass() == int.class) { return int.class.cast(obj); } if (obj instanceof Number) { return ((Number) obj).intValue(); } else if (obj instanceof Boolean || obj.getClass() == Boolean.class) { boolean value = toBoolean(obj); return value ? 1 : 0; } else if (obj instanceof CharSequence) { try { return StringScanner.parseInt(Str.toString(obj)); } catch (Exception ex) { return defaultValue; } } return defaultValue; }
/** * This is one is forgiving of null paths. * This works with getters first, i.e., properties. * * @param object object * @param property property * @return value */ public static Object getProp( Object object, final String property ) { if ( object == null ) { return null; } if ( isDigits( property ) ) { /* We can index numbers and names. */ object = idx(object, StringScanner.parseInt(property)); } Class<?> cls = object.getClass(); /** Tries the getters first. */ Map<String, FieldAccess> fields = Reflection.getPropertyFieldAccessors( cls ); if ( !fields.containsKey( property ) ) { fields = Reflection.getAllAccessorFields( cls ); } if ( !fields.containsKey( property ) ) { return null; } else { return fields.get( property ).getValue(object); } }
/** * This is one is forgiving of null paths. * This works with getters first, i.e., properties. * * @param object object * @param property property * @return value */ public static Object getProp( Object object, final String property ) { if ( object == null ) { return null; } if ( isDigits( property ) ) { /* We can index numbers and names. */ object = idx(object, StringScanner.parseInt(property)); } Class<?> cls = object.getClass(); /** Tries the getters first. */ Map<String, FieldAccess> fields = Reflection.getPropertyFieldAccessors( cls ); if ( !fields.containsKey( property ) ) { fields = Reflection.getAllAccessorFields( cls ); } if ( !fields.containsKey( property ) ) { return null; } else { return fields.get( property ).getValue(object); } }
if ( CharScanner.isDigit(c) ) { object = idx ( object, StringScanner.parseInt ( property ) );
if ( CharScanner.isDigit(c) ) { object = idx ( object, StringScanner.parseInt ( property ) );
object = idx ( object, StringScanner.parseInt ( property ) );
object = idx ( object, StringScanner.parseInt ( property ) );
object = idx ( object, StringScanner.parseInt(property) );
object = idx ( object, StringScanner.parseInt(property) );
object = idx ( object, StringScanner.parseInt ( property ) );
object = idx ( object, StringScanner.parseInt ( property ) );