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 com.google.common.collect.Sets;
024import org.apache.commons.lang3.Validate;
025import org.hl7.fhir.instance.model.api.IBase;
026import org.hl7.fhir.instance.model.api.IBaseExtension;
027import org.hl7.fhir.instance.model.api.IBaseHasExtensions;
028
029import java.util.Collections;
030import java.util.HashMap;
031import java.util.List;
032import java.util.Map;
033import java.util.Set;
034
035public class RuntimeChildExt extends BaseRuntimeChildDefinition {
036
037        private Map<String, BaseRuntimeElementDefinition<?>> myNameToChild;
038        private Map<Class<? extends IBase>, BaseRuntimeElementDefinition<?>> myDatatypeToChild;
039        private Map<Class<? extends IBase>, String> myDatatypeToChildName;
040
041        @Override
042        public IAccessor getAccessor() {
043                return new IAccessor() {
044                        @SuppressWarnings({"unchecked", "rawtypes"})
045                        @Override
046                        public List<IBase> getValues(IBase theTarget) {
047                                List extension = ((IBaseHasExtensions) theTarget).getExtension();
048                                return Collections.unmodifiableList(extension);
049                        }
050                };
051        }
052
053        @Override
054        public BaseRuntimeElementDefinition<?> getChildByName(String theName) {
055                return myNameToChild.get(theName);
056        }
057
058        @Override
059        public BaseRuntimeElementDefinition<?> getChildElementDefinitionByDatatype(Class<? extends IBase> theType) {
060                return myDatatypeToChild.get(theType);
061        }
062
063        @Override
064        public String getChildNameByDatatype(Class<? extends IBase> theDatatype) {
065                return myDatatypeToChildName.get(theDatatype);
066        }
067
068        @Override
069        public String getElementName() {
070                return "extension";
071        }
072
073        @Override
074        public int getMax() {
075                return -1;
076        }
077
078        @Override
079        public int getMin() {
080                return 0;
081        }
082
083        @Override
084        public IMutator getMutator() {
085                return new IMutator() {
086                        @Override
087                        public void addValue(IBase theTarget, IBase theValue) {
088                                List extensions = ((IBaseHasExtensions) theTarget).getExtension();
089                                IBaseExtension<?, ?> value = (IBaseExtension<?, ?>) theValue;
090                                extensions.add(value);
091                        }
092
093                        @Override
094                        public void setValue(IBase theTarget, IBase theValue) {
095                                List extensions = ((IBaseHasExtensions) theTarget).getExtension();
096                                extensions.clear();
097                                if (theValue != null) {
098                                        IBaseExtension<?, ?> value = (IBaseExtension<?, ?>) theValue;
099                                        extensions.add(value);
100                                }
101                        }
102                };
103        }
104
105        @Override
106        public Set<String> getValidChildNames() {
107                return Sets.newHashSet("extension");
108        }
109
110        @Override
111        public boolean isSummary() {
112                return false;
113        }
114
115        @Override
116        void sealAndInitialize(FhirContext theContext, Map<Class<? extends IBase>, BaseRuntimeElementDefinition<?>> theClassToElementDefinitions) {
117                myNameToChild = new HashMap<>();
118                myDatatypeToChild = new HashMap<>();
119                myDatatypeToChildName = new HashMap<>();
120
121                for (BaseRuntimeElementDefinition<?> next : theClassToElementDefinitions.values()) {
122                        if (next.getName().equals("Extension")) {
123                                myNameToChild.put("extension", next);
124                                myDatatypeToChild.put(next.getImplementingClass(), next);
125                                myDatatypeToChildName.put(next.getImplementingClass(), "extension");
126                        }
127                }
128
129                Validate.isTrue(!myNameToChild.isEmpty());
130        }
131}