/***
 * Excerpted from "Domain-Driven Design Using Naked Objects",
 * published by The Pragmatic Bookshelf.
 * Copyrights apply to this code. It may not be used to create training material, 
 * courses, books, articles, and the like. Contact us if you are in doubt.
 * We make no guarantees that this code is fit for any purpose. 
 * Visit http://www.pragmaticprogrammer.com/titles/dhnako for more book information.
***/
package com.pragprog.dhnako.carserv.dom.person;

import org.nakedobjects.applib.AbstractDomainObject;
import org.nakedobjects.applib.annotation.Hidden;
import org.nakedobjects.applib.annotation.MaxLength;
import org.nakedobjects.applib.annotation.MemberOrder;
import org.nakedobjects.applib.util.TitleBuffer;

public class Name extends AbstractDomainObject {

	// {{ Identification
	public String title() {
		final TitleBuffer buf = new TitleBuffer();
		if (getTitle() != null) {
			buf.append(getTitle());
		}
		buf.append(getFirstName()).append(getLastName());
		return buf.toString();
	}

	// }}

	
	// {{ Title
	private Title title;

	@MemberOrder(sequence = "1")
	public Title getTitle() {
		return title;
	}

	public void setTitle(final Title title) {
		this.title = title;
	}
	// }}



	// {{ FirstName
	private String firstName;

	@MaxLength(30)
	@MemberOrder(sequence = "1.3")
	public String getFirstName() {
		return firstName;
	}

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

	// {{ LastName
	private String lastName;

	@MaxLength(30)
	@MemberOrder(sequence = "1.2")
	public String getLastName() {
		return lastName;
	}

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

	
	// {{ Helpers
	@Hidden
	public boolean matches(final String firstName, final String lastName) {
		return nullSafeEquals(this.getFirstName(), firstName) &&
		       nullSafeEquals(this.getLastName(), lastName);
	}
	private static <T> boolean nullSafeEquals(final T s1, final T s2) {
		    return s1 == null || s2 == null ||
		           s1 != null && s2 != null && s1.equals(s2);
	}
	// }}

}
