001package ca.uhn.fhir.context;
002
003/*
004 * #%L
005 * HAPI FHIR - Core Library
006 * %%
007 * Copyright (C) 2014 - 2019 University Health Network
008 * %%
009 * Licensed under the Apache License, Version 2.0 (the "License");
010 * you may not use this file except in compliance with the License.
011 * You may obtain a copy of the License at
012 *
013 *      http://www.apache.org/licenses/LICENSE-2.0
014 *
015 * Unless required by applicable law or agreed to in writing, software
016 * distributed under the License is distributed on an "AS IS" BASIS,
017 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
018 * See the License for the specific language governing permissions and
019 * limitations under the License.
020 * #L%
021 */
022
023import java.util.List;
024import java.util.Map;
025import java.util.Optional;
026import java.util.Set;
027import java.util.Map.Entry;
028
029import org.hl7.fhir.instance.model.api.IBase;
030import org.hl7.fhir.instance.model.api.IBaseReference;
031
032public abstract class BaseRuntimeChildDefinition {
033
034        public abstract IAccessor getAccessor();
035
036        public abstract BaseRuntimeElementDefinition<?> getChildByName(String theName);
037
038        public abstract BaseRuntimeElementDefinition<?> getChildElementDefinitionByDatatype(Class<? extends IBase> theType);
039
040        public abstract String getChildNameByDatatype(Class<? extends IBase> theDatatype);
041
042        public abstract String getElementName();
043
044        public String getExtensionUrl() {
045                return null;
046        }
047
048        public Object getInstanceConstructorArguments() {
049                return null;
050        }
051
052        public abstract int getMax();
053
054        public abstract int getMin();
055
056        public abstract IMutator getMutator();
057
058        public abstract Set<String> getValidChildNames();
059
060        public abstract boolean isSummary();
061
062        abstract void sealAndInitialize(FhirContext theContext, Map<Class<? extends IBase>, BaseRuntimeElementDefinition<?>> theClassToElementDefinitions);
063
064        @Override
065        public String toString() {
066                return getClass().getSimpleName() + "[" + getElementName() + "]";
067        }
068
069        public interface IAccessor {
070                List<IBase> getValues(IBase theTarget);
071
072                default <T extends IBase> Optional<T> getFirstValueOrNull(IBase theTarget) {
073                        return (Optional<T>) getValues(theTarget).stream().findFirst();
074                }
075        }
076
077        public interface IMutator {
078                void addValue(IBase theTarget, IBase theValue);
079
080                void setValue(IBase theTarget, IBase theValue);
081        }
082
083        BaseRuntimeElementDefinition<?> findResourceReferenceDefinition(Map<Class<? extends IBase>, BaseRuntimeElementDefinition<?>> theClassToElementDefinitions) {
084                for (Entry<Class<? extends IBase>, BaseRuntimeElementDefinition<?>> next : theClassToElementDefinitions.entrySet()) {
085                        if (IBaseReference.class.isAssignableFrom(next.getKey())) {
086                                return next.getValue();
087                        }
088                }
089                
090                // Shouldn't happen
091                throw new IllegalStateException("Unable to find reference type");
092        }
093
094        // public String getExtensionUrl() {
095        // return null;
096        // }
097}