Class RichText
- All Implemented Interfaces:
AttributedCharSequence,RichTextRuns,ToRichText,CharSequence,Iterable<Run>
Sequences of characters that share the same formatting attributes form a
Run. Use RichTextBuilder create RichText instances.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intA character used as a delimiter or marker to insert where subsequent text segment should not be joined into a single one. -
Method Summary
Modifier and TypeMethodDescriptionvoidappendTo(RichTextBuilder builder) Append this object's rich text representation to a buffer.voidappendTo(RichTextBuilder builder, int from, int to) Appends a subset of rich text content to the specifiedRichTextBuilder.Returns a copy of this text with the supplied attributes applied to all characters.Returns a copy of this text with the supplied attributes applied to a subrange.attributedCharAt(int index) Returns theAttributedCharactervalue at the specified index.attributesAt(int idx) Retrieves the text attributes at the specified index.charcharAt(int index) intcommonPrefixLength(RichText other) Returns the number of leading characters that are equal in this text andother, including matching text attributes.intcommonSuffixLength(RichText other) Returns the number of trailing characters that are equal in this text andother, including matching text attributes.booleanTest if CharSequence is contained.static RichTextReturns the empty String as RichText.static BiPredicate<@Nullable RichText, @Nullable RichText> Returns a BiPredicate that checks whether two RichText objects are equal based on the provided ComparisonSettings.booleanequals(@Nullable RichText other, BiPredicate<? super Run, ? super Run> runEquals) Compare using a user supplied predicate for comparing Run instances.booleanbooleanequalsText(@Nullable CharSequence other) Textual compare.booleanequalsTextAndFont(@Nullable RichText other) Check if texts and style are equal, ignoring other attributes.booleanequalsTextIgnoreCase(@Nullable CharSequence other) Textual compare ignoring case.doublegetBaseline(Font defaultFont) Calculates the baseline value based on the maximum descent of fonts within the runs of text.inthashCode()intindexOf(char ch, int off) Find character.intindexOf(int ch) Find character.intReturn the index of the needle in this RichText instance.intindexOf(CharSequence s, int fromIndex) Return the index of the needle in this RichText instance.booleanisBlank()Test if blank.booleanisEmpty()Checks if the collection of rich text runs is empty.iterator()static RichTextjoin(CharSequence delimiter, CharSequence... elements) Join RichText instances together.static RichTextjoin(CharSequence delimiter, Iterable<? extends CharSequence> elements) Join RichText instances together.static RichTextJoinerCreate aRichTextJoiner.static RichTextJoinerCreate aRichTextJoiner.static RichTextJoinerCreate aRichTextJoiner.static RichTextJoinerCreate aRichTextJoiner.intlength()Length of text in characters.lines()Get stream of lines contained in this instance.static RichTextMatcherCreate aRichTextMatcher.static RichTextnewline()Returns RichText containing a single newline character.removeStyle(Style style, int start, int end) Returns a copy of this text with the specified style removed from a subrange.Replaces a text range with the suppliedreplacement.replaceAll(String regex, RichText replacement) Replaces each substring of this RichText that matches the given regular expression with the specified RichText replacement.replaceAll(String regex, String replacement) Replaces every occurrence of the specified regular expression in the text with the given replacement string.replaceFirst(String regex, RichText replacement) Replaces the first substring of this RichText that matches the given regular expression with the specified replacement RichText.replaceFirst(String regex, String replacement) Replaces the first substring of this RichText object that matches the given regular expression with the specified replacement string.runAt(int pos) Get run at position.runs()Get the runs of this instance.Get a stream containing the runs of this instance.static RichTextspace()Returns RichText containing a single space character.RichText[]Splits this RichText into an array of RichText objects based on the provided regular expression.RichText[]Splits the current RichText object into an array of RichText objects based on the given regular expression and limit.RichText[]Splits the current RichText into an array of RichText instances based on the provided pattern.RichText[]Splits the current text into an array of RichText objects based on the given pattern and limit.booleanTest whether this instance starts with the givenCharSequence.stream()A stream of the Runs this text consists of.strip()Remove leading and trailing whitespace, as defined by Unicode, removed.Remove leading whitespace, as defined by Unicode, removed.Remove trailing whitespace, as defined by Unicode, removed.stylesAt(int pos) Get active styles at position.subSequence(int start) Get a subrange of this instance.subSequence(int start, int end) Get a subrange of this instance.static RichTexttab()Returns RichText containing a single tabulator.static booleantextAndFontEquals(@Nullable RichText a, @Nullable RichText b) Check if texts and style are equal, ignoring other attributes.Get RichText representation of this object (similar totoString()).toString()trim()Remove leading and trailing whitespace, defined as "any character whose codepoint is less than or equal to'U+0020'(the space character)".static RichTextvalueOf(char c) Convert char to RichText.static RichTextConvert char to RichText.static RichTextvalueOf(char c, Collection<Style> styles) Convert char to RichText.static RichTextGet RichText containing an objects string representation.static RichTextGet styled RichText containing an object's string representation.static RichTextvalueOf(@Nullable Object obj, Collection<Style> styles) Get styled RichText containing an object's string representation.static RichTextConvert String to RichText.static RichTextCreates a new RichText instance with the given string and applies the specified style.Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface com.dua3.utility.text.AttributedCharSequence
attributedCharsMethods inherited from interface java.lang.CharSequence
chars, codePointsMethods inherited from interface java.lang.Iterable
forEach, spliterator
-
Field Details
-
SPLIT_MARKER
public static final int SPLIT_MARKERA character used as a delimiter or marker to insert where subsequent text segment should not be joined into a single one.When two texts both marked to have italic style are joined, the
RichTextBuilderclass will not detect the attribute change and for example when converted to HTML, the result of the joined texts will not be "<i>some text </i><i>other text</i>" but instead "<i>some text other text<i>".For attributes that merely control text appearance, this is usually the desired result. But to prevent two paragraphs to be joined into a single one, at least one character must come between both texts so that the change can be detected. A SPLIT_MARKER can thus be inserted to make sure the paragraphs are not joined.
The
HtmlConverterclass for example automatically removes SPLIT_MARKER from the output.- See Also:
-
-
Method Details
-
emptyText
Returns the empty String as RichText.- Returns:
- the empty text
-
space
Returns RichText containing a single space character.- Returns:
- RichText.valueOf(" ")
-
tab
Returns RichText containing a single tabulator.- Returns:
- RichText.valueOf(" \ t ")
-
newline
Returns RichText containing a single newline character.- Returns:
- RichText.valueOf(" \ n ")
-
valueOf
Get RichText containing an objects string representation.- Parameters:
obj- the object to convert to RichText- Returns:
- RichText.valueOf(String.valueOf ( obj))
-
valueOf
Convert String to RichText.- Parameters:
s- the String to convert- Returns:
- RichText representation of s
-
valueOf
Creates a new RichText instance with the given string and applies the specified style.- Parameters:
s- the source string to be converted to RichText, may be null.attributes- the attributes to be applied to the RichText instance.- Returns:
- a RichText instance containing the specified string with the applied style.
-
valueOf
Convert char to RichText.- Parameters:
c- the character- Returns:
- RichText containing only the character c
-
valueOf
Get styled RichText containing an object's string representation.- Parameters:
obj- the object to convert to RichTextstyles- the styles to apply- Returns:
- RichText.valueOf(String.valueOf ( obj))
-
valueOf
Get styled RichText containing an object's string representation.- Parameters:
obj- the object to convert to RichTextstyles- the styles to apply- Returns:
- RichText representation of s
-
valueOf
Convert char to RichText.- Parameters:
c- the characterstyles- the styles to apply- Returns:
- RichText containing only the character c
-
valueOf
Convert char to RichText.- Parameters:
c- the characterstyles- the styles to apply- Returns:
- RichText containing only the character c
-
textAndFontEquals
Check if texts and style are equal, ignoring other attributes.- Parameters:
a- textb- text- Returns:
- true, if a and b consist of the same characters with the same styling
-
join
Join RichText instances together.- Parameters:
delimiter- the delimiterelements- the elements to join- Returns:
- RichText containing the joined elements
-
join
Join RichText instances together.- Parameters:
delimiter- the delimiterelements- the elements to join- Returns:
- RichText containing the joined elements
-
matcher
Create aRichTextMatcher.- Parameters:
pattern- the patterntext- the text- Returns:
- a matcher
-
joiner
Create aRichTextJoiner.- Parameters:
delimiter- the delimiter to use- Returns:
- the joiner
-
joiner
Create aRichTextJoiner.- Parameters:
delimiter- the delimiter to useprefix- the prefixsuffix- the suffix- Returns:
- the joiner
-
joiner
Create aRichTextJoiner.- Parameters:
delimiter- the delimiter to use- Returns:
- the joiner
-
joiner
Create aRichTextJoiner.- Parameters:
delimiter- the delimiter to useprefix- the prefixsuffix- the suffix- Returns:
- the joiner
-
equals
-
equals
Compare using a user supplied predicate for comparing Run instances.- Parameters:
other- the object to compare torunEquals- the BiPredicate used for comparing- Returns:
- result true, if obj is an instance of RichText and all runs compare as equal to this instance's runs using the supplied predicate
-
equalsTextAndFont
Check if texts and style are equal, ignoring other attributes.- Parameters:
other- the text to compare with- Returns:
- true, if other consist of the same characters with the same styling as this instance
-
equalsText
Textual compare.- Parameters:
other- theCharSequenceto compare to- Returns:
- true, if this instance contains the same sequence of characters as
other
-
equalsTextIgnoreCase
Textual compare ignoring case.- Parameters:
other- theCharSequenceto compare to- Returns:
- true, if this instance contains the same sequence of characters as
other
-
hashCode
public int hashCode() -
isBlank
public boolean isBlank()Test if blank.- Returns:
trueif this instance is empty or contains only whitespace characters,falseotherwise
-
iterator
-
length
public int length()Length of text in characters.- Specified by:
lengthin interfaceCharSequence- Returns:
- the text length
-
stream
A stream of the Runs this text consists of.- Returns:
- stream of Runs
-
toString
- Specified by:
toStringin interfaceCharSequence- Overrides:
toStringin classObject
-
appendTo
Description copied from interface:ToRichTextAppend this object's rich text representation to a buffer.- Specified by:
appendToin interfaceToRichText- Parameters:
builder- the builder
-
appendTo
Appends a subset of rich text content to the specifiedRichTextBuilder. The content to be appended is determined by the specified start and end indices.- Specified by:
appendToin interfaceToRichText- Parameters:
builder- theRichTextBuilderto which the content will be appendedfrom- the start index (inclusive) of the content to be appendedto- the end index (exclusive) of the content to be appended- Throws:
IndexOutOfBoundsException- if the specified start or end indices are out of bounds or if start is greater than endNullPointerException- ifbuilderisnull
-
toRichText
Description copied from interface:ToRichTextGet RichText representation of this object (similar totoString()).- Specified by:
toRichTextin interfaceToRichText- Returns:
- RichText presentation of the object
-
lines
Get stream of lines contained in this instance.- Returns:
- stream of this text's lines
-
subSequence
Get a subrange of this instance.- Specified by:
subSequencein interfaceAttributedCharSequence- Specified by:
subSequencein interfaceCharSequence- Parameters:
start- begin index (inclusive)end- end index (exclusive)- Returns:
- RichText instance of the subrange
-
subSequence
Get a subrange of this instance.- Parameters:
start- start index (inclusive)- Returns:
- RichText instance of the subrange from beginIndex to the end
-
charAt
public char charAt(int index) - Specified by:
charAtin interfaceCharSequence
-
attributedCharAt
Description copied from interface:AttributedCharSequenceReturns theAttributedCharactervalue at the specified index.- Specified by:
attributedCharAtin interfaceAttributedCharSequence- Parameters:
index- the index of theAttributedCharactervalue to be returned- Returns:
- the specified
AttributedCharactervalue
-
attributesAt
Retrieves the text attributes at the specified index.- Parameters:
idx- the index for which the text attributes are requested- Returns:
- the text attributes associated with the run at the specified index
-
trim
Remove leading and trailing whitespace, defined as "any character whose codepoint is less than or equal to'U+0020'(the space character)".- Returns:
- copy of this instance with leading and trailing whitespace according to above criteria removed
- See Also:
-
strip
Remove leading and trailing whitespace, as defined by Unicode, removed.- Returns:
- copy of this instance with leading and trailing whitespace (according to Unicode) removed
- See Also:
-
stripLeading
Remove leading whitespace, as defined by Unicode, removed.- Returns:
- copy of this instance with leading whitespace (according to Unicode) removed
- See Also:
-
stripTrailing
Remove trailing whitespace, as defined by Unicode, removed.- Returns:
- copy of this instance with trailing whitespace (according to Unicode) removed
- See Also:
-
split
Splits this RichText into an array of RichText objects based on the provided regular expression.- Parameters:
regex- the regular expression to use as a delimiter for splitting this RichText- Returns:
- an array of RichText objects computed by splitting this RichText around matches of the given regular expression
- See Also:
-
split
Splits the current RichText into an array of RichText instances based on the provided pattern.- Parameters:
pattern- the regular expression pattern used to split the RichText. It determines the boundaries for splitting.- Returns:
- an array of RichText objects resulting from splitting the current RichText based on the given pattern.
- See Also:
-
split
Splits the current RichText object into an array of RichText objects based on the given regular expression and limit.The method splits the text into segments using the provided regular expression as the delimiter. The maximum number of resulting segments can be controlled by the limit parameter.
- Parameters:
regex- the regular expression used to determine the boundaries for splittinglimit- the maximum number of segments to include in the result; if the limit is zero or negative, the method will include all segments, including trailing empty strings if any- Returns:
- an array of RichText objects resulting from the split operation
- See Also:
-
split
Splits the current text into an array of RichText objects based on the given pattern and limit.- Parameters:
pattern- the regular expression pattern used to define delimiters for splitting the textlimit- the maximum number of elements in the result array; if limit is less than or equal to zero, splits are unlimited; if limit is 0, trailing empty matches are removed from the result- Returns:
- an array of RichText objects, split from the current text based on the specified pattern and limit
- See Also:
-
replace
Replaces a text range with the suppliedreplacement.This method avoids rebuilding a monolithic backing string. Instead it splices run metadata and uses a segmented shared character sequence as backing storage.
- Parameters:
start- start index (inclusive)end- end index (exclusive)replacement- replacement rich text- Returns:
- updated rich text
-
commonPrefixLength
Returns the number of leading characters that are equal in this text andother, including matching text attributes.- Parameters:
other- other text- Returns:
- common prefix length
-
commonSuffixLength
Returns the number of trailing characters that are equal in this text andother, including matching text attributes.- Parameters:
other- other text- Returns:
- common suffix length
-
replaceFirst
Replaces the first substring of this RichText that matches the given regular expression with the specified replacement RichText.- Parameters:
regex- the regular expression to which this RichText is to be matchedreplacement- the RichText to be used as a replacement for the first match- Returns:
- a new RichText with the first matching substring replaced by the replacement RichText
- See Also:
-
replaceFirst
Replaces the first substring of this RichText object that matches the given regular expression with the specified replacement string.- Parameters:
regex- the regular expression to which the substring should matchreplacement- the string to replace the first matching substring- Returns:
- a new RichText object with the first matching substring replaced
- See Also:
-
replaceAll
Replaces each substring of this RichText that matches the given regular expression with the specified RichText replacement.- Parameters:
regex- the regular expression to which this RichText is matchedreplacement- the RichText to be substituted for each match- Returns:
- a new RichText resulting from replacing all occurrences of the pattern with the specified replacement
- See Also:
-
replaceAll
Replaces every occurrence of the specified regular expression in the text with the given replacement string.- Parameters:
regex- the regular expression to be matchedreplacement- the string to replace each match- Returns:
- a new RichText object with the replacements made
- See Also:
-
indexOf
public int indexOf(int ch) Find character.- Parameters:
ch- the character- Returns:
- the index of the first occurrence of
ch, or -1 if not found - See Also:
-
indexOf
public int indexOf(char ch, int off) Find character.- Parameters:
ch- the characteroff- the starting position- Returns:
- the position where the char was found or -1 if not found
- See Also:
-
indexOf
Return the index of the needle in this RichText instance.- Parameters:
s- the text to find- Returns:
- the first index, where s is found within this instance
- See Also:
-
indexOf
Return the index of the needle in this RichText instance.- Parameters:
s- the text to findfromIndex- the starting position- Returns:
- the first index, where s is found within this instance
- See Also:
-
startsWith
Test whether this instance starts with the givenCharSequence.- Parameters:
s- the sequence to test- Returns:
- true, if this instance starts with s
- See Also:
-
contains
Test if CharSequence is contained.- Parameters:
s- the sequence to search for- Returns:
- true, if s is contained
- See Also:
-
apply
Returns a copy of this text with the supplied attributes applied to all characters. Existing attributes are preserved unless overwritten by an entry inattributes.- Parameters:
attributes- attributes to apply- Returns:
- copy with the attributes applied
-
apply
Returns a copy of this text with the supplied attributes applied to a subrange. Existing attributes are preserved unless overwritten by an entry inattributes.- Parameters:
attributes- attributes to applyfrom- start index (inclusive)to- end index (exclusive)- Returns:
- copy with the attributes applied to the selected range
-
removeStyle
Returns a copy of this text with the specified style removed from a subrange. Other attributes are preserved.- Parameters:
style- the style to removestart- start index (inclusive)end- end index (exclusive)- Returns:
- copy with the style removed from the selected range
-
stylesAt
Get active styles at position.- Parameters:
pos- the position (character index)- Returns:
- (unmodifiable) list of styles
-
runAt
Get run at position.- Parameters:
pos- the position (character index)- Returns:
- the Run the character at the given position belongs to
-
runs
Get the runs of this instance.- Specified by:
runsin interfaceRichTextRuns- Returns:
- unmodifiable list of runs
-
isEmpty
public boolean isEmpty()Description copied from interface:RichTextRunsChecks if the collection of rich text runs is empty.- Specified by:
isEmptyin interfaceCharSequence- Specified by:
isEmptyin interfaceRichTextRuns- Returns:
- true if there are no rich text runs in the collection; false otherwise
-
runStream
Get a stream containing the runs of this instance.- Specified by:
runStreamin interfaceRichTextRuns- Returns:
- stream of runs
-
equalizer
Returns a BiPredicate that checks whether two RichText objects are equal based on the provided ComparisonSettings.- Parameters:
s- the ComparisonSettings to be used for the equality check- Returns:
- the BiPredicate that checks for equality based on the ComparisonSettings
-
getBaseline
Calculates the baseline value based on the maximum descent of fonts within the runs of text.- Parameters:
defaultFont- the default font used as a fallback when deriving fonts from font definitions- Returns:
- the maximum descent value among all derived fonts; returns 0.0 if no runs are present
-