Skip to content

Instantly share code, notes, and snippets.

@Krylez-zz
Last active December 17, 2015 21:39
Show Gist options
  • Select an option

  • Save Krylez-zz/5675855 to your computer and use it in GitHub Desktop.

Select an option

Save Krylez-zz/5675855 to your computer and use it in GitHub Desktop.
ContentValuesBuilder.java
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;
}
}
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