Skip to content

Optional and Required Properties

geoffreywiseman edited this page Mar 1, 2012 · 1 revision

In some cases, you might prefer that Moo not require a source for every property visible in the destination. If this is generally true of how you use Moo, you can use the [Moo Configuration] to set the default value, but if you want to make a small number of properties optional, you can do that as well.

You might find this useful when translating multiple members of a class hierarchy. For instance, if you were going to have a single class that summarizes a policy owner and a contingent policy owner, and you track the email address for the owner but not the contingent owner, you might:

@Access(AccessMode.FIELD)
public class OwnerSummary {
}

public class Owner {
  private String name;
  private String phoneNumber;
  @Property(optionality=OPTIONAL)
  private String emailAddress;
}

public class PrimaryOwner extends Owner {
  private String emailAddress;
}

public class ContingentOwner extends Owner {
}

This allows you to translate the PrimaryOwner to an OwnerSummary that includes an email address, and translate the Contingent Owner to an OwnerSummary with no email address.

Judicious use of static imports makes the optionality attribute a little less ugly.