Coverage Report - ca.uhn.hl7v2.model.AbstractComposite
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractComposite
78%
22/28
83%
15/18
3
 
 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  
         protected Logger log;
 41  
 
 42  
         public AbstractComposite(Message message) {
 43  137724
                 super(message);
 44  137724
                 log = LoggerFactory.getLogger(getClass());
 45  137724
         }
 46  
 
 47  
         @Override
 48  
         public void clear() {
 49  255
                 super.clear();
 50  1330
                 for (Type component : getComponents()) {
 51  1075
                         component.clear();
 52  
                 }
 53  255
         }
 54  
 
 55  
         protected <T extends Type> T getTyped(int idx, Class<T> type) {
 56  
                 try {
 57  1310
                         @SuppressWarnings("unchecked") T ret = (T)getComponent(idx);
 58  1310
                         return ret;
 59  0
                 } catch (HL7Exception e) {
 60  0
                  log.error("Unexpected problem accessing known data type component - this is a bug.", e);
 61  0
                  throw new RuntimeException(e);
 62  
                 }
 63  
         }
 64  
 
 65  
         @Override
 66  
         public boolean isEmpty() throws HL7Exception {
 67  3140
                 for (Type type : getComponents()) {
 68  2820
                         if (!type.isEmpty()) return false;
 69  
                 }
 70  320
                 return super.isEmpty(); // for the ExtraComponents
 71  
         }
 72  
 
 73  
     public boolean accept(MessageVisitor visitor, Location location) throws HL7Exception {
 74  360
         if (visitor.start(this, location)) {
 75  350
             Type[] types = getComponents();
 76  1615
             for (int i = 0; i < types.length; i++) {
 77  1380
                 Type t = getComponent(i);
 78  1380
                 Location nextLocation = t.provideLocation(location, i + 1, location.getFieldRepetition());
 79  1380
                 if (!t.accept(visitor, nextLocation)) break;
 80  
             }
 81  350
             ExtraComponents ec = getExtraComponents();
 82  350
             for (int i = 0; i < ec.numComponents(); i++) {
 83  0
                 Variable v = ec.getComponent(i);
 84  0
                Location nextLocation = v.provideLocation(location, i + types.length, -1);
 85  0
                if (!v.accept(visitor, nextLocation)) break;
 86  
             }
 87  
         }
 88  360
         return visitor.end(this, location);
 89  
     }
 90  
 
 91  
     @Override
 92  
     public Location provideLocation(Location location, int index, int repetition) {
 93  100
         return super.provideLocation(location, index, repetition).atComponentLevel(!location.isComponentLevel());
 94  
     }
 95  
 }