Conditions are part of our everyday language. The
Condition interface captures a conditional as something that can be reused. For example, waiting for something can be achieved using the
WaitFor (see waiting) class and an anonymous
Condition. For example,
1 2 3 4 5 6 7
will wait for some
server to indicate that it has shutdown. The
Conditions class collects useful
Condition objects such as
not. There is also a useful
Condition to indicate if a thread is in a waiting state in
Some common thread related conditions have been collected in the
Conditions class. These include a not condition to invert the result of some other condition and various thread state related conditions such as checking if a a thread alive. See the miscellaneous thread utils section for details.
For testing purposes, you might want to assert against the outcome of a
Conditions class has an
assertThat method which takes a
Matcher<Boolean> for use with JUnit. For example, you could assert against a condition using vanilla JUnit like this.
or using the
Conditions class, you can tidy the assert up to look like this.
If you have
Matchers available in your production code, you can use this with flexible waits, for example,