use of org.bson.types.Decimal128 in project realm-java by realm.
the class BulkInsertTests method insertOrUpdate_nullTypes.
@Test
public void insertOrUpdate_nullTypes() {
NullTypes nullTypes1 = new NullTypes();
nullTypes1.setId(1);
nullTypes1.setFieldIntegerNull(1);
nullTypes1.setFieldFloatNull(2F);
nullTypes1.setFieldDoubleNull(3D);
nullTypes1.setFieldBooleanNull(true);
nullTypes1.setFieldStringNull("4");
nullTypes1.setFieldDateNull(new Date(12345));
nullTypes1.setFieldDecimal128Null(new Decimal128(BigDecimal.TEN));
nullTypes1.setFieldObjectIdNull(new ObjectId(TestHelper.generateObjectIdHexString(7)));
nullTypes1.setFieldUUIDNull(UUID.fromString(TestHelper.generateUUIDString(7)));
realm.beginTransaction();
realm.insert(nullTypes1);
realm.commitTransaction();
NullTypes first = realm.where(NullTypes.class).findFirst();
assertNotNull(first);
assertEquals(nullTypes1.getId(), first.getId());
assertEquals(nullTypes1.getFieldIntegerNull(), first.getFieldIntegerNull());
assertEquals(nullTypes1.getFieldFloatNull(), first.getFieldFloatNull());
assertEquals(nullTypes1.getFieldDoubleNull(), first.getFieldDoubleNull());
assertEquals(nullTypes1.getFieldBooleanNull(), first.getFieldBooleanNull());
assertEquals(nullTypes1.getFieldStringNull(), first.getFieldStringNull());
assertEquals(nullTypes1.getFieldDateNull(), first.getFieldDateNull());
assertEquals(nullTypes1.getFieldDecimal128Null(), first.getFieldDecimal128Null());
assertEquals(nullTypes1.getFieldObjectIdNull(), first.getFieldObjectIdNull());
assertEquals(nullTypes1.getFieldUUIDNull(), first.getFieldUUIDNull());
NullTypes nullTypes2 = new NullTypes();
nullTypes2.setId(2);
NullTypes nullTypes3 = new NullTypes();
nullTypes3.setId(3);
nullTypes1 = new NullTypes();
nullTypes1.setId(1);
nullTypes1.setFieldIntegerNull(null);
nullTypes1.setFieldFloatNull(null);
nullTypes1.setFieldDoubleNull(null);
nullTypes1.setFieldBooleanNull(null);
nullTypes1.setFieldStringNull(null);
nullTypes1.setFieldDateNull(null);
nullTypes1.setFieldDecimal128Null(null);
nullTypes1.setFieldObjectIdNull(null);
nullTypes1.setFieldUUIDNull(null);
nullTypes1.setFieldListNull(new RealmList<NullTypes>());
nullTypes1.getFieldListNull().add(nullTypes2);
nullTypes1.getFieldListNull().add(nullTypes3);
OrderedRealmCollection<NullTypes> collection = new RealmList<NullTypes>();
collection.add(nullTypes2);
collection.add(nullTypes1);
collection.add(nullTypes3);
realm.beginTransaction();
realm.insertOrUpdate(collection);
realm.commitTransaction();
first = realm.where(NullTypes.class).equalTo("id", 1).findFirst();
assertNotNull(first);
assertEquals(nullTypes1.getId(), first.getId());
assertNull(first.getFieldIntegerNull());
assertNull(first.getFieldFloatNull());
assertNull(first.getFieldDoubleNull());
assertNull(first.getFieldBooleanNull());
assertNull(first.getFieldStringNull());
assertNull(first.getFieldDateNull());
assertNull(first.getFieldDecimal128Null());
assertNull(first.getFieldObjectIdNull());
assertNull(first.getFieldUUIDNull());
assertEquals(2, first.getFieldListNull().size());
assertEquals(2, first.getFieldListNull().get(0).getId());
assertEquals(3, first.getFieldListNull().get(1).getId());
}
use of org.bson.types.Decimal128 in project realm-java by realm.
the class DynamicRealmObjectTests method typedGettersAndSetters.
// Tests all typed setters/setters.
@Test
public void typedGettersAndSetters() {
realm.beginTransaction();
AllJavaTypes obj = realm.createObject(AllJavaTypes.class, 0);
DynamicRealmObject dObj = new DynamicRealmObject(obj);
try {
for (SupportedType type : SupportedType.values()) {
switch(type) {
case BOOLEAN:
dObj.setBoolean(AllJavaTypes.FIELD_BOOLEAN, true);
assertTrue(dObj.getBoolean(AllJavaTypes.FIELD_BOOLEAN));
break;
case SHORT:
dObj.setShort(AllJavaTypes.FIELD_SHORT, (short) 42);
assertEquals(42, dObj.getShort(AllJavaTypes.FIELD_SHORT));
break;
case INT:
dObj.setInt(AllJavaTypes.FIELD_INT, 42);
assertEquals(42, dObj.getInt(AllJavaTypes.FIELD_INT));
break;
case LONG:
dObj.setLong(AllJavaTypes.FIELD_LONG, 42L);
assertEquals(42, dObj.getLong(AllJavaTypes.FIELD_LONG));
break;
case BYTE:
dObj.setByte(AllJavaTypes.FIELD_BYTE, (byte) 4);
assertEquals(4, dObj.getByte(AllJavaTypes.FIELD_BYTE));
break;
case FLOAT:
dObj.setFloat(AllJavaTypes.FIELD_FLOAT, 1.23f);
assertEquals(1.23f, dObj.getFloat(AllJavaTypes.FIELD_FLOAT), 0f);
break;
case DOUBLE:
dObj.setDouble(AllJavaTypes.FIELD_DOUBLE, 1.234d);
assertEquals(1.234d, dObj.getDouble(AllJavaTypes.FIELD_DOUBLE), 0d);
break;
case STRING:
dObj.setString(AllJavaTypes.FIELD_STRING, "str");
assertEquals("str", dObj.getString(AllJavaTypes.FIELD_STRING));
break;
case BINARY:
dObj.setBlob(AllJavaTypes.FIELD_BINARY, new byte[] { 1, 2, 3 });
assertArrayEquals(new byte[] { 1, 2, 3 }, dObj.getBlob(AllJavaTypes.FIELD_BINARY));
break;
case DATE:
dObj.setDate(AllJavaTypes.FIELD_DATE, new Date(1000));
assertEquals(new Date(1000), dObj.getDate(AllJavaTypes.FIELD_DATE));
break;
case DECIMAL128:
dObj.setDecimal128(AllJavaTypes.FIELD_DECIMAL128, new Decimal128(BigDecimal.ONE));
assertEquals(new Decimal128(BigDecimal.ONE), dObj.getDecimal128(AllJavaTypes.FIELD_DECIMAL128));
break;
case OBJECT_ID:
dObj.setObjectId(AllJavaTypes.FIELD_OBJECT_ID, new ObjectId(TestHelper.generateObjectIdHexString(0)));
assertEquals(new ObjectId(TestHelper.generateObjectIdHexString(0)), dObj.getObjectId(AllJavaTypes.FIELD_OBJECT_ID));
break;
case UUID:
String uuid = UUID.randomUUID().toString();
dObj.setUUID(AllJavaTypes.FIELD_UUID, UUID.fromString(uuid));
assertEquals(UUID.fromString(uuid), dObj.getUUID(AllJavaTypes.FIELD_UUID));
break;
case OBJECT:
dObj.setObject(AllJavaTypes.FIELD_OBJECT, dObj);
assertEquals(dObj, dObj.getObject(AllJavaTypes.FIELD_OBJECT));
break;
case LIST_INTEGER:
checkSetGetValueList(dObj, AllJavaTypes.FIELD_INTEGER_LIST, Integer.class, new RealmList<>(null, 1));
break;
case LIST_STRING:
checkSetGetValueList(dObj, AllJavaTypes.FIELD_STRING_LIST, String.class, new RealmList<>(null, "foo"));
break;
case LIST_BOOLEAN:
checkSetGetValueList(dObj, AllJavaTypes.FIELD_BOOLEAN_LIST, Boolean.class, new RealmList<>(null, true));
break;
case LIST_FLOAT:
checkSetGetValueList(dObj, AllJavaTypes.FIELD_FLOAT_LIST, Float.class, new RealmList<>(null, 1.23F));
break;
case LIST_DOUBLE:
checkSetGetValueList(dObj, AllJavaTypes.FIELD_DOUBLE_LIST, Double.class, new RealmList<>(null, 1.234D));
break;
case LIST_BINARY:
checkSetGetValueList(dObj, AllJavaTypes.FIELD_BINARY_LIST, byte[].class, new RealmList<>(null, new byte[] { 1, 2, 3 }));
break;
case LIST_DATE:
checkSetGetValueList(dObj, AllJavaTypes.FIELD_DATE_LIST, Date.class, new RealmList<>(null, new Date(1000)));
break;
case LIST_DECIMAL128:
checkSetGetValueList(dObj, AllJavaTypes.FIELD_DECIMAL128_LIST, Decimal128.class, new RealmList<>(null, new Decimal128(BigDecimal.ONE)));
break;
case LIST_OBJECT_ID:
checkSetGetValueList(dObj, AllJavaTypes.FIELD_OBJECT_ID_LIST, ObjectId.class, new RealmList<>(null, new ObjectId(TestHelper.generateObjectIdHexString(0))));
break;
case LIST_UUID:
checkSetGetValueList(dObj, AllJavaTypes.FIELD_UUID_LIST, UUID.class, new RealmList<>(null, UUID.randomUUID()));
break;
case LIST:
// Ignores. See testGetList/testSetList.
break;
default:
fail();
}
}
} finally {
realm.cancelTransaction();
}
}
use of org.bson.types.Decimal128 in project realm-java by realm.
the class NativeRealmAnyCollection method newDecimal128Collection.
public static NativeRealmAnyCollection newDecimal128Collection(Collection<? extends Decimal128> collection) {
long[] lowValues = new long[collection.size()];
long[] highValues = new long[collection.size()];
boolean[] notNull = new boolean[collection.size()];
int i = 0;
for (Decimal128 decimal128 : collection) {
if (decimal128 != null) {
lowValues[i] = decimal128.getLow();
highValues[i] = decimal128.getHigh();
notNull[i] = true;
}
i++;
}
return new NativeRealmAnyCollection(nativeCreateDecimal128Collection(lowValues, highValues, notNull));
}
use of org.bson.types.Decimal128 in project realm-java by realm.
the class ProxyUtils method createRealmListWithJsonStream.
/**
* Called by proxy to create an unmanaged {@link RealmList} according to the given {@link JsonReader}.
*
* @param elementClass the type of the {@link RealmList}.
* @param jsonReader the JSON stream to be parsed which may contain the data of the list to be set.
* @param <E> type of the {@link RealmList}.
* @throws IOException if it fails to parse JSON stream.
*/
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
static <E> RealmList<E> createRealmListWithJsonStream(Class<E> elementClass, JsonReader jsonReader) throws IOException {
if (jsonReader.peek() == null) {
jsonReader.skipValue();
return null;
}
jsonReader.beginArray();
RealmList realmList = new RealmList<E>();
if (elementClass == Boolean.class) {
while (jsonReader.hasNext()) {
if (jsonReader.peek() == JsonToken.NULL) {
jsonReader.skipValue();
realmList.add(null);
} else {
realmList.add(jsonReader.nextBoolean());
}
}
} else if (elementClass == Float.class) {
while (jsonReader.hasNext()) {
if (jsonReader.peek() == JsonToken.NULL) {
jsonReader.skipValue();
realmList.add(null);
} else {
realmList.add((float) jsonReader.nextDouble());
}
}
} else if (elementClass == Double.class) {
while (jsonReader.hasNext()) {
if (jsonReader.peek() == JsonToken.NULL) {
jsonReader.skipValue();
realmList.add(null);
} else {
realmList.add(jsonReader.nextDouble());
}
}
} else if (elementClass == String.class) {
while (jsonReader.hasNext()) {
if (jsonReader.peek() == JsonToken.NULL) {
jsonReader.skipValue();
realmList.add(null);
} else {
realmList.add(jsonReader.nextString());
}
}
} else if (elementClass == byte[].class) {
while (jsonReader.hasNext()) {
if (jsonReader.peek() == JsonToken.NULL) {
jsonReader.skipValue();
realmList.add(null);
} else {
realmList.add(JsonUtils.stringToBytes(jsonReader.nextString()));
}
}
} else if (elementClass == Date.class) {
while (jsonReader.hasNext()) {
JsonToken token = jsonReader.peek();
if (token == JsonToken.NULL) {
jsonReader.skipValue();
realmList.add(null);
} else if (token == JsonToken.NUMBER) {
realmList.add(new Date(jsonReader.nextLong()));
} else {
realmList.add(JsonUtils.stringToDate(jsonReader.nextString()));
}
}
} else if (elementClass == Long.class) {
while (jsonReader.hasNext()) {
if (jsonReader.peek() == JsonToken.NULL) {
jsonReader.skipValue();
realmList.add(null);
} else {
realmList.add(jsonReader.nextLong());
}
}
} else if (elementClass == Integer.class) {
while (jsonReader.hasNext()) {
if (jsonReader.peek() == JsonToken.NULL) {
jsonReader.skipValue();
realmList.add(null);
} else {
realmList.add((int) jsonReader.nextLong());
}
}
} else if (elementClass == Short.class) {
while (jsonReader.hasNext()) {
if (jsonReader.peek() == JsonToken.NULL) {
jsonReader.skipValue();
realmList.add(null);
} else {
realmList.add((short) jsonReader.nextLong());
}
}
} else if (elementClass == Byte.class) {
while (jsonReader.hasNext()) {
if (jsonReader.peek() == JsonToken.NULL) {
jsonReader.skipValue();
realmList.add(null);
} else {
realmList.add((byte) jsonReader.nextLong());
}
}
} else if (elementClass == ObjectId.class) {
while (jsonReader.hasNext()) {
if (jsonReader.peek() == JsonToken.NULL) {
jsonReader.skipValue();
realmList.add(null);
} else {
realmList.add(new ObjectId(jsonReader.nextString()));
}
}
} else if (elementClass == Decimal128.class) {
while (jsonReader.hasNext()) {
if (jsonReader.peek() == JsonToken.NULL) {
jsonReader.skipValue();
realmList.add(null);
} else {
realmList.add(org.bson.types.Decimal128.parse(jsonReader.nextString()));
}
}
} else if (elementClass == UUID.class) {
while (jsonReader.hasNext()) {
if (jsonReader.peek() == JsonToken.NULL) {
jsonReader.skipValue();
realmList.add(null);
} else {
realmList.add(java.util.UUID.fromString(jsonReader.nextString()));
}
}
} else if (elementClass == RealmAny.class) {
while (jsonReader.hasNext()) {
if (jsonReader.peek() == JsonToken.NULL) {
jsonReader.skipValue();
realmList.add(RealmAny.nullValue());
} else if (jsonReader.peek() == JsonToken.STRING) {
realmList.add(RealmAny.valueOf(jsonReader.nextString()));
} else if (jsonReader.peek() == JsonToken.NUMBER) {
String value = jsonReader.nextString();
if (value.contains(".")) {
realmList.add(RealmAny.valueOf(Double.parseDouble(value)));
} else {
realmList.add(RealmAny.valueOf(Long.parseLong(value)));
}
} else if (jsonReader.peek() == JsonToken.BOOLEAN) {
realmList.add(RealmAny.valueOf(jsonReader.nextBoolean()));
}
}
} else if (elementClass == ObjectId.class) {
while (jsonReader.hasNext()) {
if (jsonReader.peek() == JsonToken.NULL) {
jsonReader.skipValue();
realmList.add(null);
} else {
realmList.add(new ObjectId(jsonReader.nextString()));
}
}
} else if (elementClass == Decimal128.class) {
while (jsonReader.hasNext()) {
if (jsonReader.peek() == JsonToken.NULL) {
jsonReader.skipValue();
realmList.add(null);
} else {
realmList.add(org.bson.types.Decimal128.parse(jsonReader.nextString()));
}
}
} else {
throwWrongElementType(elementClass);
}
jsonReader.endArray();
return realmList;
}
use of org.bson.types.Decimal128 in project realm-java by realm.
the class ProxyUtils method setRealmListWithJsonObject.
/**
* Called by proxy to set the managed {@link RealmList} according to the given {@link JSONObject}.
*
* @param realmList the managed {@link RealmList}.
* @param jsonObject the {@link JSONObject} which may contain the data of the list to be set.
* @param fieldName the field name of the {@link RealmList}.
* @param <E> type of the {@link RealmList}.
* @throws JSONException if it fails to parse JSON.
*/
static <E> void setRealmListWithJsonObject(Realm realm, RealmList<E> realmList, JSONObject jsonObject, String fieldName, boolean update) throws JSONException {
if (!jsonObject.has(fieldName)) {
return;
}
OsList osList = realmList.getOsList();
if (jsonObject.isNull(fieldName)) {
osList.removeAll();
return;
}
JSONArray jsonArray = jsonObject.getJSONArray(fieldName);
osList.removeAll();
int arraySize = jsonArray.length();
if (realmList.clazz == Boolean.class) {
for (int i = 0; i < arraySize; i++) {
if (jsonArray.isNull(i)) {
osList.addNull();
} else {
osList.addBoolean(jsonArray.getBoolean(i));
}
}
} else if (realmList.clazz == Float.class) {
for (int i = 0; i < arraySize; i++) {
if (jsonArray.isNull(i)) {
osList.addNull();
} else {
osList.addFloat((float) jsonArray.getDouble(i));
}
}
} else if (realmList.clazz == Double.class) {
for (int i = 0; i < arraySize; i++) {
if (jsonArray.isNull(i)) {
osList.addNull();
} else {
osList.addDouble(jsonArray.getDouble(i));
}
}
} else if (realmList.clazz == String.class) {
for (int i = 0; i < arraySize; i++) {
if (jsonArray.isNull(i)) {
osList.addNull();
} else {
osList.addString(jsonArray.getString(i));
}
}
} else if (realmList.clazz == byte[].class) {
for (int i = 0; i < arraySize; i++) {
if (jsonArray.isNull(i)) {
osList.addNull();
} else {
osList.addBinary(JsonUtils.stringToBytes(jsonArray.getString(i)));
}
}
} else if (realmList.clazz == Date.class) {
for (int i = 0; i < arraySize; i++) {
if (jsonArray.isNull(i)) {
osList.addNull();
continue;
}
Object timestamp = jsonArray.get(i);
if (timestamp instanceof String) {
osList.addDate(JsonUtils.stringToDate((String) timestamp));
} else {
osList.addDate(new Date(jsonArray.getLong(i)));
}
}
} else if (realmList.clazz == ObjectId.class) {
for (int i = 0; i < arraySize; i++) {
if (jsonArray.isNull(i)) {
osList.addNull();
continue;
}
Object id = jsonArray.get(i);
if (id instanceof String) {
osList.addObjectId(new ObjectId((String) id));
} else {
osList.addObjectId((ObjectId) id);
}
}
} else if (realmList.clazz == Decimal128.class) {
for (int i = 0; i < arraySize; i++) {
if (jsonArray.isNull(i)) {
osList.addNull();
continue;
}
Object decimal = jsonArray.get(i);
if (decimal instanceof org.bson.types.Decimal128) {
osList.addDecimal128((org.bson.types.Decimal128) decimal);
} else if (decimal instanceof String) {
osList.addDecimal128(org.bson.types.Decimal128.parse((String) decimal));
} else if (decimal instanceof Integer) {
osList.addDecimal128(new org.bson.types.Decimal128((Integer) (decimal)));
} else if (decimal instanceof Long) {
osList.addDecimal128(new org.bson.types.Decimal128((Long) (decimal)));
} else if (decimal instanceof Double) {
osList.addDecimal128(new org.bson.types.Decimal128(new java.math.BigDecimal((Double) (decimal))));
} else {
osList.addDecimal128((Decimal128) decimal);
}
}
} else if (realmList.clazz == UUID.class) {
for (int i = 0; i < arraySize; i++) {
if (jsonArray.isNull(i)) {
osList.addNull();
continue;
}
Object uuid = jsonArray.get(i);
if (uuid instanceof java.util.UUID) {
osList.addUUID((java.util.UUID) uuid);
} else {
osList.addUUID(java.util.UUID.fromString((String) uuid));
}
}
} else if (realmList.clazz == RealmAny.class) {
for (int i = 0; i < arraySize; i++) {
if (jsonArray.isNull(i)) {
osList.addNull();
continue;
}
Object value = jsonArray.get(i);
RealmAny realmAny;
if (value instanceof String) {
realmAny = RealmAny.valueOf((String) value);
} else if (value instanceof Integer) {
realmAny = RealmAny.valueOf((Integer) value);
} else if (value instanceof Long) {
realmAny = RealmAny.valueOf((Long) value);
} else if (value instanceof Double) {
realmAny = RealmAny.valueOf((Double) value);
} else if (value instanceof Boolean) {
realmAny = RealmAny.valueOf((Boolean) value);
} else if (value instanceof RealmAny) {
realmAny = (io.realm.RealmAny) value;
realmAny = ProxyUtils.copyOrUpdate(realmAny, realm, update, new HashMap<>(), new HashSet<>());
} else {
throw new IllegalArgumentException(String.format("Unsupported JSON type: %s", value.getClass().getSimpleName()));
}
osList.addRealmAny(realmAny.getNativePtr());
}
} else if (realmList.clazz == Long.class || realmList.clazz == Integer.class || realmList.clazz == Short.class || realmList.clazz == Byte.class) {
for (int i = 0; i < arraySize; i++) {
if (jsonArray.isNull(i)) {
osList.addNull();
} else {
osList.addLong(jsonArray.getLong(i));
}
}
} else {
throwWrongElementType(realmList.clazz);
}
}
Aggregations