use of java.util.OptionalDouble in project fastjson by alibaba.
the class OptionalCodec method write.
public void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType, int features) throws IOException {
if (object == null) {
serializer.writeNull();
return;
}
if (object instanceof Optional) {
Optional<?> optional = (Optional<?>) object;
Object value = optional.isPresent() ? optional.get() : null;
serializer.write(value);
return;
}
if (object instanceof OptionalDouble) {
OptionalDouble optional = (OptionalDouble) object;
if (optional.isPresent()) {
double value = optional.getAsDouble();
serializer.write(value);
} else {
serializer.writeNull();
}
return;
}
if (object instanceof OptionalInt) {
OptionalInt optional = (OptionalInt) object;
if (optional.isPresent()) {
int value = optional.getAsInt();
serializer.out.writeInt(value);
} else {
serializer.writeNull();
}
return;
}
if (object instanceof OptionalLong) {
OptionalLong optional = (OptionalLong) object;
if (optional.isPresent()) {
long value = optional.getAsLong();
serializer.out.writeLong(value);
} else {
serializer.writeNull();
}
return;
}
throw new JSONException("not support optional : " + object.getClass());
}
use of java.util.OptionalDouble in project fastjson by alibaba.
the class OptionalTest method test_optionalDouble.
public void test_optionalDouble() throws Exception {
OptionalDouble val = OptionalDouble.of(3.1D);
String text = JSON.toJSONString(val);
Assert.assertEquals("3.1", text);
OptionalDouble val2 = JSON.parseObject(text, OptionalDouble.class);
Assert.assertEquals(Double.toString(val.getAsDouble()), Double.toString(val2.getAsDouble()));
}
use of java.util.OptionalDouble in project jdk8u_jdk by JetBrains.
the class BasicDouble method testEmpty.
@Test(groups = "unit")
public void testEmpty() {
OptionalDouble empty = OptionalDouble.empty();
OptionalDouble present = OptionalDouble.of(1.0);
// empty
assertTrue(empty.equals(empty));
assertTrue(empty.equals(OptionalDouble.empty()));
assertTrue(!empty.equals(present));
assertTrue(0 == empty.hashCode());
assertTrue(!empty.toString().isEmpty());
assertTrue(!empty.isPresent());
empty.ifPresent(v -> {
fail();
});
assertEquals(2.0, empty.orElse(2.0));
assertEquals(2.0, empty.orElseGet(() -> 2.0));
}
use of java.util.OptionalDouble in project jdk8u_jdk by JetBrains.
the class BasicDouble method testEmptyOrElseThrowNull.
@Test(expectedExceptions = NullPointerException.class)
public void testEmptyOrElseThrowNull() throws Throwable {
OptionalDouble empty = OptionalDouble.empty();
double got = empty.orElseThrow(null);
}
use of java.util.OptionalDouble in project jdk8u_jdk by JetBrains.
the class BasicDouble method testPresent.
@Test(groups = "unit")
public void testPresent() {
OptionalDouble empty = OptionalDouble.empty();
OptionalDouble present = OptionalDouble.of(1.0);
// present
assertTrue(present.equals(present));
assertFalse(present.equals(OptionalDouble.of(0.0)));
assertTrue(present.equals(OptionalDouble.of(1.0)));
assertTrue(!present.equals(empty));
assertTrue(Double.hashCode(1.0) == present.hashCode());
assertFalse(present.toString().isEmpty());
assertTrue(-1 != present.toString().indexOf(Double.toString(present.getAsDouble()).toString()));
assertEquals(1.0, present.getAsDouble());
try {
present.ifPresent(v -> {
throw new ObscureException();
});
fail();
} catch (ObscureException expected) {
}
assertEquals(1.0, present.orElse(2.0));
assertEquals(1.0, present.orElseGet(null));
assertEquals(1.0, present.orElseGet(() -> 2.0));
assertEquals(1.0, present.orElseGet(() -> 3.0));
assertEquals(1.0, present.<RuntimeException>orElseThrow(null));
assertEquals(1.0, present.<RuntimeException>orElseThrow(ObscureException::new));
}
Aggregations