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

org.dellroad.querystream.jpa.SearchValue Maven / Gradle / Ivy


/*
 * Copyright (C) 2018 Archie L. Cobbs. All rights reserved.
 */

package org.dellroad.querystream.jpa;

import java.util.Calendar;
import java.util.Date;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;

import javax.persistence.FlushModeType;
import javax.persistence.LockModeType;
import javax.persistence.NoResultException;
import javax.persistence.Parameter;
import javax.persistence.TemporalType;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Selection;
import javax.persistence.metamodel.SingularAttribute;

/**
 * A {@link SearchStream} that is guaranteed to return at most a single result.
 */
public interface SearchValue> extends SearchStream {

    /**
     * Build and evaluate a JPA query based on this instance and return the single result, if any.
     *
     * @return result of executed query
     * @throws NoResultException if there is no result
     */
    default X value() {
        return this.toQuery().getSingleResult();
    }

    /**
     * Build and evaluate a JPA query based on this instance and return the single result, if any,
     * otherwise the given value.
     *
     * @param defaultValue value to return if there is no match
     * @return result of executed query, or {@code defaultValue} if not found
     */
    default X orElse(X defaultValue) {
        try {
            return this.value();
        } catch (NoResultException e) {
            return defaultValue;
        }
    }

    /**
     * Build and evaluate a JPA query based on this instance and return the single result, if any,
     * otherwise throw an exception provided by the given {@link Supplier}.
     *
     * @param supplier creator of exception
     * @param  exception type
     * @return result of executed query
     * @throws T if there is no result
     */
    default  X orElseThrow(Supplier supplier) throws T {
        if (supplier == null)
            throw new IllegalArgumentException("null supplier");
        try {
            return this.value();
        } catch (NoResultException e) {
            throw supplier.get();
        }
    }

    /**
     * Build and evaluate a JPA query based on this instance and return the single result, if any,
     * otherwise the value from the given {@link Supplier}.
     *
     * @param supplier creator of exception
     * @return result of executed query
     * @throws IllegalArgumentException if {@code supplier} is null
     */
    default X orElseGet(Supplier supplier) {
        if (supplier == null)
            throw new IllegalArgumentException("null supplier");
        try {
            return this.value();
        } catch (NoResultException e) {
            return supplier.get();
        }
    }

    /**
     * Build and evaluate a JPA query based on this instance and give the returned value, if any, to the given {@link Consumer}.
     *
     * @param consumer receives value returned by query, if any
     * @throws IllegalArgumentException if {@code consumer} is null
     */
    default void ifPresent(Consumer consumer) {
        if (consumer == null)
            throw new IllegalArgumentException("null consumer");
        final X value;
        try {
            value = this.value();
        } catch (NoResultException e) {
            return;
        }
        consumer.accept(value);
    }

    /**
     * Build and evaluate a JPA query based on this instance and return true if a result is returned, otherwise false.
     *
     * @return true if executed query returns a result, false otherwise
     */
    default boolean isPresent() {
        try {
            this.value();
        } catch (NoResultException e) {
            return false;
        }
        return true;
    }

    /**
     * Build and evaluate a JPA query based on this instance and return the single result, if any, as an {@link Optional}.
     *
     * 

* Note: due to limitations of the {@link Optional} class, this method does not support returning null values; * if the query returns a null value, an exception is thrown. * * @return the optional result of the executed query * @throws IllegalArgumentException if this query returns a null value */ default Optional toOptional() { final X value; try { value = this.value(); } catch (NoResultException e) { return Optional.empty(); } try { return Optional.of(value); } catch (NullPointerException e) { throw new IllegalArgumentException("null values cannot be represented in Optional", e); } } // Narrowing overrides (SearchStream) @Override default SearchValue> mapToSelection(Class type, Function> selectionFunction) { return ((SearchStreamImpl>)SearchStream.super.mapToSelection(type, selectionFunction)).toValue(); } // Narrowing overrides (QueryStream) @Override SearchValue bind(Ref ref); @Override SearchValue peek(Consumer peeker); @Override > SearchValue bind(Ref ref, Function refFunction); @Override SearchValue filter(SingularAttribute attribute); @Override SearchValue filter(Function> predicateBuilder); @Override SearchValue withFlushMode(FlushModeType flushMode); @Override SearchValue withLockMode(LockModeType lockMode); @Override SearchValue withHint(String name, Object value); @Override SearchValue withHints(Map hints); @Override SearchValue withParam(Parameter parameter, T value); @Override SearchValue withParam(Parameter parameter, Date value, TemporalType temporalType); @Override SearchValue withParam(Parameter parameter, Calendar value, TemporalType temporalType); @Override SearchValue withParams(Set> params); @Override SearchValue withLoadGraph(String name); @Override SearchValue withFetchGraph(String name); }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy