001/**
002The contents of this file are subject to the Mozilla Public License Version 1.1 
003(the "License"); you may not use this file except in compliance with the License. 
004You may obtain a copy of the License at http://www.mozilla.org/MPL/ 
005Software distributed under the License is distributed on an "AS IS" basis, 
006WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the 
007specific language governing rights and limitations under the License. 
008
009The Original Code is "Location.java".  Description: 
010"Location of an validated message element" 
011
012The Initial Developer of the Original Code is University Health Network. Copyright (C) 
0132012.  All Rights Reserved. 
014
015Contributor(s): ______________________________________. 
016
017Alternatively, the contents of this file may be used under the terms of the 
018GNU General Public License (the "GPL"), in which case the provisions of the GPL are 
019applicable instead of those above.  If you wish to allow use of your version of this 
020file only under the terms of the GPL and not to allow others to use your version 
021of this file under the MPL, indicate your decision by deleting  the provisions above 
022and replace  them with the notice and other provisions required by the GPL License.  
023If you do not delete the provisions above, a recipient may use your version of 
024this file under either the MPL or the GPL. 
025 */
026package ca.uhn.hl7v2;
027
028/**
029 * Location class to determine the location of an element that caused an exception
030 * 
031 * @author Christian Ohr
032 */
033public class Location {
034
035        private String segmentName = null;
036        private int segmentRepetition = -1;
037        private int field = -1;
038        private int fieldRepetition = -1;
039        private int component = -1;
040        private int subcomponent = -1;
041
042        public static final Location UNKNOWN = new Location();
043
044        public Location() {
045        }
046
047        public Location(Location l) {
048                this.segmentName = l.segmentName;
049                this.segmentRepetition = l.segmentRepetition;
050                this.field = l.field;
051                this.fieldRepetition = l.fieldRepetition;
052                this.component = l.component;
053                this.subcomponent = l.subcomponent;
054        }
055        
056        public boolean isUnknown() {
057            return this == UNKNOWN;
058        }
059
060        public String getSegmentName() {
061                return segmentName;
062        }
063
064        public void setSegmentName(String segmentName) {
065                this.segmentName = segmentName;
066        }
067
068        public int getSegmentRepetition() {
069                return segmentRepetition;
070        }
071
072        public void setSegmentRepetition(int segmentRepetition) {
073                this.segmentRepetition = segmentRepetition;
074        }
075
076        public int getField() {
077                return field;
078        }
079
080        public void setField(int field) {
081                this.field = field;
082        }
083
084        public int getFieldRepetition() {
085                return fieldRepetition;
086        }
087
088        public void setFieldRepetition(int fieldRepetition) {
089                this.fieldRepetition = fieldRepetition;
090        }
091
092        public int getComponent() {
093                return component;
094        }
095
096        public void setComponent(int component) {
097                this.component = component;
098        }
099
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}