Advertisement
Guest User

Untitled

a guest
Mar 31st, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.66 KB | None | 0 0
  1. package model.account;
  2.  
  3. import java.util.Optional;
  4.  
  5. /**
  6.  * Implementation of Account Interface.
  7.  *
  8.  */
  9. public final class AccountImpl implements Account {
  10.  
  11.     private final String username;
  12.     private String password;
  13.     private String nickname;
  14.     private int bestScore;
  15.     private Settings settings;
  16.  
  17.     private AccountImpl(final String username, final String nickname, final String password, final int bestScore, final Settings settings) {
  18.         this.username = username;
  19.         this.nickname = nickname;
  20.         this.password = password;
  21.         this.bestScore = bestScore;
  22.         this.settings = settings;
  23.     }
  24.  
  25.     /**
  26.      * Builder class for Account.
  27.      *
  28.      */
  29.     public static class Builder {
  30.  
  31.         private final String username;
  32.         private final String password;
  33.         private Optional<String> nick = Optional.empty();
  34.         private int score;
  35.         private Optional<Settings> setting = Optional.empty();
  36.  
  37.         /**
  38.          * Build the initial Builder.
  39.          * @param username the necessary username
  40.          * @param password the necessary password
  41.          */
  42.         public Builder(final String username, final String password) {
  43.             if (username == null || password == null) {
  44.                 throw new IllegalArgumentException();
  45.             }
  46.             this.username = username;
  47.             this.password = password;
  48.         }
  49.  
  50.         /**
  51.          * Set the nickname.
  52.          * @param nickname the nickname to set
  53.          * @return the Builder
  54.          */
  55.         public Builder nickname(final String nickname) {
  56.             this.nick = Optional.ofNullable(nickname);
  57.             return this;
  58.         }
  59.  
  60.         /**
  61.          * Set the bestScore.
  62.          * @param bestScore the bestScore to set
  63.          * @return the Builder
  64.          */
  65.         public Builder bestScore(final int bestScore) {
  66.             this.score = bestScore;
  67.             return this;
  68.         }
  69.  
  70.         /**
  71.          * Set the settings.
  72.          * @param settings the settings to set
  73.          * @return the Builder
  74.          */
  75.         public Builder settings(final Settings settings) {
  76.             this.setting = Optional.ofNullable(settings);
  77.             return this;
  78.         }
  79.  
  80.         /**
  81.          * Build the Account.
  82.          * @return the account
  83.          */
  84.         public Account build() {
  85.             if (this.score < 0) {
  86.                 throw new IllegalStateException();
  87.             }
  88.             if (this.nick.isPresent()) {
  89.                 if (!this.setting.isPresent()) {
  90.                     return new AccountImpl(this.username, this.nick.get(), this.password, this.score, Settings.DEFAULT);
  91.                 } else {
  92.                     return new AccountImpl(this.username, this.nick.get(), this.password, this.score, this.setting.get());
  93.                 }
  94.             } else {
  95.                 if (!this.setting.isPresent()) {
  96.                     return new AccountImpl(this.username, this.username, this.password, this.score, Settings.DEFAULT);
  97.                 } else {
  98.                     return new AccountImpl(this.username, this.username, this.password, this.score, this.setting.get());
  99.                 }
  100.             }
  101.         }
  102.     }
  103.  
  104.     /**
  105.      * {@inheritDoc}
  106.      */
  107.     public String getUsername() {
  108.         return this.username;
  109.     }
  110.  
  111.     /**
  112.      * {@inheritDoc}
  113.      */
  114.     public String getNickname() {
  115.         return this.nickname;
  116.     }
  117.  
  118.     /**
  119.      * {@inheritDoc}
  120.      */
  121.     public String getPassword() {
  122.         return this.password;
  123.     }
  124.  
  125.     /**
  126.      * {@inheritDoc}
  127.      */
  128.     public int getBestScore() {
  129.         return this.bestScore;
  130.     }
  131.  
  132.     /**
  133.      * {@inheritDoc}
  134.      */
  135.     public Settings getSettings() {
  136.         return settings;
  137.     }
  138.  
  139.     /**
  140.      * {@inheritDoc}
  141.      */
  142.     public void setNickname(final String nickname) {
  143.         if (nickname == null) {
  144.             throw new IllegalArgumentException();
  145.         }
  146.         this.nickname = nickname;
  147.     }
  148.  
  149.     /**
  150.      * {@inheritDoc}
  151.      */
  152.     public void setPassword(final String password) {
  153.         if (password == null) {
  154.             throw new IllegalArgumentException();
  155.         }
  156.         this.password = password;
  157.     }
  158.  
  159.     /**
  160.      * {@inheritDoc}
  161.      */
  162.     @Override
  163.     public void setBestScore(final int bestScore) {
  164.         if (bestScore < 0 || bestScore < this.bestScore) {
  165.             throw new IllegalArgumentException();
  166.         }
  167.         this.bestScore = bestScore;
  168.     }
  169.  
  170.     /**
  171.      * {@inheritDoc}
  172.      */
  173.     @Override
  174.     public void setSettings(final Settings settings) {
  175.         if (settings == null) {
  176.             throw new IllegalArgumentException();
  177.         }
  178.         this.settings = settings;
  179.     }
  180.  
  181.     /**
  182.      * @see java.lang.Object#hashCode()
  183.      */
  184.     @Override
  185.     public int hashCode() {
  186.         final int prime = 31;
  187.         int result = 1;
  188.         result = prime * result + ((username == null) ? 0 : username.hashCode());
  189.         return result;
  190.     }
  191.  
  192.     /**
  193.      * @see java.lang.Object#equals(java.lang.Object)
  194.      */
  195.     @Override
  196.     public boolean equals(final Object obj) {
  197.         if (this == obj) {
  198.             return true;
  199.         }
  200.         if (obj == null) {
  201.             return false;
  202.         }
  203.         if (!(obj instanceof AccountImpl)) {
  204.             return false;
  205.         }
  206.         final AccountImpl other = (AccountImpl) obj;
  207.         if (username == null) {
  208.             if (other.username != null) {
  209.                 return false;
  210.             }
  211.         } else if (!username.equals(other.username)) {
  212.             return false;
  213.         }
  214.         return true;
  215.     }
  216.  
  217. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement