Coverage Report - ca.uhn.hl7v2.model.AbstractType
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractType
90%
28/31
75%
3/4
1.364
 
 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 "AbstractType.java".  Description:
 10  
  * 
 11  
  * "An abstract Type that provides a default implementation of getName()" 
 12  
  *
 13  
  * The Initial Developer of the Original Code is University Health Network. Copyright (C)
 14  
  * 2001.  All Rights Reserved.
 15  
  *
 16  
  * Contributor(s): ______________________________________.
 17  
  *
 18  
  * Alternatively, the contents of this file may be used under the terms of the
 19  
  * GNU General Public License (the  �GPL�), in which case the provisions of the GPL are
 20  
  * applicable instead of those above.  If you wish to allow use of your version of this
 21  
  * file only under the terms of the GPL and not to allow others to use your version
 22  
  * of this file under the MPL, indicate your decision by deleting  the provisions above
 23  
  * and replace  them with the notice and other provisions required by the GPL License.
 24  
  * If you do not delete the provisions above, a recipient may use your version of
 25  
  * this file under either the MPL or the GPL.
 26  
  *
 27  
  */
 28  
 
 29  
 package ca.uhn.hl7v2.model;
 30  
 
 31  
 import ca.uhn.hl7v2.HL7Exception;
 32  
 import ca.uhn.hl7v2.Location;
 33  
 import ca.uhn.hl7v2.parser.EncodingCharacters;
 34  
 import ca.uhn.hl7v2.parser.PipeParser;
 35  
 
 36  
 /**
 37  
  * An abstract Type that provides a default implementation of getName(). 
 38  
  * 
 39  
  * @author Bryan Tripp
 40  
  */
 41  
 public abstract class AbstractType implements Type {
 42  
 
 43  
         private static final long serialVersionUID = -6976260024197429201L;
 44  
         
 45  
         private final ExtraComponents extra;
 46  
     private final Message message;
 47  
     
 48  
     /** 
 49  
      * Creates a new instance of AbstractType
 50  
      * @param message message to which this type belongs 
 51  
      */
 52  778827
     public AbstractType(Message message) {
 53  778827
         extra = new ExtraComponents(message);
 54  778827
         this.message = message;
 55  778827
     }
 56  
     
 57  
     /** Returns the name of the type (used in XML encoding and profile checking)  */
 58  
     public String getName() {
 59  177968
         String longClassName = this.getClass().getName();
 60  177968
         return longClassName.substring(longClassName.lastIndexOf('.') + 1);
 61  
     }
 62  
     
 63  
     /** @see Type#getExtraComponents */
 64  
     public ExtraComponents getExtraComponents() {
 65  594643
         return this.extra;
 66  
     }
 67  
     
 68  
     
 69  
     /**
 70  
      * @return the message to which this Type belongs
 71  
      */
 72  
     public Message getMessage() {
 73  802881
         return message;
 74  
     }
 75  
 
 76  
 
 77  
     /**
 78  
      * {@inheritDoc }
 79  
      */
 80  
     public void parse(String string) throws HL7Exception {
 81  100
         clear();
 82  100
                 getMessage().getParser().parse(this, string, EncodingCharacters.getInstance(getMessage()));
 83  100
     }
 84  
 
 85  
 
 86  
     /**
 87  
      * {@inheritDoc }
 88  
      */
 89  
     public String encode() throws HL7Exception {
 90  90
         return getMessage().getParser().doEncode(this, EncodingCharacters.getInstance(getMessage()));
 91  
     }
 92  
 
 93  
         /**
 94  
          * {@inheritDoc }
 95  
          */
 96  
         public void clear() {
 97  1415
                 extra.clear();
 98  1415
         }
 99  
 
 100  
         /**
 101  
          * {@inheritDoc }
 102  
          */        
 103  
         public boolean isEmpty() throws HL7Exception {
 104  2270
                 return extra.isEmpty();
 105  
         }
 106  
 
 107  
         /**
 108  
          * Returns the datatype and attempts to pipe-encode it. For example, a string implementation
 109  
          * might return "ST[Value^Value2]". This is only intended for logging/debugging purposes.
 110  
          */
 111  
         @Override
 112  
         public String toString() {
 113  5
                 return toString(this);
 114  
         }
 115  
 
 116  
         
 117  
         /**
 118  
          * Returns the datatype and attempts to pipe-encode it. For example, a string implementation
 119  
          * might return "ST[Value^Value2]". This is only intended for logging/debugging purposes.
 120  
          */
 121  
         static String toString(Type theType) {
 122  5
                 StringBuilder b = new StringBuilder();
 123  5
                 b.append(theType.getClass().getSimpleName());
 124  5
                 b.append("[");
 125  5
                 b.append(PipeParser.encode(theType, EncodingCharacters.defaultInstance()));
 126  5
                 b.append("]");
 127  5
                 return b.toString();
 128  
         }
 129  
 
 130  
     public Location provideLocation(Location location, int index, int repetition) {
 131  1380
         if (location.getField() < 0)
 132  0
             return new Location(location)
 133  0
                 .withField(index)
 134  0
                 .withFieldRepetition(repetition);
 135  1380
         if (location.getComponent() < 0)
 136  2180
             return new Location(location)
 137  1090
                 .withComponent(index);
 138  580
         return new Location(location)
 139  290
             .withSubcomponent(index);
 140  
     }
 141  
         
 142  
 
 143  
 }