I'm using the fusionauth java client in my application to integrate with fusionauth.
When user is registering I want to set him the preferredLanguages in order to send him a verification email in the desired language. The User object has the field preferredLanguages
marked as final
and there is no possibility to set in the constructors. How can I do that ?
All FusionAuth domain objects are open (i.e. not immutable). You can use the Builder pattern with the Buildable
interface in FusionAuth's Java client to set values easily.
Here's how some code might look:
User u = new User().with(u -> u.preferredLanguages.add(Locale.English))
.with(u -> u.firstName = "Bob")
...
Since preferredLanguages
is a List<Locale>
, you can also use addAll
and any other List
methods.
While this method doesn't ensure that the User
object is completely filled out immediately after the constructor returns, Java will ensure that the variable User u
from my example is fully filled out because the with
methods all need to return before the left-side assignment is made.