Last active
December 17, 2015 21:39
-
-
Save Krylez-zz/5675855 to your computer and use it in GitHub Desktop.
ContentValuesBuilder.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class ContentValuesBuilder { | |
| private ContentValues mValues; | |
| private ContentValuesBuilder() { | |
| mValues = new ContentValues(); | |
| } | |
| public static ContentValuesBuilder newBuilder() { | |
| return new ContentValuesBuilder(); | |
| } | |
| public ContentValuesBuilder withValue(String key, Object value) { | |
| if (mValues == null) { | |
| mValues = new ContentValues(); | |
| } | |
| if (value == null) { | |
| mValues.putNull(key); | |
| } else if (value instanceof String) { | |
| mValues.put(key, (String) value); | |
| } else if (value instanceof Byte) { | |
| mValues.put(key, (Byte) value); | |
| } else if (value instanceof Short) { | |
| mValues.put(key, (Short) value); | |
| } else if (value instanceof Integer) { | |
| mValues.put(key, (Integer) value); | |
| } else if (value instanceof Long) { | |
| mValues.put(key, (Long) value); | |
| } else if (value instanceof Float) { | |
| mValues.put(key, (Float) value); | |
| } else if (value instanceof Double) { | |
| mValues.put(key, (Double) value); | |
| } else if (value instanceof Boolean) { | |
| mValues.put(key, (Boolean) value); | |
| } else if (value instanceof byte[]) { | |
| mValues.put(key, (byte[]) value); | |
| } else { | |
| throw new IllegalArgumentException("bad value type: " + value.getClass().getName()); | |
| } | |
| return this; | |
| } | |
| public ContentValues build() { | |
| return mValues; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| final ContentValuesBuilder builder = ContentValuesBuilder.newBuilder() | |
| .withValue("date", System.currentTimeMillis()) | |
| .withValue("name", "Henry") | |
| .withValue("failures", 17) | |
| .withValue("giving-up", false); | |
| ContentValues values = builder.build(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment