io.sphere.sdk.products.attributes.AttributeExtraction Maven / Gradle / Ivy
package io.sphere.sdk.products.attributes;
import io.sphere.sdk.models.Base;
import io.sphere.sdk.producttypes.AttributeDefinitionContainer;
import javax.annotation.Nullable;
import java.util.Optional;
import java.util.function.Function;
public final class AttributeExtraction extends Base {
@Nullable
private final AttributeDefinition attributeDefinition;
private final Attribute attribute;
@Nullable
private final T value;
private AttributeExtraction(@Nullable final AttributeDefinition attributeDefinition, final Attribute attribute, @Nullable final T value) {
this.attributeDefinition = attributeDefinition;
this.attribute = attribute;
this.value = value;
}
public static AttributeExtraction of(final AttributeDefinitionContainer productType, final Attribute attribute) {
final AttributeDefinition attributeDefinition = productType.getAttribute(attribute.getName());
return of(attributeDefinition, attribute, null);
}
public static AttributeExtraction of(final AttributeDefinition attributeDefinition, final Attribute attribute) {
return of(attributeDefinition, attribute, null);
}
private static AttributeExtraction of(final AttributeDefinition attributeDefinition, final Attribute attribute, @Nullable final T value) {
return new AttributeExtraction<>(attributeDefinition, attribute, value);
}
private AttributeExtraction withValue(@Nullable final T value) {
return of(attributeDefinition, attribute, value);
}
public AttributeExtraction ifGuarded(final AttributeAccess extraction, final Function> function) {
return Optional.ofNullable(value).map(x -> this).orElseGet(() -> {
final T transformed = calculateValue(extraction).flatMap(value -> function.apply(value)).orElse(null);
return withValue(transformed);
});
}
public AttributeExtraction ifIs(final AttributeAccess extraction, final Function function) {
return ifIs(extraction, function, x -> true);
}
@SuppressWarnings("unchecked")
private Optional calculateValue(final AttributeAccess extraction) {
return Optional.ofNullable(attributeDefinition).flatMap(attributeDefinition -> calculateValue(extraction, attributeDefinition));
}
private Optional calculateValue(final AttributeAccess extraction, final AttributeDefinition attributeDefinition) {
if (extraction.canHandle(attributeDefinition)) {
final A value = attribute.getValue(extraction);
return Optional.of(value);
} else {
return Optional.empty();
}
}
public AttributeExtraction ifIs(final AttributeAccess extraction, final Function function, final java.util.function.Predicate guard) {
return Optional.ofNullable(value).map(x -> this).orElseGet(() -> {
final T newValue = calculateValue(extraction).flatMap(attributeValue -> {
Optional mappedValue = Optional.empty();
if (guard.test(attributeValue)) {
mappedValue = Optional.of(function.apply(attributeValue));
}
return mappedValue;
}).orElse(null);
return withValue(newValue);
});
}
public Optional findValue() {
return Optional.ofNullable(getValue());
}
@Nullable
public T getValue() {
return value;
}
}