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 "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      public AbstractType(Message message) {
53          extra = new ExtraComponents(message);
54          this.message = message;
55      }
56      
57      /** Returns the name of the type (used in XML encoding and profile checking)  */
58      public String getName() {
59          String longClassName = this.getClass().getName();
60          return longClassName.substring(longClassName.lastIndexOf('.') + 1);
61      }
62      
63      /** @see Type#getExtraComponents */
64      public ExtraComponents getExtraComponents() {
65          return this.extra;
66      }
67      
68      
69      /**
70       * @return the message to which this Type belongs
71       */
72      public Message getMessage() {
73          return message;
74      }
75  
76  
77      /**
78       * {@inheritDoc }
79       */
80      public void parse(String string) throws HL7Exception {
81          clear();
82  		getMessage().getParser().parse(this, string, EncodingCharacters.getInstance(getMessage()));
83      }
84  
85  
86      /**
87       * {@inheritDoc }
88       */
89      public String encode() throws HL7Exception {
90          return getMessage().getParser().doEncode(this, EncodingCharacters.getInstance(getMessage()));
91      }
92  
93  	/**
94  	 * {@inheritDoc }
95  	 */
96  	public void clear() {
97  		extra.clear();
98  	}
99  
100 	/**
101 	 * {@inheritDoc }
102 	 */	
103 	public boolean isEmpty() throws HL7Exception {
104 		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 		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         return theType.getClass().getSimpleName() +
123                 "[" +
124                 PipeParser.encode(theType, EncodingCharacters.defaultInstance()) +
125                 "]";
126 	}
127 
128     public Location/../ca/uhn/hl7v2/Location.html#Location">Location provideLocation(Location location, int index, int repetition) {
129         if (location.getField() < 0)
130             return new Location(location)
131                 .withField(index)
132                 .withFieldRepetition(repetition);
133         if (location.getComponent() < 0)
134             return new Location(location)
135                 .withComponent(index);
136         return new Location(location)
137             .withSubcomponent(index);
138     }
139 	
140 
141 }