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

com.sap.cloud.sdk.result.GenericObjectExtractor Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (c) 2020 SAP SE or an SAP affiliate company. All rights reserved.
 */

package com.sap.cloud.sdk.result;

import javax.annotation.Nonnull;

import lombok.RequiredArgsConstructor;

/**
 * Extractor trying to generically extract an object out of a given {@code ResultElement}.
 * 
 * @param 
 *            The type of the object to extract.
 */
@RequiredArgsConstructor
public class GenericObjectExtractor implements ObjectExtractor
{
    @Nonnull
    private final Class objectType;

    @Nonnull
    @Override
    public T extract( @Nonnull final ResultElement resultElement )
        throws UnsupportedOperationException
    {
        if( resultElement.isResultObject() ) {
            return resultElement.getAsObject().as(objectType);
        }

        if( resultElement.isResultPrimitive() ) {
            // TODO introduce concept for registering type converters
            // - try to use a matching converter
            // - if no converter found, try using PrimitiveBasedObjectExtractor

            return new PrimitiveBasedObjectExtractor<>(objectType).extract(resultElement);
        }

        throw new UnsupportedOperationException(
            "Failed to convert "
                + ResultElement.class.getSimpleName()
                + " to type "
                + objectType.getName()
                + ": "
                + resultElement
                + ".");
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy