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

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

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

package com.sap.cloud.sdk.result;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import javax.annotation.Nonnull;

import com.google.common.collect.Lists;
import com.google.common.collect.Sets;

import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.ToString;

/**
 * Default implementation of the {@code CollectedResultCollection} interface, allowing strongly typed access to specific
 * fields annotated with {@code ElementName} in elements of a {@code ResultCollection}.
 *
 * @see ResultCollection
 */
@RequiredArgsConstructor
@EqualsAndHashCode
@ToString
public class DefaultCollectedResultCollection implements CollectedResultCollection
{
    /**
     * The common name of all elements addressed by this collection.
     */
    @Getter
    private final String collectedElementName;

    @Getter
    private final Iterable resultElements;

    @Nonnull
    @Override
    public  List asList( @Nonnull final Class objectType )
        throws UnsupportedOperationException
    {
        return collectListFromObject(new GenericObjectExtractor<>(objectType));
    }

    @Nonnull
    private  List collectListFromObject( @Nonnull final ObjectExtractor extractor )
        throws UnsupportedOperationException
    {
        return collectListFromCollection(new GenericCollectionExtractor<>(extractor));
    }

    @Nonnull
    private  List collectListFromCollection( @Nonnull final CollectionExtractor, T> extractor )
        throws UnsupportedOperationException
    {
        return getCollection(extractor, Lists.newArrayList());
    }

    @Nonnull
    private , T> CollectionT getCollection(
        @Nonnull final CollectionExtractor extractor,
        @Nonnull final CollectionT collection )
        throws UnsupportedOperationException
    {
        try {
            for( final ResultElement element : resultElements ) {
                if( element.isResultObject() ) {
                    final ResultElement nestedElement = element.getAsObject().get(collectedElementName);
                    collection.addAll(extractor.extract(nestedElement));
                }
            }
        }
        catch( final Exception e ) {
            throw new UnsupportedOperationException(
                "Failed to collect nested elements with name " + collectedElementName + ".",
                e);
        }

        return collection;
    }

    @Nonnull
    @Override
    public  Set asSet( @Nonnull final Class objectType )
        throws UnsupportedOperationException
    {
        return collectSetFromObject(new GenericObjectExtractor<>(objectType));
    }

    @Nonnull
    private  Set collectSetFromObject( @Nonnull final ObjectExtractor extractor )
        throws UnsupportedOperationException
    {
        return collectSetFromCollection(resultElement -> Collections.singleton(extractor.extract(resultElement)));
    }

    @Nonnull
    private  Set collectSetFromCollection( @Nonnull final CollectionExtractor, T> extractor )
        throws UnsupportedOperationException
    {
        return getCollection(extractor, Sets.newHashSet());
    }

    @Nonnull
    @Override
    public List asBooleanList()
        throws UnsupportedOperationException
    {
        return collectListFromObject(new BooleanExtractor());
    }

    @Nonnull
    @Override
    public Set asBooleanSet()
        throws UnsupportedOperationException
    {
        return collectSetFromObject(new BooleanExtractor());
    }

    @Nonnull
    @Override
    public List asByteList()
        throws UnsupportedOperationException
    {
        return collectListFromObject(new ByteExtractor());
    }

    @Nonnull
    @Override
    public Set asByteSet()
        throws UnsupportedOperationException
    {
        return collectSetFromObject(new ByteExtractor());
    }

    @Nonnull
    @Override
    public List asCharacterList()
        throws UnsupportedOperationException
    {
        return collectListFromObject(new CharacterExtractor());
    }

    @Nonnull
    @Override
    public Set asCharacterSet()
        throws UnsupportedOperationException
    {
        return collectSetFromObject(new CharacterExtractor());
    }

    @Nonnull
    @Override
    public List asStringList()
        throws UnsupportedOperationException
    {
        return collectListFromObject(new StringExtractor());
    }

    @Nonnull
    @Override
    public Set asStringSet()
        throws UnsupportedOperationException
    {
        return collectSetFromObject(new StringExtractor());
    }

    @Nonnull
    @Override
    public List asIntegerList()
        throws UnsupportedOperationException
    {
        return collectListFromObject(new IntegerExtractor());
    }

    @Nonnull
    @Override
    public Set asIntegerSet()
        throws UnsupportedOperationException
    {
        return collectSetFromObject(new IntegerExtractor());
    }

    @Nonnull
    @Override
    public List asShortList()
        throws UnsupportedOperationException
    {
        return collectListFromObject(new ShortExtractor());
    }

    @Nonnull
    @Override
    public Set asShortSet()
        throws UnsupportedOperationException
    {
        return collectSetFromObject(new ShortExtractor());
    }

    @Nonnull
    @Override
    public List asLongList()
        throws UnsupportedOperationException
    {
        return collectListFromObject(new LongExtractor());
    }

    @Nonnull
    @Override
    public Set asLongSet()
        throws UnsupportedOperationException
    {
        return collectSetFromObject(new LongExtractor());
    }

    @Nonnull
    @Override
    public List asFloatList()
        throws UnsupportedOperationException
    {
        return collectListFromObject(new FloatExtractor());
    }

    @Nonnull
    @Override
    public Set asFloatSet()
        throws UnsupportedOperationException
    {
        return collectSetFromObject(new FloatExtractor());
    }

    @Nonnull
    @Override
    public List asDoubleList()
        throws UnsupportedOperationException
    {
        return collectListFromObject(new DoubleExtractor());
    }

    @Nonnull
    @Override
    public Set asDoubleSet()
        throws UnsupportedOperationException
    {
        return collectSetFromObject(new DoubleExtractor());
    }

    @Nonnull
    @Override
    public List asBigIntegerList()
        throws UnsupportedOperationException
    {
        return collectListFromObject(new BigIntegerExtractor());
    }

    @Nonnull
    @Override
    public Set asBigIntegerSet()
        throws UnsupportedOperationException
    {
        return collectSetFromObject(new BigIntegerExtractor());
    }

    @Nonnull
    @Override
    public List asBigDecimalList()
        throws UnsupportedOperationException
    {
        return collectListFromObject(new BigDecimalExtractor());
    }

    @Nonnull
    @Override
    public Set asBigDecimalSet()
        throws UnsupportedOperationException
    {
        return collectSetFromObject(new BigDecimalExtractor());
    }

    /**
     * @deprecated This method is deprecated and will be removed in the future.
     */
    @Deprecated
    public boolean isResultPrimitive()
    {
        return false;
    }

    /**
     * @deprecated This method is deprecated and will be removed in the future.
     */
    @Deprecated
    public boolean isResultCollection()
    {
        return true;
    }

    /**
     * @deprecated This method is deprecated and will be removed in the future.
     */
    @Deprecated
    public boolean isResultObject()
    {
        return false;
    }

    /**
     * @deprecated This method is deprecated and will be removed in the future.
     */
    @Deprecated
    @Nonnull
    public ResultPrimitive getAsPrimitive()
        throws UnsupportedOperationException
    {
        throw new UnsupportedOperationException("Cannot get a collection as primitive.");
    }

    /**
     * @deprecated This method is deprecated and will be removed in the future.
     */
    @Deprecated
    @Nonnull
    public ResultObject getAsObject()
        throws UnsupportedOperationException
    {
        throw new UnsupportedOperationException("Cannot get a collection as object.");
    }

    /**
     * @deprecated This method is deprecated and will be removed in the future.
     */
    @Deprecated
    public boolean asBoolean()
        throws UnsupportedOperationException
    {
        throw new UnsupportedOperationException("Cannot get a collection as primitive.");
    }

    /**
     * @deprecated This method is deprecated and will be removed in the future.
     */
    @Deprecated
    public byte asByte()
        throws UnsupportedOperationException
    {
        throw new UnsupportedOperationException("Cannot get a collection as primitive.");
    }

    /**
     * @deprecated This method is deprecated and will be removed in the future.
     */
    @Deprecated
    public char asCharacter()
        throws UnsupportedOperationException
    {
        throw new UnsupportedOperationException("Cannot get a collection as primitive.");
    }

    /**
     * @deprecated This method is deprecated and will be removed in the future.
     */
    @Deprecated
    @Nonnull
    public String asString()
        throws UnsupportedOperationException
    {
        throw new UnsupportedOperationException("Cannot get a collection as primitive.");
    }

    /**
     * @deprecated This method is deprecated and will be removed in the future.
     */
    @Deprecated
    public int asInteger()
        throws UnsupportedOperationException
    {
        throw new UnsupportedOperationException("Cannot get a collection as primitive.");
    }

    /**
     * @deprecated This method is deprecated and will be removed in the future.
     */
    @Deprecated
    public short asShort()
        throws UnsupportedOperationException
    {
        throw new UnsupportedOperationException("Cannot get a collection as primitive.");
    }

    /**
     * @deprecated This method is deprecated and will be removed in the future.
     */
    @Deprecated
    public long asLong()
        throws UnsupportedOperationException
    {
        throw new UnsupportedOperationException("Cannot get a collection as primitive.");
    }

    /**
     * @deprecated This method is deprecated and will be removed in the future.
     */
    @Deprecated
    public float asFloat()
        throws UnsupportedOperationException
    {
        throw new UnsupportedOperationException("Cannot get a collection as primitive.");
    }

    /**
     * @deprecated This method is deprecated and will be removed in the future.
     */
    @Deprecated
    public double asDouble()
        throws UnsupportedOperationException
    {
        throw new UnsupportedOperationException("Cannot get a collection as primitive.");
    }

    /**
     * @deprecated This method is deprecated and will be removed in the future.
     */
    @Deprecated
    @Nonnull
    public BigInteger asBigInteger()
        throws UnsupportedOperationException
    {
        throw new UnsupportedOperationException("Cannot get a collection as primitive.");
    }

    /**
     * @deprecated This method is deprecated and will be removed in the future.
     */
    @Deprecated
    @Nonnull
    public BigDecimal asBigDecimal()
        throws UnsupportedOperationException
    {
        throw new UnsupportedOperationException("Cannot get a collection as primitive.");
    }

    @Nonnull
    public Iterator iterator()
    {
        return resultElements.iterator();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy