io.sphere.sdk.products.ProductVariantImpl Maven / Gradle / Ivy
package io.sphere.sdk.products;
import com.fasterxml.jackson.annotation.JsonCreator;
import io.sphere.sdk.json.JsonException;
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.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 int 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 JsonException e) {
throw enrich(format("ProductVariant(id=%s)", id), attributeName, mapper, e.getCause());
}
});
}
private JsonException enrich(final Object objectWithAttributes, final String attributeName, final AttributeMapper mapper, final Throwable cause) {
return new JsonException(format("%s does not contain an attribute '%s' which can be mapped with %s.", objectWithAttributes, attributeName, mapper), cause);
}
@Override
public List getImages() {
return images;
}
@Override
public Optional getAvailability() {
return availability;
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy