001/** 002 * The contents of this file are subject to the Mozilla Public License Version 1.1 003 * (the "License"); you may not use this file except in compliance with the License. 004 * You may obtain a copy of the License at http://www.mozilla.org/MPL/ 005 * Software distributed under the License is distributed on an "AS IS" basis, 006 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the 007 * specific language governing rights and limitations under the License. 008 * 009 * The Original Code is "AbstractType.java". Description: 010 * 011 * "An abstract Type that provides a default implementation of getName()" 012 * 013 * The Initial Developer of the Original Code is University Health Network. Copyright (C) 014 * 2001. All Rights Reserved. 015 * 016 * Contributor(s): ______________________________________. 017 * 018 * Alternatively, the contents of this file may be used under the terms of the 019 * GNU General Public License (the ?GPL?), in which case the provisions of the GPL are 020 * applicable instead of those above. If you wish to allow use of your version of this 021 * file only under the terms of the GPL and not to allow others to use your version 022 * of this file under the MPL, indicate your decision by deleting the provisions above 023 * and replace them with the notice and other provisions required by the GPL License. 024 * If you do not delete the provisions above, a recipient may use your version of 025 * this file under either the MPL or the GPL. 026 * 027 */ 028 029package ca.uhn.hl7v2.model; 030 031import ca.uhn.hl7v2.HL7Exception; 032import ca.uhn.hl7v2.parser.EncodingCharacters; 033import ca.uhn.hl7v2.parser.PipeParser; 034 035/** 036 * An abstract Type that provides a default implementation of getName(). 037 * 038 * @author Bryan Tripp 039 */ 040public abstract class AbstractType implements Type { 041 042 private static final long serialVersionUID = -6976260024197429201L; 043 044 private final ExtraComponents extra; 045 private final Message message; 046 047 /** 048 * Creates a new instance of AbstractType 049 * @param message message to which this type belongs 050 */ 051 public AbstractType(Message message) { 052 extra = new ExtraComponents(message); 053 this.message = message; 054 } 055 056 /** Returns the name of the type (used in XML encoding and profile checking) */ 057 public String getName() { 058 String longClassName = this.getClass().getName(); 059 return longClassName.substring(longClassName.lastIndexOf('.') + 1); 060 } 061 062 /** @see Type#getExtraComponents */ 063 public ExtraComponents getExtraComponents() { 064 return this.extra; 065 } 066 067 068 /** 069 * @return the message to which this Type belongs 070 */ 071 public Message getMessage() { 072 return message; 073 } 074 075 076 /** 077 * {@inheritDoc } 078 */ 079 public void parse(String string) throws HL7Exception { 080 clear(); 081 getMessage().getParser().parse(this, string, EncodingCharacters.getInstance(getMessage())); 082 } 083 084 085 /** 086 * {@inheritDoc } 087 */ 088 public String encode() throws HL7Exception { 089 return getMessage().getParser().doEncode(this, EncodingCharacters.getInstance(getMessage())); 090 } 091 092 /** 093 * {@inheritDoc } 094 */ 095 public void clear() { 096 extra.clear(); 097 } 098 099 /** 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}