Guest User

Untitled

a guest
Jul 22nd, 2018
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.36 KB | None | 0 0
  1. public class Employee {
  2.  
  3. public static class Builder<T extends Employee> {
  4.  
  5. private T entity;
  6.  
  7. public Builder() {
  8. this.entity = new Employee();
  9. }
  10.  
  11. public Builder withAge(final String age) {
  12. this.entity.age = age;
  13. return this;
  14. }
  15.  
  16. public Builder withName(final String name) {
  17. this.entity.name = name;
  18. return this;
  19. }
  20.  
  21. public Builder withRole(final String role) {
  22. this.entity.role = role;
  23. return this;
  24. }
  25.  
  26. public T build() {
  27. if (this.entity.age == null || this.entity.age.trim().isEmpty()) {
  28. throw new IllegalArgumentException("Missing age");
  29. }
  30. if (this.entity.name == null || this.entity.name.trim().isEmpty()) {
  31. throw new IllegalArgumentException("Missing name");
  32. }
  33. if (this.entity.role == null || this.entity.role.trim().isEmpty()) {
  34. throw new IllegalArgumentException("Missing role");
  35. }
  36. return new Employee(this);
  37. }
  38.  
  39. }
  40.  
  41. private String age;
  42. private String name;
  43. private String role;
  44.  
  45. private Employee() {
  46. }
  47.  
  48. private Employee(final Builder builder) {
  49. this.age = builder.entity.age;
  50. this.name = builder.entity.name;
  51. this.role = builder.entity.role;
  52. }
  53.  
  54. public String getAge() {
  55. this.age;
  56. }
  57.  
  58. public String getName() {
  59. this.name;
  60. }
  61.  
  62. public String getRole() {
  63. this.role;
  64. }
  65.  
  66. // equals not included in this example
  67. // hashCode not included in this example
  68. // toString not included in this example
  69.  
  70. }
Add Comment
Please, Sign In to add comment