package example.entity;

import java.io.Serializable;
import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;

import com.tangosol.io.pof.annotation.Portable;
import com.tangosol.io.pof.annotation.PortableProperty;

/**
 * Example person classe, used in various education examples
 * 
 * @author education@oracle.com
 * 
 */
@SuppressWarnings("serial")
@Portable
public class Person implements Serializable {

	@PortableProperty(0) protected String name;
	@PortableProperty(1) protected int age;
	@PortableProperty(2) protected String sex;
	@PortableProperty(3) protected Address address;
	@PortableProperty(4) protected int touchCount;
	private transient Date lastTouch;

	/**
	 * No arg constructor required for POF
	 */
	public Person() {}

	@Override
	public String toString() {
		return "Person("+getKey()+") [name=" + name + ", age=" + age + ", sex=" + sex
				+ ", isValidPerson=" + isValidPerson() + ", address=" + (address == null? " null": address.toString())
				+ ", touchCount=" + touchCount + "]";
	}

	public Person(String name, int age, String sex) {
		this.name = name;
		this.age = age;
		this.sex = sex;
		this.address = null;
		this.touchCount = 0;
		this.lastTouch = new Date();

	}

	public boolean isValidPerson() {
		if (name == null )
			return false;
		else if (age <=0 )
			return false;
		else if (sex == null)
			return false;
		return true;
	}
	public String getName() {
		return name;
	}

	public void setName(String name) {
		touch();
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		touch();
		this.age = age;
	}

	public Address getAddress() {
		return address;
	}

	public void setAddress(Address address) {
		touch();
		this.address = address;
	}

	public int getTouchCount() {
		return touchCount;
	}

	public void touch() {
		touchCount++;
		lastTouch = new Date();
	}

	public Date getLastTouch() {
		return lastTouch;
	}

	public String getLastTouchDate() {
		DateFormat dateFormat = new SimpleDateFormat(
				"yyyy/MM/dd HH:mm:ss.SSSSS");

		return dateFormat.format(lastTouch);
	}
	
	public String getKey() {
		return name+"."+ new Integer(age).toString();
	}

	

}
