@Test public void testSafeSet_nonEmptyPrimitive() { BasicDto basic = new BasicDto(); SetterUtils.safeSet(basic::setString,Optional.of("succeed"),String.class); SetterUtils.safeSet(basic::setBoolean,Optional.of(true),boolean.class); SetterUtils.safeSet(basic::setByte,Optional.of(Byte.MAX_VALUE),byte.class); SetterUtils.safeSet(basic::setChar,Optional.of(Character.MAX_VALUE),char.class); SetterUtils.safeSet(basic::setShort,Optional.of(Short.MAX_VALUE),short.class); SetterUtils.safeSet(basic::setInt,Optional.of(Integer.MAX_VALUE),int.class); SetterUtils.safeSet(basic::setLong,Optional.of(Long.MAX_VALUE),long.class); SetterUtils.safeSet(basic::setfloat,Optional.of(Float.MAX_VALUE),float.class); SetterUtils.safeSet(basic::setDouble,Optional.of(Double.MAX_VALUE),double.class); assertEquals("non primitive should be set to null","succeed",basic.getString()); assertEquals("boolean non default value",true,basic.getBoolean()); assertEquals("byte non default value",Byte.MAX_VALUE,basic.getByte()); assertEquals("char non default value",Character.MAX_VALUE,basic.getChar()); assertEquals("short non default value",Short.MAX_VALUE,basic.getShort()); assertEquals("int non default value",Integer.MAX_VALUE,basic.getInt()); assertEquals("long non default value",Long.MAX_VALUE,basic.getLong()); assertEquals("float non default value",Float.MAX_VALUE,basic.getFloat(),1e-15); assertEquals("double non default value",Double.MAX_VALUE,basic.getDouble(),1e-15); }
public static <T> void safeSet(final Consumer<T> setter, final Optional<T> value) { if (value != null) { setter.accept(value.orElse(null)); } }
SetterUtils { public static <T> void safeSet(final Consumer<T> setter, final Optional<T> value) { if (value != null) { setter.accept(value.orElse(null)); } } }
SetterUtils { public static <T> void safeSet(final Consumer<T> setter, final Optional<T> value) { if (value != null) { setter.accept(value.orElse(null)); } }  }
SetterUtils { public static <T> void safeSet(final Consumer<T> setter, final Optional<T> value) { if (value != null) { setter.accept(value.orElse(null)); } }  static void safeSet(final Consumer<T> setter, final Optional<T> value); static void safeSet(final Consumer<T> setter, final Optional<T> value, final Class primitive); static void set(final Consumer<T> setter, final Optional<T> value); }
SetterUtils { public static <T> void safeSet(final Consumer<T> setter, final Optional<T> value) { if (value != null) { setter.accept(value.orElse(null)); } }  static void safeSet(final Consumer<T> setter, final Optional<T> value); static void safeSet(final Consumer<T> setter, final Optional<T> value, final Class primitive); static void set(final Consumer<T> setter, final Optional<T> value);  }