io.sphere.sdk.attributes.AttributeAccess Maven / Gradle / Ivy
package io.sphere.sdk.attributes;
import com.fasterxml.jackson.core.type.TypeReference;
import io.sphere.sdk.categories.Category;
import io.sphere.sdk.channels.Channel;
import io.sphere.sdk.models.*;
import io.sphere.sdk.products.Product;
import io.sphere.sdk.producttypes.ProductType;
import javax.money.MonetaryAmount;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalTime;
import java.util.Set;
import java.util.function.Predicate;
import static io.sphere.sdk.models.TypeReferences.*;
public final class AttributeAccess extends Base {
private final AttributeMapper attributeMapper;
private final java.util.function.Predicate canHandle;
private AttributeAccess(final AttributeMapper attributeMapper, final Predicate canHandle) {
this.attributeMapper = attributeMapper;
this.canHandle = canHandle;
}
public static AttributeAccess ofBoolean() {
return ofPrimitive(booleanTypeReference(), BooleanAttributeDefinition.class);
}
public static AttributeAccess> ofBooleanSet() {
return ofSet(BooleanType.class, new TypeReference>() {
});
}
public static AttributeAccess ofString() {
return ofPrimitive(stringTypeReference(), TextAttributeDefinition.class);
}
public static AttributeAccess> ofStringSet() {
return ofSet(TextType.class, new TypeReference>() {
});
}
public static AttributeAccess ofText() {
return ofString();
}
public static AttributeAccess> ofTextSet() {
return ofStringSet();
}
public static AttributeAccess ofLocalizedStrings() {
return ofPrimitive(LocalizedStrings.typeReference(), LocalizedTextAttributeDefinition.class);
}
public static AttributeAccess> ofLocalizedStringsSet() {
return ofSet(LocalizedTextType.class, new TypeReference>() {});
}
public static AttributeAccess ofPlainEnumValue() {
return ofEnumLike(PlainEnumValue.typeReference(), EnumAttributeDefinition.class);
}
public static AttributeAccess> ofPlainEnumValueSet() {
return ofEnumLikeSet(EnumType.class, new TypeReference>() {});
}
public static AttributeAccess ofLocalizedEnumValue() {
return ofEnumLike(LocalizedEnumValue.typeReference(), LocalizedEnumAttributeDefinition.class);
}
public static AttributeAccess> ofLocalizedEnumValueSet() {
return ofEnumLikeSet(LocalizedEnumType.class, new TypeReference>() {});
}
public static AttributeAccess ofDouble() {
return ofPrimitive(doubleTypeReference(), NumberAttributeDefinition.class);
}
public static AttributeAccess> ofDoubleSet() {
return ofSet(NumberType.class, new TypeReference>() {});
}
public static AttributeAccess ofMoney() {
return ofPrimitive(monetaryAmountTypeReference(), MoneyAttributeDefinition.class);
}
public static AttributeAccess> ofMoneySet() {
return ofSet(MoneyType.class, new TypeReference>() {});
}
public static AttributeAccess ofDate() {
return ofPrimitive(localDateTypeReference(), DateAttributeDefinition.class);
}
public static AttributeAccess> ofDateSet() {
return ofSet(DateType.class, new TypeReference>() {});
}
public static AttributeAccess ofTime() {
return ofPrimitive(localTimeTypeReference(), TimeAttributeDefinition.class);
}
public static AttributeAccess> ofTimeSet() {
return ofSet(TimeType.class, new TypeReference>() {
});
}
public static AttributeAccess ofDateTime() {
return ofPrimitive(instantTypeReference(), DateTimeAttributeDefinition.class);
}
public static AttributeAccess> ofDateTimeSet() {
return ofSet(DateTimeType.class, new TypeReference>() {
});
}
public static AttributeAccess> ofProductReference() {
return OfReferenceType(ReferenceType.ofProduct());
}
public static AttributeAccess>> ofProductReferenceSet() {
return ofSet(ReferenceType.ofProduct(), new TypeReference>>() {
});
}
public static AttributeAccess> ofProductTypeReference() {
return OfReferenceType(ReferenceType.ofProductType());
}
public static AttributeAccess>> ofProductTypeReferenceSet() {
return ofSet(ReferenceType.ofProductType(), new TypeReference>>() {
});
}
public static AttributeAccess> ofCategoryReference() {
return OfReferenceType(ReferenceType.ofCategory());
}
public static AttributeAccess>> ofCategoryReferenceSet() {
return ofSet(ReferenceType.ofCategory(), new TypeReference>>() {
});
}
public static AttributeAccess> ofChannelReference() {
return OfReferenceType(ReferenceType.ofChannel());
}
public static AttributeAccess>> ofChannelReferenceSet() {
return ofSet(ReferenceType.ofChannel(), new TypeReference>>() {
});
}
public AttributeGetterSetter getterSetter(final String name) {
return ofName(name);
}
public AttributeGetterSetter ofName(final String name) {
return AttributeGetterSetter.of(name, attributeMapper);
}
public AttributeGetter getter(final String name) {
return this.ofName(name);
}
public AttributeSetter setter(final String name) {
return this.ofName(name);
}
public AttributeMapper attributeMapper() {
return attributeMapper;
}
public boolean canHandle(final AttributeDefinition attributeDefinition) {
return canHandle.test(attributeDefinition);
}
private static AttributeAccess ofEnumLike(final TypeReference typeReference, final Class extends AttributeDefinition> attributeDefinitionClass) {
final AttributeMapper mapper = new EnumLikeAttributeMapperImpl<>(typeReference);
return new AttributeAccess<>(mapper, attributeDefinition ->
attributeDefinitionClass.isAssignableFrom(attributeDefinition.getClass())
);
}
private static AttributeAccess ofPrimitive(final TypeReference typeReference, final Class extends AttributeDefinition> attributeDefinitionClass) {
return new AttributeAccess<>(AttributeMapper.of(typeReference), attributeDefinition -> attributeDefinitionClass.isAssignableFrom(attributeDefinition.getClass()));
}
private static AttributeAccess> OfReferenceType(final RichReferenceType referenceType) {
final AttributeMapper> mapper = new ReferenceAttributeMapperImpl<>(referenceType.typeReference());
return new AttributeAccess<>(mapper,
attributeDefinition -> {
boolean canHandle = false;
if (ReferenceAttributeDefinition.class.isAssignableFrom(attributeDefinition.getClass())) {
ReferenceAttributeDefinition casted = (ReferenceAttributeDefinition) attributeDefinition;
canHandle = casted.getAttributeType().getReferenceTypeId().equals(referenceType.getReferenceTypeId());
}
return canHandle;
});
}
private static AttributeAccess> ofSet(final Class extends AttributeType> typeClass, final TypeReference> typeReference) {
final AttributeMapper> mapper = AttributeMapper.of(typeReference);
return new AttributeAccess<>(mapper, attributeDefinition -> {
boolean canHandle = false;
if (SetAttributeDefinition.class.isAssignableFrom(attributeDefinition.getClass())) {
final SetAttributeDefinition setAttributeDefinition = (SetAttributeDefinition) attributeDefinition;
final SetType attributeType = setAttributeDefinition.getAttributeType();
canHandle = typeClass.isAssignableFrom(attributeType.getElementType().getClass());
}
return canHandle;
});
}
private static AttributeAccess>> ofSet(final ReferenceType requiredReferenceType, final TypeReference>> typeReference) {
final AttributeMapper>> mapper = AttributeMapper.of(typeReference);
return new AttributeAccess<>(mapper, attributeDefinition -> {
boolean canHandle = false;
if (SetAttributeDefinition.class.isAssignableFrom(attributeDefinition.getClass())) {
final SetAttributeDefinition setAttributeDefinition = (SetAttributeDefinition) attributeDefinition;
final SetType attributeType = setAttributeDefinition.getAttributeType();
if (attributeType.getElementType() instanceof ReferenceType) {
final ReferenceType referenceType = (ReferenceType) attributeType.getElementType();
canHandle = referenceType.getReferenceTypeId().equals(requiredReferenceType.getReferenceTypeId());
}
}
return canHandle;
});
}
private static AttributeAccess> ofEnumLikeSet(final Class extends AttributeType> clazz,
final TypeReference> typeReference) {
final AttributeMapper> mapper = new EnumLikeSetAttributeMapperImpl<>(typeReference);
return new AttributeAccess<>(mapper, attributeDefinition -> {
boolean canHandle1 = false;
if (SetAttributeDefinition.class.isAssignableFrom(attributeDefinition.getClass())) {
final SetAttributeDefinition setAttributeDefinition = (SetAttributeDefinition) attributeDefinition;
final SetType attributeType = setAttributeDefinition.getAttributeType();
canHandle1 = clazz.isAssignableFrom(attributeType.getElementType().getClass());
}
return canHandle1;
});
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy