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        private static final Logger log = LoggerFactory.getLogger(AbstractComposite.class);
041
042        public AbstractComposite(Message message) {
043                super(message);
044        }
045
046        @Override
047        public void clear() {
048                super.clear();
049                for (Type component : getComponents()) {
050                        component.clear();
051                }
052        }
053
054        protected <T extends Type> T getTyped(int idx, Class<T> type) {
055                try {
056                        @SuppressWarnings("unchecked") T ret = (T)getComponent(idx);
057                        return ret;
058                } catch (HL7Exception e) {
059                 log.error("Unexpected problem accessing known data type component - this is a bug. Class is " + getClass().getName(), e);
060                 throw new RuntimeException(e);
061                }
062        }
063
064        @Override
065        public boolean isEmpty() throws HL7Exception {
066                for (Type type : getComponents()) {
067                        if (!type.isEmpty()) return false;
068                }
069                return super.isEmpty(); // for the ExtraComponents
070        }
071
072    public boolean accept(MessageVisitor visitor, Location location) throws HL7Exception {
073        if (visitor.start(this, location)) {
074            Type[] types = getComponents();
075            for (int i = 0; i < types.length; i++) {
076                Type t = getComponent(i);
077                Location nextLocation = t.provideLocation(location, i + 1, location.getFieldRepetition());
078                if (!t.accept(visitor, nextLocation)) break;
079            }
080            ExtraComponents ec = getExtraComponents();
081            for (int i = 0; i < ec.numComponents(); i++) {
082                Variable v = ec.getComponent(i);
083               Location nextLocation = v.provideLocation(location, i + types.length, -1);
084               if (!v.accept(visitor, nextLocation)) break;
085            }
086        }
087        return visitor.end(this, location);
088    }
089
090    @Override
091    public Location provideLocation(Location location, int index, int repetition) {
092        return super.provideLocation(location, index, repetition).atComponentLevel(!location.isComponentLevel());
093    }
094}