All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.contentgrid.thunx.predicates.model.ThunkExpression Maven / Gradle / Ivy

Go to download

Set of (vendor-neutral) data structures to model authorization policy expressions

There is a newer version: 0.11.0
Show newest version
package com.contentgrid.thunx.predicates.model;

import java.util.Optional;

public interface ThunkExpression {

    Class getResultType();

    default  ThunkExpression assertResultType(Class resultType) {
        if(resultType != getResultType()) {
            throw new IllegalArgumentException("Result of expression "+this+" is "+getResultType()+", which does not match the asserted type "+resultType+".");
        }
        return (ThunkExpression) this;
    }

     R accept(ThunkExpressionVisitor visitor, C context);

    static  Optional> maybeScalar(ThunkExpression expression) {
        if(expression instanceof Scalar) {
            return Optional.of((Scalar) expression);
        }
        return Optional.empty();
    }

    static  Optional maybeValue(ThunkExpression expression) {
        if(expression.getResultType() == Void.class) {
            throw new IllegalArgumentException("Expression with result type "+Void.class+" can not be mapped with #maybeValue(), use #maybeScalar() instead.");
        }
        return maybeScalar(expression)
                .map(Scalar::getValue);
    }

}





© 2015 - 2025 Weber Informatics LLC | Privacy Policy