It may change in the future, but I think you can try take a look at the overloaded builder methods generated for optional members:
For an optional attribute named opt
, where elements are of type T
opt(T)
sets present value for T
opt(Optional<T>)
specifies present or absent
A Example
SomeValue.java
@Value.Immutable
public abstract class SomeValue {
public abstract int foo();
public abstract Optional<Object> obj();
public abstract com.google.common.base.Optional<Double> d();
}
TestImmutables.java
public class TestImmutables {
public static void main(String [] args) {
SomeValue someValue = ImmutableSomeValue.builder().foo(2).build();
for (Method method : someValue.getClass().getDeclaredMethods()) {
System.out.print(method.getName() + "(");
for (Type t : method.getGenericParameterTypes()) {
System.out.print(t);
System.out.print(",");
}
System.out.println(")");
}
}
}
Output
Each line is a method, pay attention to the methods generated for optional members.
builder()
equalTo(class immutables.ImmutableSomeValue,)
withObj(class java.lang.Object,) < obj
withObj(java.util.Optional<java.lang.Object>,) < obj
withD(com.google.common.base.Optional<java.lang.Double>,) < d
withD(double,) < d
d()
withFoo(int,)
foo()
obj()
equals(class java.lang.Object,)
toString()
hashCode()
copyOf(class immutables.SomeValue,)
Basically you are looking for two overloads, each with exactly one parameter, and if parameter type of one is T
, the parameter type of the other is SomeOptional<T>
.