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

io.sphere.sdk.products.ProductVariantImpl Maven / Gradle / Ivy

There is a newer version: 1.0.0-M12
Show newest version
package io.sphere.sdk.products;

import com.fasterxml.jackson.annotation.JsonCreator;
import io.sphere.sdk.models.Base;
import io.sphere.sdk.attributes.Attribute;
import io.sphere.sdk.attributes.AttributeGetter;
import io.sphere.sdk.attributes.AttributeMapper;
import io.sphere.sdk.attributes.AttributeMappingException;
import io.sphere.sdk.models.Image;

import java.util.List;
import java.util.Objects;
import java.util.Optional;

import static java.lang.String.format;

class ProductVariantImpl extends Base implements ProductVariant {
    private final int id;
    private final Optional sku;
    private final List prices;
    private final List attributes;
    private final List images;
    private final Optional availability;

    @JsonCreator
    ProductVariantImpl(final int id, final Optional sku, final List prices,
                       final List attributes, final List images,
                       final Optional availability) {
        this.id = id;
        this.sku = sku;
        this.prices = prices;
        this.attributes = attributes;
        this.images = images;
        this.availability = availability;
    }

    @Override
    public long getId() {
        return id;
    }

    @Override
    public Optional getSku() {
        return sku;
    }

    @Override
    public List getPrices() {
        return prices;
    }

    @Override
    public List getAttributes() {
        return attributes;
    }

    @Override
    public  Optional getAttribute(final AttributeGetter accessor) {
        final String attributeName = accessor.getName();
        final Optional attributeOption = getAttributes().stream()
                .filter(a -> Objects.equals(attributeName, a.getName()))
                .findFirst();
        return attributeOption.map(attribute -> {
            final AttributeMapper mapper = accessor.getMapper();
            try {
                return attribute.getValue(mapper);
            } catch (final AttributeMappingException e) {
                throw new AttributeMappingException(format("ProductVariant(id=%s)", id), attributeName, mapper, e.getCause());
            }
        });
    }

    @Override
    public List getImages() {
        return images;
    }

    @Override
    public Optional getAvailability() {
        return availability;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy