Account.java

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

  2. import static java.util.Objects.requireNonNull;

  3. import java.io.Serializable;
  4. import java.util.Objects;
  5. import java.util.Optional;

  6. /**
  7.  * An account in JaBudGet.<br>
  8.  * The Account is identified by its id.
  9.  */
  10. public class Account implements Serializable {

  11.     private static final long serialVersionUID = 1L;
  12.     private final String id;
  13.     private String name;

  14.     /**
  15.      * Create an account.
  16.      *
  17.      * @param id the id. MUST NOT be {@code null}.
  18.      */
  19.     public Account(final String id) {
  20.         requireNonNull(id, "'id' MUST NOT be null.");
  21.         this.id = id;
  22.     }

  23.     /**
  24.      * Gets the identity of the Account.
  25.      *
  26.      * @return the identity of an Account.
  27.      */
  28.     public String getId() {
  29.         return id;
  30.     }

  31.     /**
  32.      * Gets the given name of the Account.
  33.      *
  34.      * @return the given name. If no name is given returns an empty String.
  35.      */
  36.     public String getName() {
  37.         if (Objects.nonNull(name)) {
  38.             return name;
  39.         } else {
  40.             return "";
  41.         }
  42.     }

  43.     /**
  44.      * Sets the given name of the Account.
  45.      *
  46.      * @param name the new given name. SHOULD NOT BE {@code null}.
  47.      */
  48.     public void setName(final String name) {
  49.         this.name = name;
  50.     }

  51.     /**
  52.      * {@inheritDoc}
  53.      * <p>
  54.      * <strong>This implementation</strong> uses just {@link Account#id}.
  55.      */
  56.     @Override
  57.     public int hashCode() {
  58.         return id.hashCode();
  59.     }

  60.     /**
  61.      * {@inheritDoc}
  62.      * <p>
  63.      * <strong>This implementation</strong> uses just {@link Account#id}.
  64.      */
  65.     @Override
  66.     public boolean equals(final Object obj) {
  67.         if (this == obj) {
  68.             return true;
  69.         }
  70.         if (obj == null) {
  71.             return false;
  72.         }
  73.         if (getClass() != obj.getClass()) {
  74.             return false;
  75.         }
  76.         final Account other = (Account) obj;
  77.         if (!id.equals(other.id)) {
  78.             return false;
  79.         }
  80.         return true;
  81.     }

  82.     /**
  83.      * {@inheritDoc}
  84.      * <p>
  85.      * <strong>This implementation</strong> shows all fields.
  86.      */
  87.     @Override
  88.     public String toString() {
  89.         final StringBuilder builder = new StringBuilder();
  90.         builder.append("Account [id=").append(id).append(", name=").append(name).append("]");
  91.         return builder.toString();
  92.     }

  93.     /**
  94.      * Builder for {@link Account}s.
  95.      */
  96.     public static class Builder {

  97.         private String id;
  98.         private String name;

  99.         /**
  100.          * Create an empty account builder.
  101.          */
  102.         public Builder() {
  103.         }

  104.         /**
  105.          * Create an account builder with all required fields.
  106.          *
  107.          * @param id the id. MUST NOT be {@code null}.
  108.          */
  109.         public Builder(final String id) {
  110.             this.id = id;
  111.         }

  112.         /**
  113.          * Create an account builder by example;
  114.          *
  115.          * @param account the example account. MAY BE {@code null}.
  116.          */
  117.         public Builder(final Account account) {
  118.             if (account != null) {
  119.                 this.id = account.id;
  120.                 this.name = account.name;
  121.             }
  122.         }

  123.         /**
  124.          * Create an empty account builder.
  125.          *
  126.          * @return the {@link Builder}.
  127.          */
  128.         public static Builder builder() {
  129.             return new Builder();
  130.         }

  131.         /**
  132.          * Create an account builder with all required fields.
  133.          *
  134.          * @param id the id. MUST NOT be {@code null}.
  135.          * @return the {@link Builder}.
  136.          */
  137.         public static Builder builder(final String id) {
  138.             return new Builder(id);
  139.         }

  140.         /**
  141.          * Create an account builder by example;
  142.          *
  143.          * @param account the example account.
  144.          * @return the {@link Builder}.
  145.          */
  146.         public static Builder builder(final Account account) {
  147.             return new Builder(account);
  148.         }

  149.         /**
  150.          * Build the account.
  151.          *
  152.          * @return a new account instance.
  153.          */
  154.         public Account build() {
  155.             final Account result = new Account(id);
  156.             result.name = name;
  157.             return result;
  158.         }

  159.         /**
  160.          * Build the account if it would be valid.
  161.          *
  162.          * @return a new account instance or an empty optional.
  163.          */
  164.         public Optional<Account> buildOptional() {
  165.             if (id == null) {
  166.                 return Optional.empty();
  167.             }
  168.             final Account result = new Account(id);
  169.             result.name = name;
  170.             return Optional.of(result);
  171.         }

  172.         /**
  173.          * Sets the id of an Account. MUST NOT be {@code null}.
  174.          *
  175.          * @param id the id of an Account
  176.          * @return this
  177.          */
  178.         public Builder setId(final String id) {
  179.             this.id = id;
  180.             return this;
  181.         }

  182.         /**
  183.          * Sets the given name of an Account.
  184.          *
  185.          * @param name the new given name of an Account
  186.          * @return this
  187.          */
  188.         public Builder setName(final String name) {
  189.             this.name = name;
  190.             return this;
  191.         }
  192.     }
  193. }