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 super S, ? extends Selection> selectionFunction) {
return ((SearchStreamImpl>)SearchStream.super.mapToSelection(type, selectionFunction)).toValue();
}
// Narrowing overrides (QueryStream)
@Override
SearchValue bind(Ref ref);
@Override
SearchValue peek(Consumer super S> peeker);
@Override
> SearchValue bind(Ref ref, Function super S, ? extends S2> refFunction);
@Override
SearchValue filter(SingularAttribute super X, Boolean> attribute);
@Override
SearchValue filter(Function super S, ? extends Expression> 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