
001package ca.uhn.fhir.model.api; 002 003/* 004 * #%L 005 * HAPI FHIR - Core Library 006 * %% 007 * Copyright (C) 2014 - 2023 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 ca.uhn.fhir.i18n.Msg; 024import ca.uhn.fhir.model.api.annotation.Child; 025import ca.uhn.fhir.model.api.annotation.DatatypeDef; 026import ca.uhn.fhir.model.primitive.StringDt; 027import org.apache.commons.lang3.Validate; 028import org.apache.commons.lang3.builder.ToStringBuilder; 029import org.apache.commons.lang3.builder.ToStringStyle; 030import org.hl7.fhir.instance.model.api.IBaseDatatype; 031import org.hl7.fhir.instance.model.api.IBaseExtension; 032 033import java.util.ArrayList; 034import java.util.List; 035 036@DatatypeDef(name = "Extension") 037public class ExtensionDt extends BaseIdentifiableElement implements ICompositeDatatype, IBaseExtension<ExtensionDt, IDatatype> { 038 039 private static final long serialVersionUID = 6399491332783085935L; 040 041 private boolean myModifier; 042 043 @Child(name="url", type=StringDt.class, order=0, min=1, max=1) 044 private StringDt myUrl; 045 046 @Child(name = "value", type = IDatatype.class, order = 1, min = 0, max = 1) 047 private IBaseDatatype myValue; 048 049 public ExtensionDt() { 050 } 051 052 public ExtensionDt(boolean theIsModifier) { 053 myModifier = theIsModifier; 054 } 055 056 public ExtensionDt(boolean theIsModifier, String theUrl) { 057 Validate.notEmpty(theUrl, "URL must be populated"); 058 059 myModifier = theIsModifier; 060 myUrl = new StringDt(theUrl); 061 } 062 063 public ExtensionDt(boolean theIsModifier, String theUrl, IBaseDatatype theValue) { 064 Validate.notEmpty(theUrl, "URL must be populated"); 065 Validate.notNull(theValue, "Value must not be null"); 066 067 myModifier = theIsModifier; 068 myUrl = new StringDt(theUrl); 069 myValue=theValue; 070 } 071 072 /** 073 * Returns the URL for this extension. 074 * <p> 075 * Note that before HAPI 0.9 this method returned a {@link StringDt} but as of 076 * HAPI 0.9 this method returns a plain string. This was changed because it does not make sense to use a StringDt here 077 * since the URL itself can not contain extensions and it was therefore misleading. 078 * </p> 079 */ 080 @Override 081 public String getUrl() { 082 return myUrl != null ? myUrl.getValue() : null; 083 } 084 085 /** 086 * Retained for backward compatibility 087 * 088 * @see ExtensionDt#getUrl() 089 */ 090 public String getUrlAsString() { 091 return getUrl(); 092 } 093 094 /** 095 * Returns the value of this extension, if one exists. 096 * <p> 097 * Note that if this extension contains extensions (instead of a datatype) then <b>this method will return null</b>. In that case, you must use {@link #getUndeclaredExtensions()} and 098 * {@link #getUndeclaredModifierExtensions()} to retrieve the child extensions. 099 * </p> 100 */ 101 @Override 102 public IBaseDatatype getValue() { 103 return myValue; 104 } 105 106 /** 107 * Returns the value of this extension, casted to a primitive datatype. This is a convenience method which should only be called if you are sure that the value for this particular extension will 108 * be a primitive. 109 * <p> 110 * Note that if this extension contains extensions (instead of a datatype) then <b>this method will return null</b>. In that case, you must use {@link #getUndeclaredExtensions()} and 111 * {@link #getUndeclaredModifierExtensions()} to retrieve the child extensions. 112 * </p> 113 * 114 * @throws ClassCastException 115 * If the value of this extension is not a primitive datatype 116 */ 117 public IPrimitiveDatatype<?> getValueAsPrimitive() { 118 if (!(getValue() instanceof IPrimitiveDatatype)) { 119 throw new ClassCastException(Msg.code(1887) + "Extension with URL["+myUrl+"] can not be cast to primitive type, type is: "+ getClass().getCanonicalName()); 120 } 121 return (IPrimitiveDatatype<?>) getValue(); 122 } 123 124 @Override 125 public boolean isEmpty() { 126 return super.isBaseEmpty() && (myValue == null || myValue.isEmpty()); 127 } 128 129 public boolean isModifier() { 130 return myModifier; 131 } 132 133 public void setModifier(boolean theModifier) { 134 myModifier = theModifier; 135 } 136 137 @Override 138 public ExtensionDt setUrl(String theUrl) { 139 myUrl = theUrl != null ? new StringDt(theUrl) : myUrl; 140 return this; 141 } 142 143 public ExtensionDt setUrl(StringDt theUrl) { 144 myUrl = theUrl; 145 return this; 146 } 147 148 @Override 149 public ExtensionDt setValue(IBaseDatatype theValue) { 150 myValue = theValue; 151 return this; 152 } 153 154 @Override 155 @Deprecated //override deprecated method 156 public <T extends IElement> List<T> getAllPopulatedChildElementsOfType(Class<T> theType) { 157 return new ArrayList<T>(); 158 } 159 160 @Override 161 public List<ExtensionDt> getExtension() { 162 return getAllUndeclaredExtensions(); 163 } 164 165 @Override 166 public String toString() { 167 ToStringBuilder retVal = new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE); 168 retVal.append("url", getUrl()); 169 retVal.append("value", getValue()); 170 return retVal.build(); 171 } 172 173 174 175}