@Test public void putNumberParam() { Bundle bundle = new Bundle(); String intKey = "int"; String intValue = "3"; String longKey = "long"; String longValue = "8L"; String longKey2 = "long2"; String longValue2 = "" + Long.MAX_VALUE; String doubleKey = "double"; String doubleValue = "4.0"; String floatKey = "float"; String floatValue = "2f"; BundleHelper.putNumberParam(bundle, intKey, intValue); BundleHelper.putNumberParam(bundle, longKey, longValue); BundleHelper.putNumberParam(bundle, longKey2, longValue2); BundleHelper.putNumberParam(bundle, floatKey, floatValue); BundleHelper.putNumberParam(bundle, doubleKey, doubleValue); Assert.assertTrue("Integer value should be 3, not " + bundle.getInt(intKey), bundle.getInt(intKey) == 3); Assert.assertTrue("Long value should be 8L, not " + bundle.getLong(longKey), bundle.getLong(longKey) == 8L); Assert.assertTrue("Long value should be " + Long.MAX_VALUE + ", not " + bundle.getLong(longKey2), bundle.getLong(longKey2) == Long.MAX_VALUE); Assert.assertTrue("Float value should be 2f, not " + bundle.getFloat(floatKey), bundle.getFloat(floatKey) == 2f); Assert.assertTrue("Double value should be 4.0, not " + bundle.getDouble(doubleKey), bundle.getDouble(doubleKey) == 4.0); }
static void putNumberParam(Bundle bundle, String key, String number) { String[] types = {"int", "long", "double", "float"}; boolean found = false; for (String type : types) { try { if (type.equals("int") && !number.toLowerCase().endsWith("l")) { int value = Integer.parseInt(number); bundle.putInt(key, value); found = true; break; } else if (type.equals("long")) { if (number.toLowerCase().endsWith("l")) { number = number.substring(0, number.length() - 1); } long value = Long.parseLong(number); bundle.putLong(key, value); found = true; break; } else if (type.equals("float")) { float value = Float.parseFloat(number); bundle.putFloat(key, value); found = true; break; } else if (type.equals("double") && !number.toLowerCase().endsWith("f")) { double value = Double.parseDouble(number); bundle.putDouble(key, value); found = true; break; } } catch (NumberFormatException e) { } } if (!found) { throw new IllegalArgumentException(); } }
BundleHelper { static void putNumberParam(Bundle bundle, String key, String number) { String[] types = {"int", "long", "double", "float"}; boolean found = false; for (String type : types) { try { if (type.equals("int") && !number.toLowerCase().endsWith("l")) { int value = Integer.parseInt(number); bundle.putInt(key, value); found = true; break; } else if (type.equals("long")) { if (number.toLowerCase().endsWith("l")) { number = number.substring(0, number.length() - 1); } long value = Long.parseLong(number); bundle.putLong(key, value); found = true; break; } else if (type.equals("float")) { float value = Float.parseFloat(number); bundle.putFloat(key, value); found = true; break; } else if (type.equals("double") && !number.toLowerCase().endsWith("f")) { double value = Double.parseDouble(number); bundle.putDouble(key, value); found = true; break; } } catch (NumberFormatException e) { } } if (!found) { throw new IllegalArgumentException(); } } }
BundleHelper { static void putNumberParam(Bundle bundle, String key, String number) { String[] types = {"int", "long", "double", "float"}; boolean found = false; for (String type : types) { try { if (type.equals("int") && !number.toLowerCase().endsWith("l")) { int value = Integer.parseInt(number); bundle.putInt(key, value); found = true; break; } else if (type.equals("long")) { if (number.toLowerCase().endsWith("l")) { number = number.substring(0, number.length() - 1); } long value = Long.parseLong(number); bundle.putLong(key, value); found = true; break; } else if (type.equals("float")) { float value = Float.parseFloat(number); bundle.putFloat(key, value); found = true; break; } else if (type.equals("double") && !number.toLowerCase().endsWith("f")) { double value = Double.parseDouble(number); bundle.putDouble(key, value); found = true; break; } } catch (NumberFormatException e) { } } if (!found) { throw new IllegalArgumentException(); } } private  BundleHelper(); }
BundleHelper { static void putNumberParam(Bundle bundle, String key, String number) { String[] types = {"int", "long", "double", "float"}; boolean found = false; for (String type : types) { try { if (type.equals("int") && !number.toLowerCase().endsWith("l")) { int value = Integer.parseInt(number); bundle.putInt(key, value); found = true; break; } else if (type.equals("long")) { if (number.toLowerCase().endsWith("l")) { number = number.substring(0, number.length() - 1); } long value = Long.parseLong(number); bundle.putLong(key, value); found = true; break; } else if (type.equals("float")) { float value = Float.parseFloat(number); bundle.putFloat(key, value); found = true; break; } else if (type.equals("double") && !number.toLowerCase().endsWith("f")) { double value = Double.parseDouble(number); bundle.putDouble(key, value); found = true; break; } } catch (NumberFormatException e) { } } if (!found) { throw new IllegalArgumentException(); } } private  BundleHelper();  }
BundleHelper { static void putNumberParam(Bundle bundle, String key, String number) { String[] types = {"int", "long", "double", "float"}; boolean found = false; for (String type : types) { try { if (type.equals("int") && !number.toLowerCase().endsWith("l")) { int value = Integer.parseInt(number); bundle.putInt(key, value); found = true; break; } else if (type.equals("long")) { if (number.toLowerCase().endsWith("l")) { number = number.substring(0, number.length() - 1); } long value = Long.parseLong(number); bundle.putLong(key, value); found = true; break; } else if (type.equals("float")) { float value = Float.parseFloat(number); bundle.putFloat(key, value); found = true; break; } else if (type.equals("double") && !number.toLowerCase().endsWith("f")) { double value = Double.parseDouble(number); bundle.putDouble(key, value); found = true; break; } } catch (NumberFormatException e) { } } if (!found) { throw new IllegalArgumentException(); } } private  BundleHelper();   }