1 package org.springframework.samples.petclinic; 2 3 import java.util.ArrayList ; 4 import java.util.Collections ; 5 import java.util.HashSet ; 6 import java.util.Iterator ; 7 import java.util.List ; 8 import java.util.Set ; 9 10 import org.springframework.beans.support.MutableSortDefinition; 11 import org.springframework.beans.support.PropertyComparator; 12 13 19 public class Owner extends Person { 20 21 private Set pets; 22 23 protected void setPetsInternal(Set pets) { 24 this.pets = pets; 25 } 26 27 protected Set getPetsInternal() { 28 if (this.pets == null) { 29 this.pets = new HashSet (); 30 } 31 return this.pets; 32 } 33 34 public List getPets() { 35 List sortedPets = new ArrayList (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 51 public Pet getPet(String name) { 52 return getPet(name, false); 53 } 54 55 61 public Pet getPet(String name, boolean ignoreNew) { 62 name = name.toLowerCase(); 63 for (Iterator it = getPetsInternal().iterator(); it.hasNext();) { 64 Pet pet = (Pet) it.next(); 65 if (!ignoreNew || !pet.isNew()) { 66 String 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 |