sai-pullabhotla
9/17/2017 - 2:49 PM

Person Entity Using Builder Pattern

Person Entity Using Builder Pattern

import com.jmethods.catatumbo.Entity;
import com.jmethods.catatumbo.Identifier;
import com.jmethods.catatumbo.Property;

@Entity
public class Person {

	@Identifier
	private final long id;

	@Property(name = "fname")
	private final String firstName;

	@Property(name = "lname")
	private final String lastName;

	@Property(indexed = false)
	private final int birthYear;

	private final boolean citizen;

	private Person(PersonBuilder builder) {
		this.id = builder.id;
		this.firstName = builder.firstName;
		this.lastName = builder.lastName;
		this.birthYear = builder.birthYear;
		this.citizen = builder.citizen;
	}

	public long getId() {
		return id;
	}

	public String getFirstName() {
		return firstName;
	}

	public String getLastName() {
		return lastName;
	}

	public int getBirthYear() {
		return birthYear;
	}

	public boolean isCitizen() {
		return citizen;
	}

	public static PersonBuilder newBuilder() {
		return new PersonBuilder();
	}

	public static class PersonBuilder {
		private long id;
		private String firstName;
		private String lastName;
		private int birthYear;
		private boolean citizen;

		public void setId(long id) {
			this.id = id;
		}

		public void setFirstName(String firstName) {
			this.firstName = firstName;
		}

		public void setLastName(String lastName) {
			this.lastName = lastName;
		}

		public void setBirthYear(int birthYear) {
			this.birthYear = birthYear;
		}

		public void setCitizen(boolean citizen) {
			this.citizen = citizen;
		}

		public Person build() {
			return new Person(this);
		}

	}

}