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.Map;
import java.util.function.Consumer;
import java.util.function.Function;

import javax.persistence.FlushModeType;
import javax.persistence.LockModeType;
import javax.persistence.NoResultException;
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;
        }
    }

// 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 withLoadGraph(String name);

    @Override
    SearchValue withFetchGraph(String name);
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy