io.sphere.sdk.messages.MessageDerivateHint Maven / Gradle / Ivy
The newest version!
package io.sphere.sdk.messages;
import com.fasterxml.jackson.databind.JavaType;
import io.sphere.sdk.json.SphereJsonUtils;
import io.sphere.sdk.messages.queries.MessageQuery;
import io.sphere.sdk.messages.queries.MessageQueryModel;
import io.sphere.sdk.models.Base;
import io.sphere.sdk.queries.QueryPredicate;
import javax.annotation.Nullable;
import java.util.function.Supplier;
/**
* TypeReference container used by query to get only messages of a certain type.
* @param the type of the message the type hint is about
* @see MessageQuery#forMessageType(io.sphere.sdk.messages.MessageDerivateHint)
*/
public final class MessageDerivateHint extends Base {
private final JavaType javaType;
private final Class extends Message> clazz;
private final Supplier> predicateSupplier;
@Nullable
private final String typeString;
@Nullable
private final String referenceTypeId;
private MessageDerivateHint(final JavaType javaType, final Supplier> predicateSupplier, final Class extends Message> clazz, final String typeString, final String referenceTypeId) {
this.javaType = javaType;
this.predicateSupplier = predicateSupplier;
this.clazz = clazz;
this.typeString = typeString;
this.referenceTypeId = referenceTypeId;
}
public QueryPredicate predicate() {
return predicateSupplier.get();
}
private static MessageDerivateHint ofSingleMessageType(final String type, final JavaType javaType, final Class clazz, final String referenceTypeId) {
return new MessageDerivateHint<>(javaType, () -> MessageQueryModel.of().type().is(type), clazz, type, referenceTypeId);
}
public static MessageDerivateHint ofSingleMessageType(final String type, final Class clazz, final String referenceTypeId) {
return ofSingleMessageType(type, SphereJsonUtils.convertToJavaType(clazz), clazz, referenceTypeId);
}
public static MessageDerivateHint ofResourceType(final String resourceId, final Class clazz, final String referenceTypeId) {
return new MessageDerivateHint<>(SphereJsonUtils.convertToJavaType(clazz), () -> MessageQueryModel.of().resource().typeId().is(resourceId), clazz, null, referenceTypeId);
}
public JavaType javaType() {
return javaType;
}
public Class extends Message> clazz() {
return clazz;
}
@Nullable
public String typeString() {
return typeString;
}
@Nullable
public String resourceReferenceTypeId() {
return referenceTypeId;
}
}