001/**
002The 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.
004You may obtain a copy of the License at http://www.mozilla.org/MPL/
005Software distributed under the License is distributed on an "AS IS" basis,
006WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the
007specific language governing rights and limitations under the License.
008
009The Original Code is "AbstractComposite.java".  Description:
010"A partial implementation of Composite"
011
012The Initial Developer of the Original Code is University Health Network. Copyright (C)
0132012.  All Rights Reserved.
014
015Contributor(s): ______________________________________.
016
017Alternatively, the contents of this file may be used under the terms of the
018GNU General Public License (the  "GPL"), in which case the provisions of the GPL are
019applicable instead of those above.  If you wish to allow use of your version of this
020file only under the terms of the GPL and not to allow others to use your version
021of this file under the MPL, indicate your decision by deleting  the provisions above
022and replace  them with the notice and other provisions required by the GPL License.
023If you do not delete the provisions above, a recipient may use your version of
024this file under either the MPL or the GPL.
025*/
026package ca.uhn.hl7v2.model;
027
028import org.slf4j.Logger;
029import org.slf4j.LoggerFactory;
030
031import ca.uhn.hl7v2.HL7Exception;
032import ca.uhn.hl7v2.Location;
033
034
035public abstract class AbstractComposite extends AbstractType implements
036                Composite {
037
038        private static final long serialVersionUID = -2657103285266475699L;
039
040        protected Logger log;
041
042        public AbstractComposite(Message message) {
043                super(message);
044                log = LoggerFactory.getLogger(getClass());
045        }
046
047        @Override
048        public void clear() {
049                super.clear();
050                for (Type component : getComponents()) {
051                        component.clear();
052                }
053        }
054
055        protected <T extends Type> T getTyped(int idx, Class<T> type) {
056                try {
057                        @SuppressWarnings("unchecked") T ret = (T)getComponent(idx);
058                        return ret;
059                } catch (HL7Exception e) {
060                 log.error("Unexpected problem accessing known data type component - this is a bug.", e);
061                 throw new RuntimeException(e);
062                }
063        }
064
065        @Override
066        public boolean isEmpty() throws HL7Exception {
067                for (Type type : getComponents()) {
068                        if (!type.isEmpty()) return false;
069                }
070                return super.isEmpty(); // for the ExtraComponents
071        }
072
073    public boolean accept(MessageVisitor visitor, Location location) throws HL7Exception {
074        if (visitor.start(this, location)) {
075            Type[] types = getComponents();
076            for (int i = 0; i < types.length; i++) {
077                Type t = getComponent(i);
078                Location nextLocation = t.provideLocation(location, i + 1, location.getFieldRepetition());
079                if (!t.accept(visitor, nextLocation)) break;
080            }
081            ExtraComponents ec = getExtraComponents();
082            for (int i = 0; i < ec.numComponents(); i++) {
083                Variable v = ec.getComponent(i);
084               Location nextLocation = v.provideLocation(location, i + types.length, -1);
085               if (!v.accept(visitor, nextLocation)) break;
086            }
087        }
088        return visitor.end(this, location);
089    }
090
091    @Override
092    public Location provideLocation(Location location, int index, int repetition) {
093        return super.provideLocation(location, index, repetition).atComponentLevel(!location.isComponentLevel());
094    }
095}