Created
September 1, 2017 15:26
-
-
Save Sxtanna/569d19f7963b9c63f82e2c654ed48c42 to your computer and use it in GitHub Desktop.
User Classes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Objects; | |
// This class is still missing @NotNull annotations | |
public final class JUser { | |
private static final int DEFAULT_AGE = 1; | |
private static final String DEFAULT_MIDDLE = ""; | |
// start fields | |
private int age; | |
private String firstName, middleName, lastName; | |
// start constructors (many permutations more due to parameter labels) | |
public JUser(String firstName, String middleName, String lastName, int age) { | |
this.age = age; | |
this.firstName = firstName; | |
this.middleName = middleName; | |
this.lastName = lastName; | |
} | |
public JUser(String firstName, String middleName, String lastName) { | |
this(firstName, middleName, lastName, DEFAULT_AGE); | |
} | |
public JUser(String firstName, String lastName, int age) { | |
this(firstName, DEFAULT_MIDDLE, lastName, age); | |
} | |
public JUser(String firstName, String lastName) { | |
this(firstName, lastName, DEFAULT_AGE); | |
} | |
// start getters/setters | |
public String getFirstName() { | |
return firstName; | |
} | |
public void setFirstName(String firstName) { | |
this.firstName = firstName; | |
} | |
public String getMiddleName() { | |
return middleName; | |
} | |
public void setMiddleName(String middleName) { | |
this.middleName = middleName; | |
} | |
public String getLastName() { | |
return lastName; | |
} | |
public void setLastName(String lastName) { | |
this.lastName = lastName; | |
} | |
public int getAge() { | |
return age; | |
} | |
public void setAge(int age) { | |
this.age = age; | |
} | |
// start components | |
public String component1() { | |
return firstName; | |
} | |
public String component2() { | |
return middleName; | |
} | |
public String component3() { | |
return lastName; | |
} | |
public int component4() { | |
return age; | |
} | |
// start copy (many permutations more due to parameter labels) | |
public JUser copy(String firstName, String middleName, String lastName, int age) { | |
return new JUser(firstName, middleName, lastName, age); | |
} | |
public JUser copy(String firstName, String middleName, String lastName) { | |
return copy(firstName, middleName, lastName, this.age); | |
} | |
public JUser copy(String firstName, String middleName) { | |
return copy(firstName, middleName, this.lastName, this.age); | |
} | |
public JUser copy(String firstName) { | |
return copy(firstName, this.middleName, this.lastName, this.age); | |
} | |
public JUser copy() { | |
return copy(this.firstName, this.middleName, this.lastName, this.age); | |
} | |
// start object overrides | |
@Override | |
public boolean equals(Object o) { | |
if (this == o) return true; | |
if (!(o instanceof JUser)) return false; | |
JUser jUser = (JUser) o; | |
return getAge() == jUser.getAge() && | |
Objects.equals(getFirstName(), jUser.getFirstName()) && | |
Objects.equals(getMiddleName(), jUser.getMiddleName()) && | |
Objects.equals(getLastName(), jUser.getLastName()); | |
} | |
@Override | |
public int hashCode() { | |
return Objects.hash(getAge(), getFirstName(), getMiddleName(), getLastName()); | |
} | |
@Override | |
public String toString() { | |
final StringBuilder sb = new StringBuilder("JUser{"); | |
sb.append("age=").append(age); | |
sb.append(", firstName='").append(firstName).append('\''); | |
sb.append(", middleName='").append(middleName).append('\''); | |
sb.append(", lastName='").append(lastName).append('\''); | |
sb.append('}'); | |
return sb.toString(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
data class KUser(var firstName : String, var middleName : String = "", var lastName : String, var age : Int = 1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment