KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > samples > petclinic > Owner


1 package org.springframework.samples.petclinic;
2
3 import java.util.ArrayList JavaDoc;
4 import java.util.Collections JavaDoc;
5 import java.util.HashSet JavaDoc;
6 import java.util.Iterator JavaDoc;
7 import java.util.List JavaDoc;
8 import java.util.Set JavaDoc;
9
10 import org.springframework.beans.support.MutableSortDefinition;
11 import org.springframework.beans.support.PropertyComparator;
12
13 /**
14  * Simple JavaBean domain object representing an owner.
15  *
16  * @author Ken Krebs
17  * @author Juergen Hoeller
18  */

19 public class Owner extends Person {
20
21     private Set JavaDoc pets;
22
23     protected void setPetsInternal(Set JavaDoc pets) {
24         this.pets = pets;
25     }
26
27     protected Set JavaDoc getPetsInternal() {
28         if (this.pets == null) {
29             this.pets = new HashSet JavaDoc();
30         }
31         return this.pets;
32     }
33
34     public List JavaDoc getPets() {
35         List JavaDoc sortedPets = new ArrayList JavaDoc(getPetsInternal());
36         PropertyComparator.sort(sortedPets, new MutableSortDefinition("name", true, true));
37         return Collections.unmodifiableList(sortedPets);
38     }
39
40     public void addPet(Pet pet) {
41         getPetsInternal().add(pet);
42         pet.setOwner(this);
43     }
44
45     /**
46      * Return the Pet with the given name,
47      * or null if none found for this Owner.
48      * @param name to test
49      * @return true if pet name is already in use
50      */

51     public Pet getPet(String JavaDoc name) {
52         return getPet(name, false);
53     }
54
55     /**
56      * Return the Pet with the given name,
57      * or null if none found for this Owner.
58      * @param name to test
59      * @return true if pet name is already in use
60      */

61     public Pet getPet(String JavaDoc name, boolean ignoreNew) {
62         name = name.toLowerCase();
63         for (Iterator JavaDoc it = getPetsInternal().iterator(); it.hasNext();) {
64             Pet pet = (Pet) it.next();
65             if (!ignoreNew || !pet.isNew()) {
66                 String JavaDoc compName = pet.getName();
67                 compName = compName.toLowerCase();
68                 if (compName.equals(name)) {
69                     return pet;
70                 }
71             }
72         }
73         return null;
74     }
75
76 }
77
Popular Tags