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.parser.EncodingCharacters;
33  import ca.uhn.hl7v2.parser.PipeParser;
34  
35  /**
36   * An abstract Type that provides a default implementation of getName(). 
37   * 
38   * @author Bryan Tripp
39   */
40  public abstract class AbstractType implements Type {
41  
42  	private static final long serialVersionUID = -6976260024197429201L;
43  	
44  	private final ExtraComponents extra;
45      private final Message message;
46      
47      /** 
48       * Creates a new instance of AbstractType
49       * @param message message to which this type belongs 
50       */
51      public AbstractType(Message message) {
52          extra = new ExtraComponents(message);
53          this.message = message;
54      }
55      
56      /** Returns the name of the type (used in XML encoding and profile checking)  */
57      public String getName() {
58          String longClassName = this.getClass().getName();
59          return longClassName.substring(longClassName.lastIndexOf('.') + 1);
60      }
61      
62      /** @see Type#getExtraComponents */
63      public ExtraComponents getExtraComponents() {
64          return this.extra;
65      }
66      
67      
68      /**
69       * @return the message to which this Type belongs
70       */
71      public Message getMessage() {
72          return message;
73      }
74  
75  
76      /**
77       * {@inheritDoc }
78       */
79      public void parse(String string) throws HL7Exception {
80          clear();
81  		getMessage().getParser().parse(this, string, EncodingCharacters.getInstance(getMessage()));
82      }
83  
84  
85      /**
86       * {@inheritDoc }
87       */
88      public String encode() throws HL7Exception {
89          return getMessage().getParser().doEncode(this, EncodingCharacters.getInstance(getMessage()));
90      }
91  
92  	/**
93  	 * {@inheritDoc }
94  	 */
95  	public void clear() {
96  		extra.clear();
97  	}
98  
99  	/**
100 	 * {@inheritDoc }
101 	 */	
102 	public boolean isEmpty() {
103 		return extra.numComponents() > 0;
104 	}
105 
106 	/**
107 	 * Returns the datatype and attempts to pipe-encode it. For example, a string implementation
108 	 * might return "ST[Value^Value2]". This is only intended for logging/debugging purposes.
109 	 */
110 	@Override
111 	public String toString() {
112 		return toString(this);
113 	}
114 
115 	
116 	/**
117 	 * Returns the datatype and attempts to pipe-encode it. For example, a string implementation
118 	 * might return "ST[Value^Value2]". This is only intended for logging/debugging purposes.
119 	 */
120 	static String toString(Type theType) {
121 		StringBuilder b = new StringBuilder();
122 		b.append(theType.getClass().getSimpleName());
123 		b.append("[");
124 		b.append(PipeParser.encode(theType, EncodingCharacters.defaultInstance()));
125 		b.append("]");
126 		return b.toString();
127 	}
128 
129 
130 	
131 
132 }