import java.lang.*; /** * PersonName.java * * * Created: Wed Jan 22 09:10:45 2003 *
* A class representing a person's name.
* Every PersonName has a first name, a middle name, and a last name.
*
* @author Stuart C. Shapiro
* @version
*/
public class PersonName implements Comparable {
private String first; // The first (given) name of this PersonName.
private String middle; // The middle name of this PersonName.
private String last; // The last (family) name of this PersonName.
/**
* Creates a new PersonName
instance
* with a first, middle, and last name.
*
* @param f the first (given) name of this PersonName.
* @param m the middle name of this PersonName.
* @param l the last (family) name of this PersonName.
*/
public PersonName (String f, String m, String l){
first = f;
middle = m;
last = l;
}
/**
* Creates a new PersonName
instance
* with a first and last name, but no middle name.
*
* @param f the first (given) name of this PersonName.
* @param l the last (family) name of this PersonName.
*/
public PersonName(String f, String l) {
this(f,"",l);
}
/**
* Get the value of first.
* @return value of first.
*/
public String getFirst() {
return first;
}
/**
* Set the value of first.
* @param v Value to assign to first.
*/
public void setFirst(String v) {
this.first = v;
}
/**
* Get the value of middle.
* @return value of middle.
*/
public String getMiddle() {
return middle;
}
/**
* Set the value of middle.
* @param v Value to assign to middle.
*/
public void setMiddle(String v) {
this.middle = v;
}
/**
* Get the value of last.
* @return value of last.
*/
public String getLast() {
return last;
}
/**
* Set the value of last.
* @param v Value to assign to last.
*/
public void setLast(String v) {
this.last = v;
}
/**
* Returns a string representation of this PersonName.
*
* @return a String
representation of this PersonName.
*/
public String toString() {
return first
+ (middle.equals("")?" ":" " + middle + " ")
+ last;
}
/**
* Compares this PersonName with another for content equality.
*
* @param n the other PersonName
* @return true if this PersonName and the other are content equal,
* false otherwise.
*/
public boolean equals(PersonName n) {
return last.equals(n.getLast())
&& middle.equals(n.getMiddle())
&& first.equals(n.getFirst());
}
/**
* Compares this PersonName with another
* to determine which should come first in an ordering.
*
* @param obj the Object
* this PersonName is to be compared with
* @return -1 if this PersonName should come before the other,
* 0 if they compare the same,
* 1 if this PersonName should come after the other.
* @throws IllegalArgumentException if obj is not a PersonName.
*/
public int compareTo(Object obj) {
if (!(obj instanceof PersonName)) {
throw new IllegalArgumentException("Not a PersonName.");
}
PersonName n = (PersonName)obj;
if (last.compareTo(n.getLast()) < 0) {return -1;}
if (last.compareTo(n.getLast()) > 0) {return 1;}
if (first.compareTo(n.getFirst()) < 0) {return -1;}
if (first.compareTo(n.getFirst()) > 0) {return 1;}
if (middle.compareTo(n.getMiddle()) < 0) {return -1;}
if (middle.compareTo(n.getMiddle()) > 0) {return 1;}
return 0;
}
}// PersonName