public final class CharSequenceUtilities extends Object
SequenceCharSequence
s special treatment where this improves performance. The
disadvantage is that changes elsewhere to the concrete class of some CharSequence (e.g.
through a wrapper) can have a severe performance impact without any visible warning.
It is advisable that any wrappers for CharSequences be aware of
SequenceCharSequence
and, where applicable, make sure the outermost wrapper stays a
SequenceCharSequence for maximum performance.
It is guaranteed that when any CharSequence wrapper method in this class encounters
a SequenceCharSequence (with potentially long terminal gaps) and the wrapped CharSequence
would also have long terminal gaps, then the concrete class of the returned value will
be SequenceCharSequence, allowing for efficient handling of the terminal gaps in the result.
This class is uninstantiable.Modifier and Type | Field and Description |
---|---|
static Comparator<CharSequence> |
CASE_SENSITIVE_ORDER
A
Comparator that lexicographically orders CharSequence s. |
Modifier and Type | Method and Description |
---|---|
static CharSequence |
asLowerCase(CharSequence charSequence)
A lowercase view of the specified underlying CharSequence, i.e.
|
static CharSequence |
asUpperCase(CharSequence charSequence)
Constructs an uppercase view on the specified underlying CharSequence, i.e.
|
static int |
commonPrefixLength(CharSequence a,
CharSequence b)
The length of the longest common (case sensitive) prefix of the two specified
CharSequences a and b, i.e.
|
static CharSequence |
concatenate(List<CharSequence> charSequences)
Creates a CharSequence that represents a concatenation of the specified
CharSequences in the order in which they appear in the specified list.
|
static boolean |
contains(CharSequence charSequence,
char c)
Checks whether a CharSequence contains the specified character.
|
static int |
count(char c,
CharSequence charSequence)
Counts the number of occurrences of c in charSequence
|
static boolean |
equals(CharSequence a,
CharSequence b)
Checks whether two CharSequences contain the same sequence of characters.
|
static boolean |
equalsIgnoreCase(CharSequence a,
CharSequence b)
Checks whether two CharSequences contain the same sequence of characters,
ignoring case.
|
static CharSequence |
immutableCopyOf(CharSequence charSequence)
Constructs an immutable CharSequence that consists of the same sequence of characters as the
specified CharSequence.
|
static boolean |
isAllGaps(CharSequence charSequence)
Returns true if the given sequence is either empty or contains only gaps.
|
static boolean |
isCharSequenceKnownToBeImmutable(CharSequence charSequence)
Returns true if we know that the specified charSequence will never change.
|
static CharSequence |
repeatedCharSequence(CharSequence internalCharSequence,
int numberOfRepeats)
Create a CharSequence that repeats another CharSequence a given number of times.
|
static CharSequence |
reverse(CharSequence charSequence)
Provides a reversed view onto the specified sequence.
|
static CharSequence |
subSequence(CharSequence sequence,
int start,
int end)
Creates a CharSequence that
starts at index
start
and finishes at index end-1
such that the length of the CharSequence is start - end . |
static String |
toString(CharSequence charSequence)
Constructs a String holding the same sequence of characters as the specified charSequence,
without calling
CharSequence.toString() . |
public static final Comparator<CharSequence> CASE_SENSITIVE_ORDER
Comparator
that lexicographically orders CharSequence
s. This Comparator
is sensitive to case.public static CharSequence asUpperCase(CharSequence charSequence)
charSequence
- The CharSequence for which to provide an uppercase view.asLowerCase(CharSequence)
public static CharSequence asLowerCase(CharSequence charSequence)
charSequence
- The CharSequence for which to provide an lowercase view.asUpperCase(CharSequence)
public static int commonPrefixLength(CharSequence a, CharSequence b)
CharSequenceUtilities.equals(a.subSequence(0, i), b.subSequence(0, i))
is true.
If the two specified CharSequences represent the same sequence of characters,
then their length will be returned.
Note: To obtain the length of the longest common suffix of two sequences a and b,
you can use getCommonPrefixLength(reverse(a), reverse(b))
.a
- A charSequenceb
- Another charSequencepublic static CharSequence reverse(CharSequence charSequence)
SequenceUtilities.reverseComplement(CharSequence)
instead)
and reversing a protein sequence isn't meaningful.charSequence
- The CharSqeuence for which to provide a reversed viewpublic static boolean equals(CharSequence a, CharSequence b)
a
- A CharSequence to be compared to b; must not be null.b
- A CharSequence to be compared to a; must not be null.NullPointerException
- if a or b is nullequalsIgnoreCase(CharSequence, CharSequence)
public static String toString(CharSequence charSequence)
CharSequence.toString()
. CharSequence implementations can use this
method to implement their toString()
method.charSequence
- CharSequence for which to construct a String; must not be nullpublic static boolean equalsIgnoreCase(CharSequence a, CharSequence b)
a
- A CharSequence to be compared to b; must not be null.b
- A CharSequence to be compared to a; must not be null.NullPointerException
- if a or b is nullequals(CharSequence, CharSequence)
public static boolean contains(CharSequence charSequence, char c)
charSequence
- a CharSequence to look for the character in.c
- the character to look forNullPointerException
- if charSequence is nullpublic static int count(char c, CharSequence charSequence)
c
- The character to search forcharSequence
- The CharSequence in which to search for c. Must not be null.public static CharSequence subSequence(CharSequence sequence, int start, int end)
start
and finishes at index end-1
such that the length of the CharSequence is start - end
.
Useful for implementing CharSequence.subSequence(int, int)
sequence
- a source CharSequence that must not change in length after constructing this SubCharSequence.start
- the first index (inclusive)end
- the finish index (exclusive)public static CharSequence repeatedCharSequence(CharSequence internalCharSequence, int numberOfRepeats)
internalCharSequence
- the CharSequence to be repeated. The length of internalCharSequence must not change in future.numberOfRepeats
- the number of times to repeat itinternalCharSequence
,
repeated numberOfRepeats
times.public static boolean isCharSequenceKnownToBeImmutable(CharSequence charSequence)
charSequence
- A charSequenceNullPointerException
- if charSequence is nullpublic static CharSequence immutableCopyOf(CharSequence charSequence)
charSequence
- A CharSequenceNullPointerException
- if charSequence is nullpublic static CharSequence concatenate(List<CharSequence> charSequences)
concatenate(charSequences).toString().equals(StringUtilities.join("", charSequences))
,
but the return value may be a wrapper backed by the specified CharSequences,
so these CharSequences must not change in length after calling this method.
If this is not what you want, then use StringUtilities.join(String, Iterable)
instead.
As of 2008-05-21, this method's return value isn't guaranteed to be instanceof
SequenceCharSequence even if the specified charSequences are. However, such a
guarantee may be made in a future version. Therefore, for future safety not
only the length but also the character sequence of the specified CharSequences
shouldn't change after being passed to this method.
Generally, the concrete type of the returned CharSequence is not guaranteed and should not
be depended on.charSequences
- the CharSequences to concatenate. These CharSequences must
not change after being passed to this method.public static boolean isAllGaps(CharSequence charSequence)
charSequence
- the sequence to check within