io.sphere.sdk.products.AttributeContainerImpl Maven / Gradle / Ivy
package io.sphere.sdk.products;
import io.sphere.sdk.products.attributes.Attribute;
import io.sphere.sdk.products.attributes.NamedAttributeAccess;
import io.sphere.sdk.products.attributes.AttributeMapper;
import io.sphere.sdk.json.JsonException;
import io.sphere.sdk.models.Base;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import static java.lang.String.format;
class AttributeContainerImpl extends Base implements AttributeContainer {
private final List attributes;
protected AttributeContainerImpl(List attributes) {
this.attributes = attributes;
}
@Override
public List getAttributes() {
return attributes;
}
@Override
public Optional findAttribute(final NamedAttributeAccess 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.attributeMapper();
try {
return attribute.getValue(accessor);
} catch (final JsonException e) {
throw transformError(e, attributeName, mapper);
}
});
}
protected JsonException transformError(JsonException e, String attributeName, AttributeMapper> mapper) {
return new JsonException(format("AttributeContainer does not contain an attribute '%s' which can be mapped with %s.", attributeName, mapper), e.getCause());
}
public static AttributeContainerImpl of(List attributes) {
return new AttributeContainerImpl(attributes);
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy