Interface Selection<T>
limited
and unlimited
selections.
Useful when you need to disambiguate and enforce correct handling of the implicitly selected all concept, in replacement of the common and error prone empty-means-all hack. That is, instead of adding (or forgetting to add) special handling like:
Set<String> choices = getChoices();
if (choices.isEmpty() || choices.contains("foo")) {
// foo is selected.
}
Use Selection
so your code is intuitive and hard to get wrong:
Selection<String> choices = getChoices();
if (choices.has("foo")) {
// foo is selected.
}
To gradually migrate from legacy code where empty sets need to be special handled all over,
use nonEmptyOrAll(set)
to convert to a Selection
object:
public class FoodDeliveryService {
private final Selection<Driver> eligibleDrivers;
// Too much code churn to change this public constructor signature.
public FoodDeliveryService(Set<Driver> eligibleDrivers) {
// But we can migrate internal implementation to using Selection:
this.eligibleDrivers = Selection.nonEmptyOrAll(eligibleDrivers);
}
...
}
While an unlimited selection is conceptually close to a trivially-true predicate, the
Selection
type provides access to the explicitly selected choices via the limited()
method. It also implements equals(java.lang.Object)
, hashCode()
and toString()
sensibly.
Nulls are prohibited throughout this class.
- Since:
- 8.0
-
Nested Class Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T> Selection
<T> all()
Returns an unlimited selection of all (unspecified) choices.static Selection.Parser
delimitedBy
(char delimiter) Returns a parser forSelection
, using thedelimiter
character to delimit explicit selection elements.static Selection.Parser
delimitedBy
(Substring.Pattern delimiter) Returns a parser forSelection
, using thedelimiter
patter to delimit explicit selection elements.boolean
Returns true ifobj
is an equivalentSelection
instance.boolean
Returns true ifcandidate
is in this selection.int
hashCode()
Returns an intersection of this selection andthat
.Returns an intersection of this selection and the elements fromset
.boolean
isEmpty()
Returns true if this is alimited
selection with zero elements included.limited()
static <T> Selection
<T> none()
Returns an empty selection.static <T> Selection
<T> nonEmptyOrAll
(Collection<? extends T> choices) Converts toSelection
from legacy code where an empty collection means all.static <T> Selection
<T> only
(T... choices) Returns a selection ofchoices
.static Selection.Parser
parser()
Returns the default parser, using','
as delimiter of elements.Returns a collector that intersects the input selections.Returns a collector that collects input elements into a limited selection.toString()
Returns"ALL"
ifunlimited
, or else returns the string representation of the set of the explicit choices.toUnion()
Returns a collector that unions the input selections.Returns an union of this selection andthat
.Returns a union of this selection and the elements fromset
.
-
Method Details
-
all
Returns an unlimited selection of all (unspecified) choices. -
none
Returns an empty selection. -
only
Returns a selection ofchoices
. Null is not allowed. -
nonEmptyOrAll
Converts toSelection
from legacy code where an empty collection means all. After converted toSelection
, user code no longer need special handling of the empty case.This method is mainly for migrating legacy code. If you have a set where empty just means "none" with no special handling needed, you should use
set.stream().collect(toSelection())
instead. -
toSelection
Returns a collector that collects input elements into a limited selection. -
toIntersection
Returns a collector that intersects the input selections. -
toUnion
Returns a collector that unions the input selections. -
parser
Returns the default parser, using','
as delimiter of elements.- Since:
- 4.7
-
delimitedBy
Returns a parser forSelection
, using thedelimiter
character to delimit explicit selection elements.Because
'*'
is used as special indicator ofall()
, it can't be used as the delimiter.- Since:
- 4.7
-
delimitedBy
Returns a parser forSelection
, using thedelimiter
patter to delimit explicit selection elements.Because
'*'
is used as special indicator ofall()
, it can't be used as the delimiter.- Since:
- 4.7
-
has
Returns true ifcandidate
is in this selection. -
isEmpty
boolean isEmpty()Returns true if this is alimited
selection with zero elements included. For example:Selection.none()
. -
limited
Returns the limited choices if this selection is alimited
instance;Optional.empty()
forunlimited
instances.Note that
limited()
returningOptional.empty()
vs. returningOptional.of(emptySet())
have completely different semantics: the former means the selection is unlimited (or, has no limit); while the latter indicates that the selection has zero choices, i.e.,none()
.The caller can use this method to optionally handle explicit choices, for example:
Selection<Region> requestedRegions = ...; requestedRegions.limited().ifPesent(this::checkValidRegions);
-
intersect
Returns an intersection of this selection andthat
. -
intersect
Returns an intersection of this selection and the elements fromset
. -
union
Returns an union of this selection andthat
. -
union
Returns a union of this selection and the elements fromset
. -
equals
Returns true ifobj
is an equivalentSelection
instance. Specifically:unlimited
is always equal to itself, while never equal to anylimited
selections (even if the limited selection includes all known choices).- Two limited selections are equal if they represent equal set of explicit choices.
The set order doesn't matter, that is,
[a, b]
is considered to be equal to[b, a]
.
-
hashCode
int hashCode() -
toString
String toString()Returns"ALL"
ifunlimited
, or else returns the string representation of the set of the explicit choices. That is,only("dog", "cat").toString()
will return"[dog, cat]"
.
-