001package ca.uhn.fhir.context;
002
003/*
004 * #%L
005 * HAPI FHIR - Core Library
006 * %%
007 * Copyright (C) 2014 - 2021 Smile CDR, Inc.
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        private BaseRuntimeChildDefinition myReplacedParentDefinition;
035
036        public abstract IAccessor getAccessor();
037
038        public abstract BaseRuntimeElementDefinition<?> getChildByName(String theName);
039
040        public abstract BaseRuntimeElementDefinition<?> getChildElementDefinitionByDatatype(Class<? extends IBase> theType);
041
042        public abstract String getChildNameByDatatype(Class<? extends IBase> theDatatype);
043
044        public abstract String getElementName();
045
046        public String getExtensionUrl() {
047                return null;
048        }
049
050        public Object getInstanceConstructorArguments() {
051                return null;
052        }
053
054        public abstract int getMax();
055
056        public abstract int getMin();
057
058        public abstract IMutator getMutator();
059
060        public abstract Set<String> getValidChildNames();
061
062        public abstract boolean isSummary();
063
064        abstract void sealAndInitialize(FhirContext theContext, Map<Class<? extends IBase>, BaseRuntimeElementDefinition<?>> theClassToElementDefinitions);
065
066        @Override
067        public String toString() {
068                return getClass().getSimpleName() + "[" + getElementName() + "]";
069        }
070
071        public BaseRuntimeChildDefinition getReplacedParentDefinition() {
072                return myReplacedParentDefinition;
073        }
074
075        public void setReplacedParentDefinition(BaseRuntimeChildDefinition myReplacedParentDefinition) {
076                this.myReplacedParentDefinition = myReplacedParentDefinition;
077        }
078
079        public interface IAccessor {
080                List<IBase> getValues(IBase theTarget);
081
082                default <T extends IBase> Optional<T> getFirstValueOrNull(IBase theTarget) {
083                        return (Optional<T>) getValues(theTarget).stream().findFirst();
084                }
085        }
086
087        public interface IMutator {
088                void addValue(IBase theTarget, IBase theValue);
089
090                void setValue(IBase theTarget, IBase theValue);
091        }
092
093        BaseRuntimeElementDefinition<?> findResourceReferenceDefinition(Map<Class<? extends IBase>, BaseRuntimeElementDefinition<?>> theClassToElementDefinitions) {
094                for (Entry<Class<? extends IBase>, BaseRuntimeElementDefinition<?>> next : theClassToElementDefinitions.entrySet()) {
095                        if (IBaseReference.class.isAssignableFrom(next.getKey())) {
096                                return next.getValue();
097                        }
098                }
099                
100                // Shouldn't happen
101                throw new IllegalStateException("Unable to find reference type");
102        }
103
104        // public String getExtensionUrl() {
105        // return null;
106        // }
107}