View Javadoc
1   /**
2   The contents of this file are subject to the Mozilla Public License Version 1.1 
3   (the "License"); you may not use this file except in compliance with the License. 
4   You may obtain a copy of the License at http://www.mozilla.org/MPL/ 
5   Software distributed under the License is distributed on an "AS IS" basis, 
6   WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the 
7   specific language governing rights and limitations under the License. 
8   
9   The Original Code is "Location.java".  Description: 
10  "Location of an validated message element" 
11  
12  The Initial Developer of the Original Code is University Health Network. Copyright (C) 
13  2012.  All Rights Reserved. 
14  
15  Contributor(s): ______________________________________. 
16  
17  Alternatively, the contents of this file may be used under the terms of the 
18  GNU General Public License (the "GPL"), in which case the provisions of the GPL are 
19  applicable instead of those above.  If you wish to allow use of your version of this 
20  file only under the terms of the GPL and not to allow others to use your version 
21  of this file under the MPL, indicate your decision by deleting  the provisions above 
22  and replace  them with the notice and other provisions required by the GPL License.  
23  If you do not delete the provisions above, a recipient may use your version of 
24  this file under either the MPL or the GPL. 
25   */
26  package ca.uhn.hl7v2;
27  
28  import java.util.Collection;
29  import java.util.Collections;
30  import java.util.Stack;
31  
32  /**
33   * Location class to determine the location of an element
34   * 
35   * @author Christian Ohr
36   */
37  public class Location {
38  
39      private Stack<GroupLocation> groups = new Stack<>();
40  	private String segmentName = null;
41  	private int segmentRepetition = 0;
42  	private int field = -1;
43  	private int fieldRepetition = 0;
44  	private int component = -1;
45  	private int subcomponent = -1;
46  
47      private boolean componentLevel;
48  
49  	public static final Locationn">Location UNKNOWN = new Location();
50  
51  	public Location() {
52  	}
53  
54  	public Location" href="../../../ca/uhn/hl7v2/Location.html#Location">Location(Location l) {
55          this.groups = new Stack<>();
56          groups.addAll(l.groups);
57  		this.segmentName = l.segmentName;
58  		this.segmentRepetition = l.segmentRepetition;
59  		this.field = l.field;
60  		this.fieldRepetition = l.fieldRepetition;
61  		this.component = l.component;
62  		this.subcomponent = l.subcomponent;
63  		this.componentLevel = l.componentLevel;
64  	}
65  
66  	public Collection<GroupLocation> getGroups() {
67  		return Collections.unmodifiableCollection(groups);
68  	}
69  	
70  	public boolean isUnknown() {
71  	    return this == UNKNOWN;
72  	}
73  
74      public Location pushGroup(String name, int rep) {
75          groups.push(new Location.GroupLocation(name, rep));
76          return this;
77      }
78  
79      public GroupLocation popGroup() {
80          return groups.pop();
81      }
82  
83      public GroupLocation getGroupLocation() {
84          return groups.peek();
85      }
86  
87  	public String getSegmentName() {
88  		return segmentName;
89  	}
90  
91  	public Location withSegmentName(String segmentName) {
92  		this.segmentName = segmentName;
93  		return this;
94  	}
95  
96      public boolean isComponentLevel() {
97          return componentLevel;
98      }
99  
100     public Location atComponentLevel(boolean componentLevel) {
101         this.componentLevel = componentLevel;
102         return this;
103     }
104 
105 	public int getSegmentRepetition() {
106 		return segmentRepetition;
107 	}
108 
109 	public Location withSegmentRepetition(int segmentRepetition) {
110 		this.segmentRepetition = segmentRepetition;
111 		return this;
112 	}
113 
114 	public int getField() {
115 		return field;
116 	}
117 
118 	public Location withField(int field) {
119 		this.field = field;
120 		return this;
121 	}
122 
123 	public int getFieldRepetition() {
124 		return fieldRepetition;
125 	}
126 
127 	public Location withFieldRepetition(int fieldRepetition) {
128 		this.fieldRepetition = fieldRepetition;
129 		return this;
130 	}
131 
132 	public int getComponent() {
133 		return component;
134 	}
135 
136 	public Location withComponent(int component) {
137 		this.component = component;
138 		return this;
139 	}
140 
141 	public int getSubcomponent() {
142 		return subcomponent;
143 	}
144 
145 	public Location withSubcomponent(int subcomponent) {
146 		this.subcomponent = subcomponent;
147 		return this;
148 	}
149 
150 	/**
151 	 * Bulk setter for field indices
152 	 * 
153 	 * @param indices integer array as returned by Terser#getIndices
154 	 */
155 	public Location withFieldIndizes(int[] indices) {
156 		field = indices[0];
157 		fieldRepetition = indices[1];
158 		component = indices[2];
159 		subcomponent = indices[3];
160 		return this;
161 	}
162 
163 
164 	
165 	@Override
166 	public String toString() {
167 		StringBuilder sb = new StringBuilder();
168         if (!groups.isEmpty()) {
169             sb.append('/');
170             for (GroupLocation gl : groups) {
171                 sb.append(gl.groupName);
172                 if (gl.repetition >= 0) {
173                     sb.append('(')
174                     .append(gl.repetition)
175                     .append(")");
176                 }
177                 sb.append("/");
178             }
179         }
180 		if (segmentName != null) {
181 			sb.append(segmentName);
182 			if (segmentRepetition > 0) {
183 				sb.append("(").append(segmentRepetition).append(")");
184 			}
185 			if (field > 0) {
186 				sb.append("-").append(field);
187 				if (fieldRepetition >= 0) {
188 					sb.append("(").append(fieldRepetition).append(")");
189 				}
190 				if (component > 0) {
191 					sb.append("-").append(component);
192 					if (subcomponent > 0) {
193 						sb.append("-").append(subcomponent);
194 					}
195 				}
196 			}
197 		} else {
198 			if (sb.length() == 0) sb.append("unknown location");
199 		}
200 		return sb.toString();
201 	}
202 
203 	@Override
204 	public int hashCode() {
205 		final int prime = 31;
206 		int result = 1;
207         result = prime * result + ((groups == null) ? 0 : groups.hashCode());
208 		result = prime * result + component;
209 		result = prime * result + field;
210 		result = prime * result + fieldRepetition;
211 		result = prime * result + ((segmentName == null) ? 0 : segmentName.hashCode());
212 		result = prime * result + segmentRepetition;
213 		result = prime * result + subcomponent;
214 		return result;
215 	}
216 
217 	@Override
218 	public boolean equals(Object obj) {
219 		if (this == obj)
220 			return true;
221 		if (obj == null)
222 			return false;
223 		if (getClass() != obj.getClass())
224 			return false;
225 		Location other = (Location) obj;
226 		if (groups == null) {
227 		    if (other.groups != null)
228 		        return false;
229 		} else if (!groups.equals(other.groups))
230 		    return false;
231 		if (component != other.component)
232 			return false;
233 		if (field != other.field)
234 			return false;
235 		if (fieldRepetition != other.fieldRepetition)
236 			return false;
237 		if (segmentName == null) {
238 			if (other.segmentName != null)
239 				return false;
240 		} else if (!segmentName.equals(other.segmentName))
241 			return false;
242 		if (segmentRepetition != other.segmentRepetition)
243 			return false;
244         return subcomponent == other.subcomponent;
245     }
246 
247     public static class GroupLocation {
248         final String groupName;
249         final int repetition;
250 
251         private GroupLocation(String groupName, int repetition) {
252             this.groupName = groupName;
253             this.repetition = repetition;
254         }
255 
256         public String getGroupName() {
257             return groupName;
258         }
259 
260         public int getRepetition() {
261             return repetition;
262         }
263     }
264 
265 }