All Downloads are FREE. Search and download functionalities are using the official Maven repository.

ca.uhn.fhir.model.primitive.BoundCodeDt Maven / Gradle / Ivy

There is a newer version: 7.4.5
Show newest version
/*
 * #%L
 * HAPI FHIR - Core Library
 * %%
 * Copyright (C) 2014 - 2024 Smile CDR, Inc.
 * %%
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * #L%
 */
package ca.uhn.fhir.model.primitive;

import ca.uhn.fhir.model.api.IValueSetEnumBinder;
import ca.uhn.fhir.model.api.annotation.DatatypeDef;
import org.apache.commons.lang3.Validate;

import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;

@DatatypeDef(name = "code", isSpecialization = true)
public class BoundCodeDt> extends CodeDt {

	private IValueSetEnumBinder myBinder;

	/**
	 * @deprecated This constructor is provided only for serialization support. Do not call it directly!
	 */
	@Deprecated
	public BoundCodeDt() {
		// nothing
	}

	public BoundCodeDt(IValueSetEnumBinder theBinder) {
		Validate.notNull(theBinder, "theBinder must not be null");
		myBinder = theBinder;
	}

	public BoundCodeDt(IValueSetEnumBinder theBinder, T theValue) {
		Validate.notNull(theBinder, "theBinder must not be null");
		myBinder = theBinder;
		setValueAsEnum(theValue);
	}

	public IValueSetEnumBinder getBinder() {
		return myBinder;
	}

	public T getValueAsEnum() {
		Validate.notNull(
				myBinder, "This object does not have a binder. Constructor BoundCodeDt() should not be called!");
		T retVal = myBinder.fromCodeString(getValue());
		if (retVal == null) {
			// TODO: throw special exception type?
		}
		return retVal;
	}

	@SuppressWarnings("unchecked")
	@Override
	public void readExternal(ObjectInput theIn) throws IOException, ClassNotFoundException {
		super.readExternal(theIn);
		myBinder = (IValueSetEnumBinder) theIn.readObject();
	}

	public void setValueAsEnum(T theValue) {
		Validate.notNull(
				myBinder, "This object does not have a binder. Constructor BoundCodeDt() should not be called!");
		if (theValue == null) {
			setValue(null);
		} else {
			setValue(myBinder.toCodeString(theValue));
		}
	}

	@Override
	public void writeExternal(ObjectOutput theOut) throws IOException {
		super.writeExternal(theOut);
		theOut.writeObject(myBinder);
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy