Search in sources :

Example 1 with IAccessor

use of ca.uhn.fhir.context.BaseRuntimeChildDefinition.IAccessor in project cqf-ruler by DBCG.

the class Reflections method getPrimitiveFunction.

/**
 * Generates a function to access a primitive property of the given
 * BaseType.
 *
 * @param <BaseType>       an IBase type
 * @param <ReturnType>     a return type for the Functions
 * @param theBaseTypeClass the class of a the IBase type
 * @param theChildName     to create a function for
 * @return a function for accessing the "theChildName" property of the
 *         BaseType
 */
@SuppressWarnings("unchecked")
public static <BaseType extends IBase, ReturnType> Function<BaseType, ReturnType> getPrimitiveFunction(final Class<? extends BaseType> theBaseTypeClass, String theChildName) {
    checkNotNull(theBaseTypeClass);
    checkNotNull(theChildName);
    IAccessor accessor = getAccessor(theBaseTypeClass, theChildName);
    return r -> {
        Optional<IBase> value = accessor.getFirstValueOrNull(r);
        if (!value.isPresent()) {
            return null;
        } else {
            return ((IPrimitiveType<ReturnType>) value.get()).getValue();
        }
    };
}
Also used : List(java.util.List) IAccessor(ca.uhn.fhir.context.BaseRuntimeChildDefinition.IAccessor) FhirContext(ca.uhn.fhir.context.FhirContext) IBase(org.hl7.fhir.instance.model.api.IBase) BaseRuntimeElementDefinition(ca.uhn.fhir.context.BaseRuntimeElementDefinition) IBaseResource(org.hl7.fhir.instance.model.api.IBaseResource) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) IPrimitiveType(org.hl7.fhir.instance.model.api.IPrimitiveType) Optional(java.util.Optional) RuntimeResourceDefinition(ca.uhn.fhir.context.RuntimeResourceDefinition) Function(java.util.function.Function) Optional(java.util.Optional) IAccessor(ca.uhn.fhir.context.BaseRuntimeChildDefinition.IAccessor)

Example 2 with IAccessor

use of ca.uhn.fhir.context.BaseRuntimeChildDefinition.IAccessor in project cqf-ruler by DBCG.

the class Reflections method getFunction.

/**
 * Generates a function to access a primitive property of the given
 * BaseType.
 *
 * @param <BaseType>       an IBase type
 * @param <ReturnType>     a return type for the Functions
 * @param theBaseTypeClass the class of a the IBase type
 * @param theChildName     to create a function for
 * @return a function for accessing the "theChildName" property of the
 *         BaseType
 */
@SuppressWarnings("unchecked")
public static <BaseType extends IBase, ReturnType extends List<? extends IBase>> Function<BaseType, ReturnType> getFunction(final Class<? extends BaseType> theBaseTypeClass, String theChildName) {
    checkNotNull(theBaseTypeClass);
    checkNotNull(theChildName);
    IAccessor accessor = getAccessor(theBaseTypeClass, theChildName);
    return r -> {
        return (ReturnType) accessor.getValues(r);
    };
}
Also used : List(java.util.List) IAccessor(ca.uhn.fhir.context.BaseRuntimeChildDefinition.IAccessor) FhirContext(ca.uhn.fhir.context.FhirContext) IBase(org.hl7.fhir.instance.model.api.IBase) BaseRuntimeElementDefinition(ca.uhn.fhir.context.BaseRuntimeElementDefinition) IBaseResource(org.hl7.fhir.instance.model.api.IBaseResource) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) IPrimitiveType(org.hl7.fhir.instance.model.api.IPrimitiveType) Optional(java.util.Optional) RuntimeResourceDefinition(ca.uhn.fhir.context.RuntimeResourceDefinition) Function(java.util.function.Function) IAccessor(ca.uhn.fhir.context.BaseRuntimeChildDefinition.IAccessor)

Example 3 with IAccessor

use of ca.uhn.fhir.context.BaseRuntimeChildDefinition.IAccessor in project cqf-ruler by DBCG.

the class ReflectionsTest method testAccessor.

@Test
public void testAccessor() {
    Library library = new Library().setName("test");
    IAccessor accessor = Reflections.getAccessor(library.getClass(), "name");
    Optional<IBase> opt = accessor.getFirstValueOrNull(library);
    @SuppressWarnings("unchecked") IPrimitiveType<String> value = (IPrimitiveType<String>) opt.get();
    assertEquals("test", value.getValue());
}
Also used : IPrimitiveType(org.hl7.fhir.instance.model.api.IPrimitiveType) Library(org.hl7.fhir.dstu3.model.Library) IAccessor(ca.uhn.fhir.context.BaseRuntimeChildDefinition.IAccessor) IBase(org.hl7.fhir.instance.model.api.IBase) Test(org.junit.jupiter.api.Test)

Aggregations

IAccessor (ca.uhn.fhir.context.BaseRuntimeChildDefinition.IAccessor)3 IBase (org.hl7.fhir.instance.model.api.IBase)3 IPrimitiveType (org.hl7.fhir.instance.model.api.IPrimitiveType)3 BaseRuntimeElementDefinition (ca.uhn.fhir.context.BaseRuntimeElementDefinition)2 FhirContext (ca.uhn.fhir.context.FhirContext)2 RuntimeResourceDefinition (ca.uhn.fhir.context.RuntimeResourceDefinition)2 Preconditions.checkNotNull (com.google.common.base.Preconditions.checkNotNull)2 List (java.util.List)2 Optional (java.util.Optional)2 Function (java.util.function.Function)2 IBaseResource (org.hl7.fhir.instance.model.api.IBaseResource)2 Library (org.hl7.fhir.dstu3.model.Library)1 Test (org.junit.jupiter.api.Test)1