Top Description Inners Fields Constructors Methods
com.google.common.escape

public final Class CharEscaperBuilder

extends Object
Class Inheritance
Annotations
@GwtCompatible
@ElementTypesAreNonnullByDefault
Static Imports
com.google.common.base.Preconditions.checkNotNull

Simple helper class to build a "sparse" array of objects based on the indexes that were added to it. The array will be from 0 to the maximum index given. All non-set indexes will contain null (so it's not really a sparse array, just a pseudo sparse array). The builder can also return a CharEscaper based on the generated array.
Author
Sven Mawson
Since
15.0

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
CharEscaperBuilder.CharArrayDecorator

Simple decorator that turns an array of replacement char[]s into a CharEscaper, this results in a very fast escape method.

Field Summary

Modifier and TypeField and Description
private final Map<Character, String>
private int

Constructor Summary

AccessConstructor and Description
public
CharEscaperBuilder()

Construct a new sparse array builder.

Method Summary

Modifier and TypeMethod and Description
public CharEscaperBuilder
addEscape(char c, String r)

Add a new mapping from an index to an object to the escaping.

public CharEscaperBuilder
addEscapes(char[] cs, String r)

Add multiple mappings at once for a particular index.

public @Nullable char[][]

Returns:

a "sparse" array that holds the replacement mappings.
toArray
()

Convert this builder into an array of char[]s where the maximum index is the value of the highest character that has been seen.

public Escaper

Returns:

an escaper that escapes based on the underlying array.
toEscaper
()

Convert this builder into a char escaper which is just a decorator around the underlying array of replacement char[]s.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

mapback to summary
private final Map<Character, String> map
maxback to summary
private int max

Constructor Detail

CharEscaperBuilderback to summary
public CharEscaperBuilder()

Construct a new sparse array builder.

Method Detail

addEscapeback to summary
public CharEscaperBuilder addEscape(char c, String r)

Add a new mapping from an index to an object to the escaping.

Annotations
@CanIgnoreReturnValue
addEscapesback to summary
public CharEscaperBuilder addEscapes(char[] cs, String r)

Add multiple mappings at once for a particular index.

Annotations
@CanIgnoreReturnValue
toArrayback to summary
public @Nullable char[][] toArray()

Convert this builder into an array of char[]s where the maximum index is the value of the highest character that has been seen. The array will be sparse in the sense that any unseen index will default to null.

Returns:@Nullable char[][]

a "sparse" array that holds the replacement mappings.

toEscaperback to summary
public Escaper toEscaper()

Convert this builder into a char escaper which is just a decorator around the underlying array of replacement char[]s.

Returns:Escaper

an escaper that escapes based on the underlying array.

com.google.common.escape back to summary

private Class CharEscaperBuilder.CharArrayDecorator

extends CharEscaper
Class Inheritance

Simple decorator that turns an array of replacement char[]s into a CharEscaper, this results in a very fast escape method.

Field Summary

Modifier and TypeField and Description
private final int
private final @Nullable char[][]

Constructor Summary

AccessConstructor and Description
pack-priv
CharArrayDecorator(@Nullable char[][] replacements)

Method Summary

Modifier and TypeMethod and Description
public String
escape(String
the literal string to be escaped
s
)

Overrides com.google.common.escape.CharEscaper.escape.

Returns the escaped form of a given literal string.
protected char[]
escape(char
the character to escape if necessary
c
)

Implements abstract com.google.common.escape.CharEscaper.escape.

Returns the escaped form of the given character, or null if this character does not need to be escaped.
Inherited from com.google.common.escape.CharEscaper:
escapeSlow

Field Detail

replaceLengthback to summary
private final int replaceLength
replacementsback to summary
private final @Nullable char[][] replacements

Constructor Detail

CharArrayDecoratorback to summary
pack-priv CharArrayDecorator(@Nullable char[][] replacements)

Method Detail

escapeback to summary
public String escape(String s)

Overrides com.google.common.escape.CharEscaper.escape.

Doc from com.google.common.escape.CharEscaper.escape.

Returns the escaped form of a given literal string.

Parameters
s:String

the literal string to be escaped

Returns:String

the escaped form of string

Annotations
@Override
escapeback to summary
protected char[] escape(char c)

Implements abstract com.google.common.escape.CharEscaper.escape.

Doc from com.google.common.escape.CharEscaper.escape.

Returns the escaped form of the given character, or null if this character does not need to be escaped. If an empty array is returned, this effectively strips the input character from the resulting text.

If the character does not need to be escaped, this method should return null, rather than a one-character array containing the character itself. This enables the escaping algorithm to perform more efficiently.

An escaper is expected to be able to deal with any char value, so this method should not throw any exceptions.

Parameters
c:char

the character to escape if necessary

Returns:char[]

the replacement characters, or null if no escaping was needed

Annotations
@Override
@CheckForNull