@Test public void testSafeSet_emptyPrimitive() { BasicDto basic = new BasicDto(); basic.setString("basic"); basic.setBoolean(true); basic.setByte(Byte.MAX_VALUE); basic.setChar(Character.MAX_VALUE); basic.setShort(Short.MAX_VALUE); basic.setInt(Integer.MAX_VALUE); basic.setLong(Long.MAX_VALUE); basic.setfloat(Float.MAX_VALUE); basic.setDouble(Double.MAX_VALUE); SetterUtils.safeSet(basic::setString,Optional.empty(),String.class); SetterUtils.safeSet(basic::setBoolean,Optional.empty(),boolean.class); SetterUtils.safeSet(basic::setByte,Optional.empty(),byte.class); SetterUtils.safeSet(basic::setChar,Optional.empty(),char.class); SetterUtils.safeSet(basic::setShort,Optional.empty(),short.class); SetterUtils.safeSet(basic::setInt,Optional.empty(),int.class); SetterUtils.safeSet(basic::setLong,Optional.empty(),long.class); SetterUtils.safeSet(basic::setfloat,Optional.empty(),float.class); SetterUtils.safeSet(basic::setDouble,Optional.empty(),double.class); BasicDto defaultValue = new BasicDto(); assertEquals("non primitive should be set to null",defaultValue.getString(),basic.getString()); assertEquals("boolean non default value",defaultValue.getBoolean(),basic.getBoolean()); assertEquals("byte non default value",defaultValue.getByte(),basic.getByte()); assertEquals("char non default value",defaultValue.getChar(),basic.getChar()); assertEquals("short non default value",defaultValue.getShort(),basic.getShort()); assertEquals("int non default value",defaultValue.getInt(),basic.getInt()); assertEquals("long non default value",defaultValue.getLong(),basic.getLong()); assertEquals("float non default value",defaultValue.getFloat(),basic.getFloat(),1e-15); assertEquals("double non default value",defaultValue.getDouble(),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);  }