View Javadoc

1   package ca.uhn.hl7v2.testpanel.model.conf;
2   
3   import java.util.ArrayList;
4   import java.util.LinkedHashMap;
5   import java.util.List;
6   import java.util.Map;
7   
8   import javax.xml.bind.annotation.XmlAccessType;
9   import javax.xml.bind.annotation.XmlAccessorType;
10  import javax.xml.bind.annotation.XmlElement;
11  import javax.xml.bind.annotation.XmlType;
12  
13  import org.apache.commons.lang.ObjectUtils;
14  import org.apache.commons.lang.builder.HashCodeBuilder;
15  
16  import ca.uhn.hl7v2.testpanel.model.AbstractModelClass;
17  
18  @XmlAccessorType(XmlAccessType.FIELD)
19  @XmlType(name = "Table")
20  public class Table extends AbstractModelClass implements Comparable<Table> {
21  
22  	public static final String PROP_CODES = Table.class.getName() + "_CODES";
23  
24  	@XmlElement(name = "code")
25  	private List<Code> myCodes;
26  
27  	@XmlElement(name = "id")
28  	private String myCodeSystemId;
29  
30  	@XmlElement(name = "name")
31  	private String myCodeSystemName;
32  
33  	private transient TableFile myTableFile;
34  
35  	private transient LinkedHashMap<String, Code> myCodeIdToCodes;
36  	
37  	public void addCode(Code theCode) {
38  		myCodes.add(theCode);
39  		updateCodeIdToCodes();
40  		firePropertyChange(PROP_CODES, null, null);
41  	}
42  
43  	private Map<String, Code> getCodeIdToCodes() {
44  		if (myCodeIdToCodes == null) {
45  			myCodeIdToCodes = new LinkedHashMap<String, Code>(getCodes().size() + 10);
46  			for (Code next : getCodes()) {
47  				next.setTable(this);
48  				myCodeIdToCodes.put(next.getCode(), next);
49  			}
50  		}
51  		return myCodeIdToCodes;
52  	}
53  	
54  	void updateCodeIdToCodes() {
55  		myCodeIdToCodes = null;
56  	}
57  	
58  	/**
59  	 * {@inheritDoc}
60  	 */
61  	@Override
62  	public boolean equals(Object theObj) {
63  		if (!(theObj instanceof Table)) {
64  			return false;
65  		}
66  
67  		return ObjectUtils.equals(myCodeSystemId, ((Table) theObj).myCodeSystemId) && ObjectUtils.equals(myCodes, ((Table) theObj).myCodes);
68  	}
69  
70  	@Override
71  	public Object exportConfigToXml() {
72  		// TODO Auto-generated method stub
73  		return null;
74  	}
75  
76  	/**
77  	 * @return the codes
78  	 */
79  	public List<Code> getCodes() {
80  		if (myCodes == null) {
81  			myCodes = new ArrayList<Code>();
82  		}
83  		return myCodes;
84  	}
85  
86  	/**
87  	 * @return the codeSystemId
88  	 */
89  	public String getCodeSystemId() {
90  		return myCodeSystemId;
91  	}
92  
93  	/**
94  	 * @return the codeSystemName
95  	 */
96  	public String getCodeSystemName() {
97  		return myCodeSystemName;
98  	}
99  
100 	/**
101 	 * @return the tableFile
102 	 */
103 	public TableFile getTableFile() {
104 		return myTableFile;
105 	}
106 
107 	/**
108 	 * {@inheritDoc}
109 	 */
110 	@Override
111 	public int hashCode() {
112 		HashCodeBuilder b = new HashCodeBuilder();
113 		b.append(myCodeSystemId);
114 		return b.toHashCode();
115 	}
116 
117 	/**
118 	 * @param theCodes
119 	 *            the codes to set
120 	 */
121 	public void setCodes(List<Code> theCodes) {
122 		myCodes = theCodes;
123 	}
124 
125 	/**
126 	 * @param theCodeSystemId
127 	 *            the codeSystemId to set
128 	 */
129 	public void setCodeSystemId(String theCodeSystemId) {
130 
131 		if (myTableFile != null) {
132 			myTableFile.updateCodeSystemIdToTable();
133 		}
134 
135 		myCodeSystemId = theCodeSystemId;
136 	}
137 
138 	/**
139 	 * @param theCodeSystemName
140 	 *            the codeSystemName to set
141 	 */
142 	public void setCodeSystemName(String theCodeSystemName) {
143 		myCodeSystemName = theCodeSystemName;
144 	}
145 
146 	public void setTableFile(TableFile theTableFile) {
147 		myTableFile = theTableFile;
148 	}
149 
150 	/**
151 	 * {@inheritDoc}
152 	 */
153 	@Override
154 	public String toString() {
155 		return "Table[id=" + myCodeSystemId + ", codes=" + myCodes + "]";
156 	}
157 
158 	public String[] getCodesAsArray() {
159 		String[] retVal = new String[myCodes.size()];
160 		int i = 0;
161 		for (Code next : myCodes) {
162 			retVal[i++] = next.getCode();
163 		}
164 		return retVal;
165 	}
166 
167 	public boolean isValidCode(String theCode) {
168 		return getCodeIdToCodes().containsKey(theCode);
169 	}
170 
171 	public int compareTo(Table theO) {
172 		return myCodeSystemId.compareTo(theO.myCodeSystemId);
173 	}
174 
175 }