Search in sources :

Example 1 with OptionalDouble

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());
}
Also used : Optional(java.util.Optional) OptionalLong(java.util.OptionalLong) JSONException(com.alibaba.fastjson.JSONException) OptionalInt(java.util.OptionalInt) OptionalDouble(java.util.OptionalDouble)

Example 2 with OptionalDouble

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()));
}
Also used : OptionalDouble(java.util.OptionalDouble)

Example 3 with OptionalDouble

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));
}
Also used : OptionalDouble(java.util.OptionalDouble) Test(org.testng.annotations.Test)

Example 4 with OptionalDouble

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);
}
Also used : OptionalDouble(java.util.OptionalDouble) Test(org.testng.annotations.Test)

Example 5 with OptionalDouble

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));
}
Also used : OptionalDouble(java.util.OptionalDouble) Test(org.testng.annotations.Test)

Aggregations

OptionalDouble (java.util.OptionalDouble)10 Test (org.testng.annotations.Test)6 JSONException (com.alibaba.fastjson.JSONException)1 Format (java.text.Format)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Objects (java.util.Objects)1 Optional (java.util.Optional)1 OptionalInt (java.util.OptionalInt)1 OptionalLong (java.util.OptionalLong)1 Set (java.util.Set)1 ArrayBlockingQueue (java.util.concurrent.ArrayBlockingQueue)1 ThreadPoolExecutor (java.util.concurrent.ThreadPoolExecutor)1 TimeUnit (java.util.concurrent.TimeUnit)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 Supplier (java.util.function.Supplier)1 Level (java.util.logging.Level)1 Logger (java.util.logging.Logger)1 Preferences (java.util.prefs.Preferences)1