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.Location;
033import ca.uhn.hl7v2.parser.EncodingCharacters;
034import ca.uhn.hl7v2.parser.PipeParser;
035
036/**
037 * An abstract Type that provides a default implementation of getName(). 
038 * 
039 * @author Bryan Tripp
040 */
041public abstract class AbstractType implements Type {
042
043        private static final long serialVersionUID = -6976260024197429201L;
044        
045        private final ExtraComponents extra;
046    private final Message message;
047    
048    /** 
049     * Creates a new instance of AbstractType
050     * @param message message to which this type belongs 
051     */
052    public AbstractType(Message message) {
053        extra = new ExtraComponents(message);
054        this.message = message;
055    }
056    
057    /** Returns the name of the type (used in XML encoding and profile checking)  */
058    public String getName() {
059        String longClassName = this.getClass().getName();
060        return longClassName.substring(longClassName.lastIndexOf('.') + 1);
061    }
062    
063    /** @see Type#getExtraComponents */
064    public ExtraComponents getExtraComponents() {
065        return this.extra;
066    }
067    
068    
069    /**
070     * @return the message to which this Type belongs
071     */
072    public Message getMessage() {
073        return message;
074    }
075
076
077    /**
078     * {@inheritDoc }
079     */
080    public void parse(String string) throws HL7Exception {
081        clear();
082                getMessage().getParser().parse(this, string, EncodingCharacters.getInstance(getMessage()));
083    }
084
085
086    /**
087     * {@inheritDoc }
088     */
089    public String encode() throws HL7Exception {
090        return getMessage().getParser().doEncode(this, EncodingCharacters.getInstance(getMessage()));
091    }
092
093        /**
094         * {@inheritDoc }
095         */
096        public void clear() {
097                extra.clear();
098        }
099
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                StringBuilder b = new StringBuilder();
123                b.append(theType.getClass().getSimpleName());
124                b.append("[");
125                b.append(PipeParser.encode(theType, EncodingCharacters.defaultInstance()));
126                b.append("]");
127                return b.toString();
128        }
129
130    public Location provideLocation(Location location, int index, int repetition) {
131        if (location.getField() < 0)
132            return new Location(location)
133                .withField(index)
134                .withFieldRepetition(repetition);
135        if (location.getComponent() < 0)
136            return new Location(location)
137                .withComponent(index);
138        return new Location(location)
139            .withSubcomponent(index);
140    }
141        
142
143}