public class NaturalDescriptionJoiner
extends java.lang.Object
implements org.hamcrest.SelfDescribing
The general structure of output is following:
result ::= [Prefix] Item1 NS Item2 NS ... Itemn-1 LS Itemn [Suffix]where Prefix is an optional main description prefix, Suffix is an optional main description suffix, NS is "normal separator" string and LS is "last separator" string. Additionally, each item has the following structure:
Itemk :== [Prefixk] Valuek [Suffixk]This means that each description item may individually have its optional prefix and suffix.
Item type | Usage | Redundant when |
---|---|---|
plain value V |
description of items | V == null
|
pair of value V and its associated matcher M |
description of mismatches | M == null || M.matches(V) |
Joiner items that are deemed redundant are not sent to output.
java.lang.Object
. Typically they are strings or other implementations of
java.lang.CharSequence
, but the general object type allows lazy evaluation. If a
text chunk object implements java.util.concurrent.Callable
interface, it is invoked
during description rendering and the returned value is re-evaluated (i.e. if a Callable C1
returns a reference to another Callable C2
, there will be two invocations of Callable.call():
C2 = C1.call(); Result = C2.call();
Constructor and Description |
---|
NaturalDescriptionJoiner()
Constructs a description joiner with default settings: prefix and suffix are undefined,
normal separator is a string consisting of comma+space characters and the last separator
is the word "and" surrounded by a single space on both ends.
|
NaturalDescriptionJoiner(java.lang.Object separator)
Constructs a simple description joiner without prefix and suffix, where identical string is used
both as "normal" as well as the "last" separator.
|
NaturalDescriptionJoiner(java.lang.Object normalSeparator,
java.lang.Object lastSeparator)
Constructs a simple description joiner without prefix and suffix.
|
NaturalDescriptionJoiner(java.lang.Object prefix,
java.lang.Object normalSeparator,
java.lang.Object lastSeparator,
java.lang.Object suffix)
Constructs a description joiner with a prefix, suffix and both normal and last separators.
|
Modifier and Type | Method and Description |
---|---|
NaturalDescriptionJoiner |
add(java.lang.Object value) |
NaturalDescriptionJoiner |
add(java.lang.Object valuePrefix,
java.lang.Object value) |
NaturalDescriptionJoiner |
add(java.lang.Object valuePrefix,
java.lang.Object value,
java.lang.Object valueSuffix)
Adds a new description item, allowing to specify an optional prefix and suffix.
|
<T> NaturalDescriptionJoiner |
add(java.lang.Object valuePrefix,
T value,
java.lang.Object valueSuffix,
DescriptionProvider<? super T> valueDescriber) |
<T> NaturalDescriptionJoiner |
addMismatch(org.hamcrest.Matcher<? super T> valueMatcher,
T value) |
<T> NaturalDescriptionJoiner |
addMismatch(java.lang.Object valuePrefix,
org.hamcrest.Matcher<? super T> valueMatcher,
T value) |
<T> NaturalDescriptionJoiner |
addMismatch(java.lang.Object valuePrefix,
org.hamcrest.Matcher<? super T> valueMatcher,
T value,
java.lang.Object valueSuffix) |
<T> NaturalDescriptionJoiner |
addMismatch(java.lang.Object valuePrefix,
org.hamcrest.Matcher<? super T> valueMatcher,
T value,
java.lang.Object valueSuffix,
MismatchDescriptionProvider<? super T> mismatchDescriber) |
void |
appendTo(java.lang.StringBuilder sb)
Appends prepared description to a provided
StringBuilder . |
void |
describeTo(org.hamcrest.Description desc)
Appends description of accumulated items to the provided
Description . |
boolean |
isEmpty()
Indicates whether a list of accumulated items is empty or all items are redundant.
|
boolean |
isUsePrefixWhenEmpty()
Indicates whether a prefix string is prepended even when the main part of the output is empty.
|
boolean |
isUseSuffixWhenEmpty()
Indicates whether a suffix string is appended even when the main part of the output is empty.
|
void |
setUsePrefixWhenEmpty(boolean usePrefixWhenEmpty)
Modifies the flag that controls whether a prefix is prepended even when the main part of the output
is empty.
|
void |
setUseSuffixWhenEmpty(boolean useSuffixWhenEmpty)
Modifies the flag that controls whether a suffix is prepended even when the main part of the output
is empty.
|
NaturalDescriptionJoiner |
withDescriptionContext(java.lang.Object newContext)
Defines a context, which is an arbitrary object, for the current use of this Joiner instance.
|
NaturalDescriptionJoiner |
withDescriptionContextReceiver(ContextAwareDescriptionProvider receiver)
Certain application types can not or do not want their description providers to implement
ContextAwareDescriptionProvider interface. |
NaturalDescriptionJoiner |
withLastSeparator(java.lang.Object lastSep)
Modifies last separator (i.e.
|
NaturalDescriptionJoiner |
withNormalSeparator(java.lang.Object normalSep)
Modifies normal separator (i.e.
|
NaturalDescriptionJoiner |
withoutPrefixWhenEmpty()
Using fluent style, resets the flag that controls whether a prefix is prepended
even when the main part of the output is empty to
false . |
NaturalDescriptionJoiner |
withoutSuffixWhenEmpty()
Using fluent style, resets the flag that controls whether a suffix is appended
even when the main part of the output is empty to
false . |
NaturalDescriptionJoiner |
withPrefix(java.lang.Object prefix)
Modifies the main prefix in fluent style.
|
NaturalDescriptionJoiner |
withPrefixWhenEmpty()
Using fluent style, sets the flag that controls whether a prefix is prepended
even when the main part of the output is empty to
true . |
NaturalDescriptionJoiner |
withSuffix(java.lang.Object suffix)
Modifies the main suffix in fluent style.
|
NaturalDescriptionJoiner |
withSuffixWhenEmpty()
Using fluent style, sets the flag that controls whether a suffix is appended
even when the main part of the output is empty to
true . |
public NaturalDescriptionJoiner(java.lang.Object prefix, java.lang.Object normalSeparator, java.lang.Object lastSeparator, java.lang.Object suffix)
prefix
- prefix placed at the start of output (optional, may be null
)normalSeparator
- normal separator (null
is equivalent to an empty string)lastSeparator
- separator placed between last two items (null
is equivalent to an empty string)suffix
- suffix appended to the end of output (optional, may be null
)public NaturalDescriptionJoiner()
Equivalent: new NaturalDescriptionJoiner(null, ", ", " and ", null)
public NaturalDescriptionJoiner(java.lang.Object separator)
Equivalent: new NaturalDescriptionJoiner(null, separator, separator, null)
separator
- string that is placed between items (null
is equivalent to an empty string)NaturalDescriptionJoiner(Object, Object, Object, Object)
public NaturalDescriptionJoiner(java.lang.Object normalSeparator, java.lang.Object lastSeparator)
Equivalent: new NaturalDescriptionJoiner(null, normalSeparator, lastSeparator, null)
normalSeparator
- normal separator (null
is equivalent to an empty string)lastSeparator
- separator placed between last two items (null
is equivalent to an empty string)NaturalDescriptionJoiner(Object, Object, Object, Object)
public NaturalDescriptionJoiner withPrefix(java.lang.Object prefix)
prefix
- new description prefixpublic NaturalDescriptionJoiner withSuffix(java.lang.Object suffix)
suffix
- new description suffixpublic NaturalDescriptionJoiner withNormalSeparator(java.lang.Object normalSep)
normalSep
- new normal separator (null
is equivalent to an empty string)public NaturalDescriptionJoiner withLastSeparator(java.lang.Object lastSep)
lastSep
- new last separator (null
is equivalent to an empty string)public NaturalDescriptionJoiner withDescriptionContext(java.lang.Object newContext)
newContext
- an object that contains application-defined contextpublic NaturalDescriptionJoiner withDescriptionContextReceiver(ContextAwareDescriptionProvider receiver)
ContextAwareDescriptionProvider
interface. As an alternative, a separate instance of
that interface can be registered by this method. The receiver
is then informed about
the current context before any description provider is invoked.receiver
- implementation of ContextAwareDescriptionProvider
that receives notifications
about current description context immediately before a description provider is invokedpublic boolean isUsePrefixWhenEmpty()
false
.true
when the prefix is sent to output even when the main output part is emptypublic void setUsePrefixWhenEmpty(boolean usePrefixWhenEmpty)
usePrefixWhenEmpty
- new flag valuepublic NaturalDescriptionJoiner withPrefixWhenEmpty()
true
.setUsePrefixWhenEmpty(boolean)
public NaturalDescriptionJoiner withoutPrefixWhenEmpty()
false
.setUsePrefixWhenEmpty(boolean)
public boolean isUseSuffixWhenEmpty()
false
.true
when the suffix is sent to output even when the main output part is emptypublic void setUseSuffixWhenEmpty(boolean useSuffixWhenEmpty)
useSuffixWhenEmpty
- new flag valuepublic NaturalDescriptionJoiner withSuffixWhenEmpty()
true
.setUseSuffixWhenEmpty(boolean)
public NaturalDescriptionJoiner withoutSuffixWhenEmpty()
false
.setUseSuffixWhenEmpty(boolean)
public boolean isEmpty()
false
when the joiner contains one or more non-redundant itemspublic NaturalDescriptionJoiner add(java.lang.Object valuePrefix, java.lang.Object value, java.lang.Object valueSuffix)
Type | Processing |
---|---|
null |
omitted from output |
CharSequence |
normally appended to output |
SelfDescribing |
appended to output by means of calling its SelfDescribing.describeTo(Description) method |
valuePrefix
- optional text chunk that will be prepended immediately before value
value
- value to be appended to outputvalueSuffix
- optional text chunk that will be appended immediately after value
public NaturalDescriptionJoiner add(java.lang.Object valuePrefix, java.lang.Object value)
public NaturalDescriptionJoiner add(java.lang.Object value)
public <T> NaturalDescriptionJoiner add(java.lang.Object valuePrefix, T value, java.lang.Object valueSuffix, DescriptionProvider<? super T> valueDescriber)
public <T> NaturalDescriptionJoiner addMismatch(java.lang.Object valuePrefix, org.hamcrest.Matcher<? super T> valueMatcher, T value, java.lang.Object valueSuffix)
public <T> NaturalDescriptionJoiner addMismatch(java.lang.Object valuePrefix, org.hamcrest.Matcher<? super T> valueMatcher, T value)
public <T> NaturalDescriptionJoiner addMismatch(org.hamcrest.Matcher<? super T> valueMatcher, T value)
public <T> NaturalDescriptionJoiner addMismatch(java.lang.Object valuePrefix, org.hamcrest.Matcher<? super T> valueMatcher, T value, java.lang.Object valueSuffix, MismatchDescriptionProvider<? super T> mismatchDescriber)
public void describeTo(org.hamcrest.Description desc)
Description
. When this argument
is null
, no operation is performed.describeTo
in interface org.hamcrest.SelfDescribing
desc
- Description
instancepublic void appendTo(java.lang.StringBuilder sb)
StringBuilder
.sb
- StringBuilder
reference