EntryBuilder.java

  1. package de.japrost.jabudget.domain.account;

  2. import java.util.Optional;

  3. /**
  4.  * Builder for {@link Entry}.
  5.  */
  6. public class EntryBuilder {

  7.     private String accountId;
  8.     private String code;
  9.     private String subject;

  10.     /**
  11.      * Create an empty builder.
  12.      */
  13.     public EntryBuilder() {
  14.     }

  15.     /**
  16.      * Create a builder by example.
  17.      *
  18.      * @param example the example to use. MAY BE {@code null}.
  19.      */
  20.     public EntryBuilder(final Entry example) {
  21.         if (example != null) {
  22.             this.accountId = example.getAccountId();
  23.             this.code = example.getCode();
  24.             this.subject = example.getSubject();
  25.         }
  26.     }

  27.     /**
  28.      * Factory for a an empty builder.
  29.      *
  30.      * @return a new builder.
  31.      */
  32.     public static EntryBuilder builder() {
  33.         return new EntryBuilder();
  34.     }

  35.     /**
  36.      * Factory for a builder by example.
  37.      *
  38.      * @param example the example to use. MAY BE {@code null}.
  39.      * @return a builder filled with value of example.
  40.      */
  41.     public static EntryBuilder builder(Entry example) {
  42.         return new EntryBuilder(example);
  43.     }
  44.     /**
  45.      * Build the result.
  46.      *
  47.      * @return a new instance.
  48.      */
  49.     public Entry build() {
  50.         final Entry result = new Entry(accountId,code,subject);
  51.         return result;
  52.     }


  53.     /**
  54.      * Build if the result would be valid.
  55.      *
  56.      * @return a instance or an empty optional.
  57.      */
  58.     public Optional<Entry> buildOptional() {
  59.         // TODO where to put this validation?
  60.         if (accountId == null || code == null || subject == null) {
  61.             return Optional.empty();
  62.         }
  63.         final Entry result = new Entry(accountId,code,subject);
  64.         return Optional.of(result);
  65.     }

  66.     /**
  67.      * @see Entry#getAccountId()
  68.      */
  69.     public void setAccountId(String accountId) {
  70.         this.accountId = accountId;
  71.     }

  72.     /**
  73.      * @see Entry#getCode()
  74.      */
  75.     public void setCode(String code) {
  76.         this.code = code;
  77.     }

  78.     /**
  79.      * @see Entry#getSubject()
  80.      */
  81.     public void setSubject(String subject) {
  82.         this.subject = subject;
  83.     }
  84. }