org.apache.juneau.doc-files.AddressBook.html Maven / Gradle / Ivy
AddressBook sample source
Sample code use in various examples throughout the Javadocs. Represents a simple POJO model consisting
of a collection (LinkedList
), beans (Address
, Person
), and a type 4a transformed type (Calendar
).
Public fields are used for bean properties in-leu of getters and setters to reduce the size of the example.
Bean properties defined using getters and setters would work identically.
AddressBook.java
// A collection of people
public class AddressBook extends LinkedList<Person> {
// Extra method for adding a person to this address book.
// Used in PojoIntrospector usage examples.
public void addPerson(String name, String birthDate, List<Address> addresses) {
add(new Person(name, birthdate, addresses));
}
}
Address.java
public class Address {
// Bean properties
public String street , city , state ;
public int zip ;
public boolean isCurrent ;
// Bean constructor
public Address() {}
// Other constructor
public Address(String street, String city, String state, int zip, boolean isCurrent) {
this .street = street;
this .city = city;
this .state = state;
this .zip = zip;
this .isCurrent = isCurrent;
}
}
Person.java
public class Person {
// Bean properties
public String name ;
public int age ;
public Calendar birthDate ;
public LinkedList<Address> addresses = new LinkedList<Address>();
// Bean constructor
public Person() {}
// Other constructor
public Person(String name, String birthDate, Address...addresses) {
this .name = name;
this .birthDate = getBirthDate (birthDate);
this .age = calcAge (birthDate);
this .addresses .addAll(Arrays.asList (addresses));
}
// Other method
// Calculates a persons age based on the birthdate
public static int calcAge(String birthDate) {
return new GregorianCalendar().get(Calendar.YEAR ) - getBirthDate(birthDate).get(Calendar.YEAR );
}
// Utility method
// Converts a birthdate string to a Calendar
private static Calendar getBirthDate(String birthDate) {
try {
Calendar c = new GregorianCalendar();
c.setTime(DateFormat.getDateInstance (DateFormat.MEDIUM ).parse(birthDate));
return c;
} catch (ParseException e) {
throw new RuntimeException(e);
}
}
}