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  /**
29   * Location class to determine the location of an element that caused an exception
30   * 
31   * @author Christian Ohr
32   */
33  public class Location {
34  
35  	private String segmentName = null;
36  	private int segmentRepetition = -1;
37  	private int field = -1;
38  	private int fieldRepetition = -1;
39  	private int component = -1;
40  	private int subcomponent = -1;
41  
42  	public static final Location UNKNOWN = new Location();
43  
44  	public Location() {
45  	}
46  
47  	public Location(Location l) {
48  		this.segmentName = l.segmentName;
49  		this.segmentRepetition = l.segmentRepetition;
50  		this.field = l.field;
51  		this.fieldRepetition = l.fieldRepetition;
52  		this.component = l.component;
53  		this.subcomponent = l.subcomponent;
54  	}
55  	
56  	public boolean isUnknown() {
57  	    return this == UNKNOWN;
58  	}
59  
60  	public String getSegmentName() {
61  		return segmentName;
62  	}
63  
64  	public void setSegmentName(String segmentName) {
65  		this.segmentName = segmentName;
66  	}
67  
68  	public int getSegmentRepetition() {
69  		return segmentRepetition;
70  	}
71  
72  	public void setSegmentRepetition(int segmentRepetition) {
73  		this.segmentRepetition = segmentRepetition;
74  	}
75  
76  	public int getField() {
77  		return field;
78  	}
79  
80  	public void setField(int field) {
81  		this.field = field;
82  	}
83  
84  	public int getFieldRepetition() {
85  		return fieldRepetition;
86  	}
87  
88  	public void setFieldRepetition(int fieldRepetition) {
89  		this.fieldRepetition = fieldRepetition;
90  	}
91  
92  	public int getComponent() {
93  		return component;
94  	}
95  
96  	public void setComponent(int component) {
97  		this.component = component;
98  	}
99  
100 	public int getSubcomponent() {
101 		return subcomponent;
102 	}
103 
104 	public void setSubcomponent(int subcomponent) {
105 		this.subcomponent = subcomponent;
106 	}
107 
108 	/**
109 	 * Bulk setter for field indices
110 	 * 
111 	 * @param indices integer array as returned by Terser#getIndices
112 	 */
113 	public void setFieldIndizes(int[] indices) {
114 		field = indices[0];
115 		fieldRepetition = indices[1];
116 		component = indices[2];
117 		subcomponent = indices[3];
118 	}
119 
120 	@Override
121 	public String toString() {
122 		StringBuilder sb = new StringBuilder();
123 		if (segmentName != null) {
124 			sb.append(segmentName);
125 			if (segmentRepetition >= 0) {
126 				sb.append("(").append(segmentRepetition).append(")");
127 			}
128 			if (field > 0) {
129 				sb.append("-").append(field);
130 				if (fieldRepetition >= 0) {
131 					sb.append("(").append(fieldRepetition).append(")");
132 				}
133 				if (component > 0) {
134 					sb.append("-").append(component);
135 					if (subcomponent > 0) {
136 						sb.append("-").append(subcomponent);
137 					}
138 				}
139 			}
140 		} else {
141 			sb.append("unknown location");
142 		}
143 		return sb.toString();
144 	}
145 
146 	@Override
147 	public int hashCode() {
148 		final int prime = 31;
149 		int result = 1;
150 		result = prime * result + component;
151 		result = prime * result + field;
152 		result = prime * result + fieldRepetition;
153 		result = prime * result + ((segmentName == null) ? 0 : segmentName.hashCode());
154 		result = prime * result + segmentRepetition;
155 		result = prime * result + subcomponent;
156 		return result;
157 	}
158 
159 	@Override
160 	public boolean equals(Object obj) {
161 		if (this == obj)
162 			return true;
163 		if (obj == null)
164 			return false;
165 		if (getClass() != obj.getClass())
166 			return false;
167 		Location other = (Location) obj;
168 		if (component != other.component)
169 			return false;
170 		if (field != other.field)
171 			return false;
172 		if (fieldRepetition != other.fieldRepetition)
173 			return false;
174 		if (segmentName == null) {
175 			if (other.segmentName != null)
176 				return false;
177 		} else if (!segmentName.equals(other.segmentName))
178 			return false;
179 		if (segmentRepetition != other.segmentRepetition)
180 			return false;
181 		if (subcomponent != other.subcomponent)
182 			return false;
183 		return true;
184 	}
185 
186 }