Package it.tidalwave.util
Interface Finder<T>
- All Superinterfaces:
Cloneable
,Serializable
- All Known Subinterfaces:
ExtendedFinderSupport<T,
,F> FinderWithId<T,
F>
- All Known Implementing Classes:
FinderWithIdMapSupport
,FinderWithIdSupport
,HierarchicFinderSupport
,SimpleFinderSupport
A factory for providing results of a search.
Finder
implementations must be immutable.- Author:
- Fabrizio Giudici
- Status: draft API
-
Nested Class Summary
Modifier and TypeInterfaceDescriptionstatic interface
An interface that should be implemented by specificFinder.SortCriterion
objects which are capable to implement by themselves the sorting of objects, by post-processing an existing collection of objects.static interface
A tag interface to mark objects which are meaningful sort criteria that can be passed tosort(it.tidalwave.util.Finder.SortCriterion)
.static enum
An enumeration to define the direction of a sort (ascending or descending). -
Method Summary
Modifier and TypeMethodDescriptionint
count()
Performs the search and returns the count of found items.static <U> Finder
<U> empty()
Returns an emptyFinder
.default T
Deprecated.default void
Iterates through results.from
(int firstResult) Tells theFinder
that only a subset of found items will be returned, starting from the given position.Tells theFinder
that only a subset of found items will be returned, starting from the given position.from
(OptionalInt firstResult) Tells theFinder
that only a subset of found items will be returned, starting from the given position.iterator()
Returns an iterator of results.static <U,
V> Finder <U> Returns a mappingFinder
on a given delegateFinder
.max
(int maxResults) Tells theFinder
that only a maximum number of found items will be returned.Tells theFinder
that only a maximum number of found items will be returned.max
(OptionalInt maxResults) Tells theFinder
that only a maximum number of found items will be returned.static <U> Finder
<U> ofCloned
(Collection<? extends U> items) Returns a wrappedFinder
on a given collection of elements.static <U> Finder
<U> ofProvider
(BiFunction<Integer, Integer, ? extends Collection<? extends U>> provider) Returns a wrappedFinder
on a given function to provide results.static <U> Finder
<U> ofSupplier
(Supplier<? extends Collection<? extends U>> supplier) Returns a wrappedFinder
on a given supplier.default <U> Finder
<U> Tells theFinder
that the specified type of results is expected.Performs the search and returns only the first found item.Performs the search assuming that it will return a single item and returns it.default T
result()
Deprecated.UseoptionalResult()
insteadresults()
Performs the search and returns the found items.sort
(Finder.SortCriterion criterion) Tells theFinder
that results will be sorted according to the given criterion, in ascending direction.sort
(Finder.SortCriterion criterion, Finder.SortDirection direction) Tells theFinder
that results will be sorted according to the given criterion and direction.stream()
Returns a stream of results.withContext
(Object context) Tells theFinder
that results should be created with the given context.
-
Method Details
-
from
Tells theFinder
that only a subset of found items will be returned, starting from the given position.- Parameters:
firstResult
- the index of the first result to return- Returns:
- the
Finder
-
from
Tells theFinder
that only a subset of found items will be returned, starting from the given position.- Parameters:
firstResult
- the index of the first result to return- Returns:
- the
Finder
- Since:
- 3.2-ALPHA-19
-
from
Tells theFinder
that only a subset of found items will be returned, starting from the given position.- Parameters:
firstResult
- the index of the first result to return- Returns:
- the
Finder
- Since:
- 3.2-ALPHA-22
-
max
Tells theFinder
that only a maximum number of found items will be returned.- Parameters:
maxResults
- the max number of results to return- Returns:
- the
Finder
-
max
Tells theFinder
that only a maximum number of found items will be returned.- Parameters:
maxResults
- the max number of results to return- Returns:
- the
Finder
- Since:
- 3.2-ALPHA-19
-
max
Tells theFinder
that only a maximum number of found items will be returned.- Parameters:
maxResults
- the max number of results to return- Returns:
- the
Finder
- Since:
- 3.2-ALPHA-22
-
withContext
Tells theFinder
that results should be created with the given context. This method can be called multiple times; contexts are accumulated.- Parameters:
context
- the context- Returns:
- the
Finder
-
ofType
Tells theFinder
that the specified type of results is expected.- Type Parameters:
U
- the static type- Parameters:
type
- the dynamic type- Returns:
- the
Finder
-
sort
Tells theFinder
that results will be sorted according to the given criterion, in ascending direction.- Parameters:
criterion
- the sort criterion- Returns:
- the
Finder
-
sort
@Nonnull Finder<T> sort(@Nonnull Finder.SortCriterion criterion, @Nonnull Finder.SortDirection direction) Tells theFinder
that results will be sorted according to the given criterion and direction.- Parameters:
criterion
- the sort criteriondirection
- the sort direction- Returns:
- the
Finder
-
results
Performs the search and returns the found items.- Returns:
- the searched items
-
count
@Nonnegative int count()Performs the search and returns the count of found items.- Returns:
- the count of found items
-
optionalResult
Performs the search assuming that it will return a single item and returns it. This method fails if the search returns more than one single item.- Returns:
- the optional result
- Throws:
RuntimeException
- if the search returned more than one single item- Since:
- 3.2-ALPHA-1 (previously in Finder8)
-
optionalFirstResult
Performs the search and returns only the first found item.- Returns:
- the first result
- Since:
- 3.2-ALPHA-1 (previously in Finder8)
-
stream
Returns a stream of results.- Returns:
- the stream
- Since:
- 3.2-ALPHA-1 (previously in Finder8)
-
iterator
Returns an iterator of results.- Returns:
- the iterator
- Since:
- 3.2-ALPHA-1 (previously in Finder8)
-
forEach
Iterates through results.- Parameters:
consumer
- the consumer- Since:
- 3.2-ALPHA-22
-
result
Deprecated.UseoptionalResult()
insteadPerforms the search assuming that it will return a single item and returns it. This method fails if the search returns more than one single item.- Returns:
- the found item
- Throws:
NotFoundException
- if the search didn't find anythingRuntimeException
- if the search returned more than one single item
-
firstResult
Deprecated.UseoptionalFirstResult()
insteadPerforms the search and returns only the first found item.- Returns:
- the first found item
- Throws:
NotFoundException
- if the search didn't find anything
-
empty
Returns an emptyFinder
.- Type Parameters:
U
- the type of theFinder
- Returns:
- the empty
Finder
- Since:
- 3.2-ALPHA-1 (previously in HierarchicFinderSupport.emptyFinder())
-
ofCloned
Returns a wrappedFinder
on a given collection of elements. The collection is cloned and will be immutable. If you need to compute the collection on demand, useofSupplier(Supplier)
. This method retrieves the full range of results that will be later segmented in compliance with the values specified byfrom(int)
andmax(int)
; this is ok if the whole list of results is already available of if it is not expensive to compute. The alternate methodofProvider(BiFunction)
allows to access the 'from' and 'max' parameter, so only the required items need to be provided.- Type Parameters:
U
- the type of theFinder
- Parameters:
items
- the objects to wrap- Returns:
- the wrapped
Finder
- Since:
- 3.2-ALPHA-1
- See Also:
-
ofSupplier
@Nonnull static <U> Finder<U> ofSupplier(@Nonnull Supplier<? extends Collection<? extends U>> supplier) Returns a wrappedFinder
on a given supplier. The collection will be cloned after being supplied. This method retrieves the full range of results that will be later segmented in compliance with the values specified byfrom(int)
andmax(int)
; this is ok if the whole list of results is already available of if it is not expensive to compute. The alternate methodofProvider(BiFunction)
allows to access the 'from' and 'max' parameter, so only the required items need to be provided.- Type Parameters:
U
- the type of theFinder
- Parameters:
supplier
- the supplier- Returns:
- the wrapped
Finder
- Since:
- 3.2-ALPHA-15
- See Also:
-
ofProvider
@Nonnull static <U> Finder<U> ofProvider(@Nonnull BiFunction<Integer, Integer, ? extends Collection<? extends U>> provider) Returns a wrappedFinder
on a given function to provide results. The function receives the 'from' and 'max' arguments to select a subrange of the results. The collection will be cloned after being supplied.- Type Parameters:
U
- the type of theFinder
- Parameters:
provider
- the function providing results- Returns:
- the wrapped
Finder
- Since:
- 3.2-ALPHA-15
- See Also:
-
mapping
@Nonnull static <U,V> Finder<U> mapping(@Nonnull Finder<V> delegate, @Nonnull Function<? super V, ? extends U> mapper) Returns a mappingFinder
on a given delegateFinder
. The mapper finder provides the same results as the delegate, transformed by a mapper function.- Type Parameters:
U
- the type of theFinder
V
- the type of the delegateFinder
- Parameters:
delegate
- the delegate findermapper
- the mapper function- Returns:
- the wrapped
Finder
- Since:
- 3.2-ALPHA-15
-
optionalFirstResult()
instead