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 "AbstractComposite.java".  Description:
10  "A partial implementation of Composite"
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.model;
27  
28  import org.slf4j.Logger;
29  import org.slf4j.LoggerFactory;
30  
31  import ca.uhn.hl7v2.HL7Exception;
32  import ca.uhn.hl7v2.Location;
33  
34  
35  public abstract class AbstractComposite extends AbstractType implements
36  		Composite {
37  
38  	private static final long serialVersionUID = -2657103285266475699L;
39  
40  	private static final Logger log = LoggerFactory.getLogger(AbstractComposite.class);
41  
42  	public AbstractComposite(Message message) {
43  		super(message);
44  	}
45  
46  	@Override
47  	public void clear() {
48  		super.clear();
49  		for (Type component : getComponents()) {
50  			component.clear();
51  		}
52  	}
53  
54  	protected <T extends Type> T getTyped(int idx, Class<T> type) {
55  		try {
56  			@SuppressWarnings("unchecked") T ret = (T)getComponent(idx);
57  			return ret;
58  		} catch (HL7Exception e) {
59  	         log.error("Unexpected problem accessing known data type component - this is a bug. Class is " + getClass().getName(), e);
60  	         throw new RuntimeException(e);
61  		}
62  	}
63  
64  	@Override
65  	public boolean isEmpty() throws HL7Exception {
66  		for (Type type : getComponents()) {
67  			if (!type.isEmpty()) return false;
68  		}
69  		return super.isEmpty(); // for the ExtraComponents
70  	}
71  
72      public boolean accept(MessageVisitor visitor, Location location) throws HL7Exception {
73          if (visitor.start(this, location)) {
74              Type[] types = getComponents();
75              for (int i = 0; i < types.length; i++) {
76                  Type t = getComponent(i);
77                  Location nextLocation = t.provideLocation(location, i + 1, location.getFieldRepetition());
78                  if (!t.accept(visitor, nextLocation)) break;
79              }
80              ExtraComponents ec = getExtraComponents();
81              for (int i = 0; i < ec.numComponents(); i++) {
82                  Variable v = ec.getComponent(i);
83                 Location nextLocation = v.provideLocation(location, i + types.length, -1);
84                 if (!v.accept(visitor, nextLocation)) break;
85              }
86          }
87          return visitor.end(this, location);
88      }
89  
90      @Override
91      public Location/../ca/uhn/hl7v2/Location.html#Location">Location provideLocation(Location location, int index, int repetition) {
92          return super.provideLocation(location, index, repetition).atComponentLevel(!location.isComponentLevel());
93      }
94  }