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

org.semanticweb.owlapi.model.HasAnnotationValue Maven / Gradle / Ivy

There is a newer version: 5.5.1
Show newest version
package org.semanticweb.owlapi.model;

import java.util.Optional;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;

/**
 * Convenience interface for classes that have an annotation value referred - i.e., annotation
 * assertion axioms, annotations and annotation values. The annotation value referred is the main
 * annotation value, nested annotations or annotations on axioms are excluded.
 * 
 * The purpose of these methods is providing stream friendly shortcuts to operate on literals, iris
 * and anonymous individuals used as values for annotations.
 */
public interface HasAnnotationValue {

    /**
     * @return for IRI values, the IRI, else an empty Optional
     */
    default Optional iriValue() {
        return Optional.ofNullable(annotationValue().asIRI().or((IRI) null));
    }

    /**
     * @return for literal values, the literal, else an empty Optional
     */
    default Optional literalValue() {
        return Optional.ofNullable(annotationValue().asLiteral().or((OWLLiteral) null));
    }

    /**
     * @return for anonymous individual values, the individual, else an empty Optional
     */
    default Optional anonymousIndividualValue() {
        return Optional.ofNullable(
            annotationValue().asAnonymousIndividual().or((OWLAnonymousIndividual) null));
    }

    /**
     * @return the annotation value itself; this method allows OWLAnnotationAssertionAxiom and
     *         OWLAnnotation to use the default methods rather than replicate the methods. For
     *         OWLAnnotationValue instances, the value eturned is the object itself.
     */
    OWLAnnotationValue annotationValue();

    /**
     * Execute the consumer if and only if the annotation value is of the specified type and the
     * predicate evaluates to true; the alternative runnable is executed if the predicate does not
     * match or the type does not match (only once if both conditions do not match).
     * 
     * @param witness class for which predicate and consumer should be executed
     * @param p predicate to test
     * @param c consumer to apply
     * @param r runnable to execute if the predicate does not match or the annotation type does not
     *        match
     */
    default  void when(Class witness, Predicate p,
        Consumer c, Runnable r) {
        OWLAnnotationValue t = annotationValue();
        if (witness.isInstance(t)) {
            T av = witness.cast(t);
            if (p.test(av)) {
                c.accept(av);
            } else {
                r.run();
            }
        } else {
            r.run();
        }
    }

    /**
     * Apply the function if and only if the annotation value is of the specified type and the
     * predicate evaluates to true; the alternative supplier is executed if the predicate does not
     * match or the type does not match (only once if both conditions do not match).
     * 
     * @param  type to match
     * @param  return type
     * @param witness class for which predicate and function should be executed
     * @param p predicate to test
     * @param f function to apply
     * @param s supplier to execute if the predicate does not match or the annotation type does not
     *        match
     * @return function result or supplier result, depending on predicate evaluation
     */
    default  O map(Class witness, Predicate p,
        Function f, Supplier s) {
        OWLAnnotationValue t = annotationValue();
        if (witness.isInstance(t)) {
            T av = witness.cast(t);
            if (p.test(av)) {
                return f.apply(av);
            } else {
                return s.get();
            }
        }
        return s.get();
    }

    /**
     * Apply the function if and only if the annotation value is of the specified type and the
     * predicate evaluates to true; the default value is returned if the predicate does not match or
     * the type does not match.
     * 
     * @param  type to match
     * @param  return type
     * @param witness class for which predicate and function should be executed
     * @param p predicate to test
     * @param f function to apply
     * @param defaultValue default value to return if the predicate does not match or the annotation
     *        type does not match
     * @return function result or default value, depending on predicate evaluation
     */
    default  O map(Class witness, Predicate p,
        Function f, O defaultValue) {
        OWLAnnotationValue t = annotationValue();
        if (witness.isInstance(t)) {
            T av = witness.cast(t);
            if (p.test(av)) {
                return f.apply(av);
            } else {
                return defaultValue;
            }
        }
        return defaultValue;
    }

    /**
     * @param literalConsumer consumer to run if the value is a literal
     */
    default void ifLiteral(Consumer literalConsumer) {
        OWLAnnotationValue value = annotationValue();
        if (value.isLiteral()) {
            literalConsumer.accept((OWLLiteral) value);
        }
    }


    /**
     * @param literalConsumer consumer to run if the value is a literal
     * @param elseAction runnable to run if the value iS not a literal
     */
    default void ifLiteralOrElse(Consumer literalConsumer, Runnable elseAction) {
        OWLAnnotationValue value = annotationValue();
        if (value.isLiteral()) {
            literalConsumer.accept((OWLLiteral) value);
        } else {
            elseAction.run();
        }
    }

    /**
     * @param iriConsumer consumer to run if the value is an IRI
     */
    default void ifIri(Consumer iriConsumer) {
        OWLAnnotationValue value = annotationValue();
        if (value.isIRI()) {
            iriConsumer.accept((IRI) value);
        }
    }

    /**
     * @param iriConsumer consumer to run if the value is an IRI
     * @param elseAction runnable to run if the value is not an IRI
     */
    default void ifIriOrElse(Consumer iriConsumer, Runnable elseAction) {
        OWLAnnotationValue value = annotationValue();
        if (value.isIRI()) {
            iriConsumer.accept((IRI) value);
        } else {
            elseAction.run();
        }
    }

    /**
     * @param anonConsumer consumer to run if the value is an anonymous individual
     */
    default void ifAnonymousIndividual(Consumer anonConsumer) {
        OWLAnnotationValue value = annotationValue();
        if (value.isIndividual()) {
            anonConsumer.accept((OWLAnonymousIndividual) value);
        }
    }

    /**
     * @param anonConsumer consumer to run if the value is an anonymous individual
     * @param elseAction runnable to run if the value is not an anonymous individual
     */
    default void ifAnonymousIndividualOrElse(Consumer anonConsumer,
        Runnable elseAction) {
        OWLAnnotationValue value = annotationValue();
        if (value.isIndividual()) {
            anonConsumer.accept((OWLAnonymousIndividual) value);
        } else {
            elseAction.run();
        }
    }

    /**
     * @param function function to run if the value is a literal
     * @param  returned type
     * @return mapped value for literals, empty otherwise
     */
    default  Optional mapLiteral(Function function) {
        OWLAnnotationValue value = annotationValue();
        if (value.isLiteral()) {
            return Optional.ofNullable(function.apply((OWLLiteral) value));
        }
        return Optional.empty();
    }

    /**
     * @param function function to run if the value is a literal
     * @param defaultValue value returned if the value if not a literal
     * @param  returned type
     * @return mapped value for literals, default value for non literals
     */
    default  T mapLiteralOrElse(Function function, T defaultValue) {
        OWLAnnotationValue value = annotationValue();
        if (value.isLiteral()) {
            return function.apply((OWLLiteral) value);
        }
        return defaultValue;
    }

    /**
     * @param function function to run if the value is a literal
     * @param defaultValue supplier to run if the value is not a literal
     * @param  returned type
     * @return mapped value for literals, supplier result for non literals
     */
    default  T mapLiteralOrElseGet(Function function, Supplier defaultValue) {
        OWLAnnotationValue value = annotationValue();
        if (value.isLiteral()) {
            return function.apply((OWLLiteral) value);
        }
        return defaultValue.get();
    }

    /**
     * @param function function to run if the value is an IRI
     * @param  returned type
     * @return mapped value for IRIs, empty for non IRIs
     */
    default  Optional mapIri(Function function) {
        OWLAnnotationValue value = annotationValue();
        if (value.isIRI()) {
            return Optional.ofNullable(function.apply((IRI) value));
        }
        return Optional.empty();
    }

    /**
     * @param function function to run if the value is an IRI
     * @param defaultValue default value to return if the value is not an IRI
     * @param  return type
     * @return mapped value for IRIs, default value for non IRIs
     */
    default  T mapIriOrElse(Function function, T defaultValue) {
        OWLAnnotationValue value = annotationValue();
        if (value.isIRI()) {
            return function.apply((IRI) value);
        }
        return defaultValue;
    }

    /**
     * @param function function to run if the value is an IRI
     * @param defaultValue supplier to run if the value is not an IRI
     * @param  returned type
     * @return mapped value for IRIs, supplier result for non IRIs
     */
    default  T mapIriOrElseGet(Function function, Supplier defaultValue) {
        OWLAnnotationValue value = annotationValue();
        if (value.isIRI()) {
            return function.apply((IRI) value);
        }
        return defaultValue.get();
    }

    /**
     * @param function function to run if the value is an anonymous individual
     * @param  returned type
     * @return mapped value for anonymous individuals, empty for non individuals
     */
    default  Optional mapAnonymousIndividual(Function function) {
        OWLAnnotationValue value = annotationValue();
        if (value.isIRI()) {
            return Optional.ofNullable(function.apply((OWLAnonymousIndividual) value));
        }
        return Optional.empty();
    }

    /**
     * @param function function to run if the value is an anonymous individual
     * @param defaultValue default value to if the value is not an anonymous individual
     * @param  returned type
     * @return mapped value for anonymous individuals, default value for non individuals
     */
    default  T mapAnonymousIndividualOrElse(Function function,
        T defaultValue) {
        OWLAnnotationValue value = annotationValue();
        if (value.isAnonymous()) {
            return function.apply((OWLAnonymousIndividual) value);
        }
        return defaultValue;
    }

    /**
     * @param function function to run if the value is an anonymous individual
     * @param defaultValue supplier to run if the value is not an anonymous individual
     * @param  returned type
     * @return mapped value for anonymous individuals, supplier result for non individuals
     */
    default  T mapAnonymousIndividualOrElseGet(Function function,
        Supplier defaultValue) {
        OWLAnnotationValue value = annotationValue();
        if (value.isAnonymous()) {
            return function.apply((OWLAnonymousIndividual) value);
        }
        return defaultValue.get();
    }

    /**
     * @param literalConsumer consumer to run for literals
     * @param iriConsumer consumer to run for IRIs
     * @param anonConsumer consumer to run for anonymous individuals
     */
    default void ifValue(Consumer literalConsumer, Consumer iriConsumer,
        Consumer anonConsumer) {
        OWLAnnotationValue value = annotationValue();
        if (value.isLiteral()) {
            ifLiteral(literalConsumer);
        } else if (value.isIRI()) {
            ifIri(iriConsumer);
        } else {
            ifAnonymousIndividual(anonConsumer);
        }
    }

    /**
     * @param literalFunction function to run for literals
     * @param iriFunction function to run for IRIs
     * @param anonFunction function to run for anonymous individuals
     * @param  returned type
     * @return mapped value, or empty if none matches (currently there will be always one matching
     *         function, but the creation of a new OWLAnnotationValue subinterface would change
     *         that)
     */
    default  Optional mapValue(Function literalFunction,
        Function iriFunction, Function anonFunction) {
        OWLAnnotationValue value = annotationValue();
        if (value.isLiteral()) {
            return mapLiteral(literalFunction);
        } else if (value.isIRI()) {
            return mapIri(iriFunction);
        } else {
            return mapAnonymousIndividual(anonFunction);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy