@Test public void parseAndSetExtra_unsupportedCast() { IntentTag.parseAndSetExtra(mBundle, "key", "coolclass(value)"); verify(mBundle).putString("key", "coolclass(value)"); }
static void parseAndSetExtra(Bundle target, String name, String value) { Matcher m = EXTRAS_VALUE_CAST_PATTERN.matcher(value); if (m.matches()) { String type = m.group(2); String rawValue = m.group(3); String booleanValue = m.group(5); if (type == null && booleanValue != null) { target.putBoolean(name, "true".equalsIgnoreCase(booleanValue)); return; } try { switch (type != null ? type : "other") { case TYPE_BYTE: target.putByte(name, Byte.parseByte(rawValue)); break; case TYPE_CHAR: try { target.putChar(name, (char) Integer.parseInt(rawValue)); } catch (NumberFormatException nfe) { target.putChar(name, rawValue.charAt(0)); } break; case TYPE_DOUBLE: target.putDouble(name, Double.parseDouble(rawValue)); break; case TYPE_FLOAT: target.putFloat(name, Float.parseFloat(rawValue)); break; case TYPE_INT: target.putInt(name, Integer.parseInt(rawValue)); break; case TYPE_LONG: target.putLong(name, Long.parseLong(rawValue)); break; case TYPE_SHORT: target.putShort(name, Short.parseShort(rawValue)); break; default: target.putString(name, value); } } catch (NumberFormatException | ClassCastException e) { throw new BadParameterException("Bad value for param \""+name+"\": "+value, e); } } else { target.putString(name, value); } }
IntentTag extends ParameterizedTag { static void parseAndSetExtra(Bundle target, String name, String value) { Matcher m = EXTRAS_VALUE_CAST_PATTERN.matcher(value); if (m.matches()) { String type = m.group(2); String rawValue = m.group(3); String booleanValue = m.group(5); if (type == null && booleanValue != null) { target.putBoolean(name, "true".equalsIgnoreCase(booleanValue)); return; } try { switch (type != null ? type : "other") { case TYPE_BYTE: target.putByte(name, Byte.parseByte(rawValue)); break; case TYPE_CHAR: try { target.putChar(name, (char) Integer.parseInt(rawValue)); } catch (NumberFormatException nfe) { target.putChar(name, rawValue.charAt(0)); } break; case TYPE_DOUBLE: target.putDouble(name, Double.parseDouble(rawValue)); break; case TYPE_FLOAT: target.putFloat(name, Float.parseFloat(rawValue)); break; case TYPE_INT: target.putInt(name, Integer.parseInt(rawValue)); break; case TYPE_LONG: target.putLong(name, Long.parseLong(rawValue)); break; case TYPE_SHORT: target.putShort(name, Short.parseShort(rawValue)); break; default: target.putString(name, value); } } catch (NumberFormatException | ClassCastException e) { throw new BadParameterException("Bad value for param \""+name+"\": "+value, e); } } else { target.putString(name, value); } } }
IntentTag extends ParameterizedTag { static void parseAndSetExtra(Bundle target, String name, String value) { Matcher m = EXTRAS_VALUE_CAST_PATTERN.matcher(value); if (m.matches()) { String type = m.group(2); String rawValue = m.group(3); String booleanValue = m.group(5); if (type == null && booleanValue != null) { target.putBoolean(name, "true".equalsIgnoreCase(booleanValue)); return; } try { switch (type != null ? type : "other") { case TYPE_BYTE: target.putByte(name, Byte.parseByte(rawValue)); break; case TYPE_CHAR: try { target.putChar(name, (char) Integer.parseInt(rawValue)); } catch (NumberFormatException nfe) { target.putChar(name, rawValue.charAt(0)); } break; case TYPE_DOUBLE: target.putDouble(name, Double.parseDouble(rawValue)); break; case TYPE_FLOAT: target.putFloat(name, Float.parseFloat(rawValue)); break; case TYPE_INT: target.putInt(name, Integer.parseInt(rawValue)); break; case TYPE_LONG: target.putLong(name, Long.parseLong(rawValue)); break; case TYPE_SHORT: target.putShort(name, Short.parseShort(rawValue)); break; default: target.putString(name, value); } } catch (NumberFormatException | ClassCastException e) { throw new BadParameterException("Bad value for param \""+name+"\": "+value, e); } } else { target.putString(name, value); } } IntentTag(String tagStr, int taggedTextStart); }
IntentTag extends ParameterizedTag { static void parseAndSetExtra(Bundle target, String name, String value) { Matcher m = EXTRAS_VALUE_CAST_PATTERN.matcher(value); if (m.matches()) { String type = m.group(2); String rawValue = m.group(3); String booleanValue = m.group(5); if (type == null && booleanValue != null) { target.putBoolean(name, "true".equalsIgnoreCase(booleanValue)); return; } try { switch (type != null ? type : "other") { case TYPE_BYTE: target.putByte(name, Byte.parseByte(rawValue)); break; case TYPE_CHAR: try { target.putChar(name, (char) Integer.parseInt(rawValue)); } catch (NumberFormatException nfe) { target.putChar(name, rawValue.charAt(0)); } break; case TYPE_DOUBLE: target.putDouble(name, Double.parseDouble(rawValue)); break; case TYPE_FLOAT: target.putFloat(name, Float.parseFloat(rawValue)); break; case TYPE_INT: target.putInt(name, Integer.parseInt(rawValue)); break; case TYPE_LONG: target.putLong(name, Long.parseLong(rawValue)); break; case TYPE_SHORT: target.putShort(name, Short.parseShort(rawValue)); break; default: target.putString(name, value); } } catch (NumberFormatException | ClassCastException e) { throw new BadParameterException("Bad value for param \""+name+"\": "+value, e); } } else { target.putString(name, value); } } IntentTag(String tagStr, int taggedTextStart); @Override boolean matchesClosingTag(String tagName); @Override void operate(Context context, Spannable builder, int taggedTextEnd); }
IntentTag extends ParameterizedTag { static void parseAndSetExtra(Bundle target, String name, String value) { Matcher m = EXTRAS_VALUE_CAST_PATTERN.matcher(value); if (m.matches()) { String type = m.group(2); String rawValue = m.group(3); String booleanValue = m.group(5); if (type == null && booleanValue != null) { target.putBoolean(name, "true".equalsIgnoreCase(booleanValue)); return; } try { switch (type != null ? type : "other") { case TYPE_BYTE: target.putByte(name, Byte.parseByte(rawValue)); break; case TYPE_CHAR: try { target.putChar(name, (char) Integer.parseInt(rawValue)); } catch (NumberFormatException nfe) { target.putChar(name, rawValue.charAt(0)); } break; case TYPE_DOUBLE: target.putDouble(name, Double.parseDouble(rawValue)); break; case TYPE_FLOAT: target.putFloat(name, Float.parseFloat(rawValue)); break; case TYPE_INT: target.putInt(name, Integer.parseInt(rawValue)); break; case TYPE_LONG: target.putLong(name, Long.parseLong(rawValue)); break; case TYPE_SHORT: target.putShort(name, Short.parseShort(rawValue)); break; default: target.putString(name, value); } } catch (NumberFormatException | ClassCastException e) { throw new BadParameterException("Bad value for param \""+name+"\": "+value, e); } } else { target.putString(name, value); } } IntentTag(String tagStr, int taggedTextStart); @Override boolean matchesClosingTag(String tagName); @Override void operate(Context context, Spannable builder, int taggedTextEnd);  }