Skip to content

Instantly share code, notes, and snippets.

@purijatin
Last active November 12, 2015 13:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save purijatin/5811ced3f9aedc977f99 to your computer and use it in GitHub Desktop.
Save purijatin/5811ced3f9aedc977f99 to your computer and use it in GitHub Desktop.
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import java.util.Optional;
import static java.util.Objects.isNull;
public class User {
final int id;
final String firstName;
final Optional<String> middleName;
final String lastName;
final int age;
final long phone;
final Optional<String> zip;
@JsonCreator
public User(
@JsonProperty("id") int id,
@JsonProperty("firstName")String firstName,
@JsonProperty("middleName")String middleName,
@JsonProperty("lastName")String lastName,
@JsonProperty("age")int age,
@JsonProperty("phoneNumber")long phoneNumber,
@JsonProperty("zip")String zip) {
if (isNull(firstName) || isNull(lastName) ) {
throw new NullPointerException((isNull(firstName) ? "firstName" : "lastName") + " is null");
}
this.id = id;
this.firstName = firstName;
this.middleName = Optional.ofNullable(middleName);
if (this.middleName.isPresent() && !isPureString(middleName)) {
throw new IllegalArgumentException("Middle name should be a string. Provided: " + middleName);
}
this.lastName = lastName;
this.age = age;
this.phone = phoneNumber;
this.zip = Optional.ofNullable(zip);
}
//@JsonCreator
public User(int id, String firstName, String lastName, int age, long phoneNumber) {
this(id, firstName, null, lastName, age, phoneNumber, null);
}
public int getId() {
return id;
}
public String getFirstName() {
return firstName;
}
public Optional<String> getMiddleName() {
return middleName;
}
public String getLastName() {
return lastName;
}
public int getAge() {
return age;
}
public long getPhone() {
return phone;
}
public Optional<String> getZip() {
return zip;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof User)) return false;
User user = (User) o;
return id == user.id;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment