1 21 22 package nu.xom.samples; 23 24 import java.util.ArrayList ; 25 import java.util.HashMap ; 26 import java.util.Iterator ; 27 import java.util.List ; 28 import java.util.Map ; 29 30 import nu.xom.Element; 31 32 33 48 public class Agency { 49 50 private String code; 51 private String name; 52 private String treasuryCode; 53 private String year; 54 55 private List bureaus = new ArrayList (); 56 57 private static Map instances = new HashMap (); 58 59 private Agency(String name, String code, String treasuryCode, 62 String year) { 63 64 this.name = name; 65 this.code = code; 66 this.treasuryCode = treasuryCode; 67 this.year = year; 68 69 } 70 71 public static Agency getInstance(String name, String code, 72 String treasuryCode, String year) { 73 74 String key = code+" "+year; 76 Agency agency = (Agency) instances.get(key); 77 if (agency == null) { 78 agency = new Agency(name, code, treasuryCode, year); 79 instances.put(key, agency); 80 } 81 82 return agency; 83 84 } 85 86 public void add(Bureau b) { 87 if (!bureaus.contains(b)) { 88 bureaus.add(b); 89 } 90 } 91 92 public Element getXML() { 93 94 Element agency = new Element("Agency"); 95 Element name = new Element("Name"); 96 Element code = new Element("Code"); 97 Element treasuryAgencyCode = new Element("TreasuryAgencyCode"); 98 name.appendChild(this.name); 99 code.appendChild(this.code); 100 treasuryAgencyCode.appendChild(treasuryCode); 101 agency.appendChild(name); 102 agency.appendChild(code); 103 agency.appendChild(treasuryAgencyCode); 104 105 Iterator iterator = bureaus.iterator(); 106 while (iterator.hasNext()) { 107 Bureau bureau = (Bureau) iterator.next(); 108 agency.appendChild(bureau.getXML()); 109 } 110 return agency; 111 112 } 113 114 } | Popular Tags |