public abstract static class Obfuscator.SplitPoint extends Object
CharSequence
or Reader
to split obfuscation.
Like Obfuscator.untilLength(int)
, this can be used to combine obfuscators. For instance, to obfuscate email addresses:
Obfuscator localPartObfuscator = portion()
.keepAtStart(1)
.keepAtEnd(1)
.withFixedTotalLength(8)
.build();
Obfuscator domainObfuscator = none();
Obfuscator obfuscator = SplitPoint.atFirst('@').splitTo(localPartObfuscator, domainObfuscator);
// Everything before @ will be obfuscated using localPartObfuscator, everything after @ will not be obfuscated
// Example input: test@example.org
// Example output: t******t@example.org
Unlike Obfuscator.untilLength(int)
it's not possible to chain splitting, but it's of course possible to nest it:
Obfuscator localPartObfuscator = portion()
.keepAtStart(1)
.keepAtEnd(1)
.withFixedTotalLength(8)
.build();
Obfuscator domainObfuscator = SplitPoint.atLast('.').splitTo(all(), none());
Obfuscator obfuscator = SplitPoint.atFirst('@').splitTo(localPartObfuscator, domainObfuscator);
// Everything before @ will be obfuscated using localPartObfuscator, everything after @ will be obfuscated until the last dot
// Example input: test@example.org
// Example output: t******t@*******.org
splitStart(CharSequence, int, int)
and splitLength()
.
Obfuscators created by calling splitTo(Obfuscator, Obfuscator)
use these two methods to determine how to split the text to obfuscate.
If splitStart(CharSequence, int, int)
returns -1, only the first obfuscator will be used. Otherwise, where splitStart
is the
result of calling splitStart(CharSequence, int, int)
:
start
to splitStart
will be obfuscated using the first obfuscator.splitStart
to splitStart +
splitLength()
will not be obfuscated.splitStart +
splitLength()
to end
will be obfuscated using the second obfuscator.splitTo(Obfuscator, Obfuscator)
. It's therefore advised to
implement Object.equals(Object)
(and Object.hashCode()
) so logically equivalent split points will be considered equal.
splitTo(Obfuscator, Obfuscator)
. It's therefore advised to override Object.toString()
to something meaningful.Modifier | Constructor and Description |
---|---|
protected |
SplitPoint()
Creates a new split point.
|
Modifier and Type | Method and Description |
---|---|
static Obfuscator.SplitPoint |
atFirst(char c)
Creates a new split point that splits at the first occurrence of a character.
|
static Obfuscator.SplitPoint |
atLast(char c)
Creates a new split point that splits at the last occurrence of a character.
|
static Obfuscator.SplitPoint |
atNth(char c,
int occurrence)
Creates a new split point that splits at a specific occurrence of a character.
|
protected abstract int |
splitLength()
Returns the length of
CharSequence ranges to not obfuscate when splitting. |
protected abstract int |
splitStart(CharSequence s,
int start,
int end)
For a given
CharSequence range, finds the index where to start to split. |
Obfuscator |
splitTo(Obfuscator beforeSplitPoint,
Obfuscator afterSplitPoint)
Creates an obfuscator that splits obfuscation at this split point.
|
public static Obfuscator.SplitPoint atFirst(char c)
c
- The character to split at.public static Obfuscator.SplitPoint atLast(char c)
c
- The character to split at.public static Obfuscator.SplitPoint atNth(char c, int occurrence)
c
- The character to split at.occurrence
- The zero-based occurrence of the character to split at.IllegalArgumentException
- If the given occurrence is negative.protected abstract int splitStart(CharSequence s, int start, int end)
CharSequence
range, finds the index where to start to split.s
- The CharSequence
to find the split start index for.start
- The start index in the CharSequence
of the range to find the split start index in, inclusive.end
- The end index in the CharSequence
of the range to find the split start index in, exclusive.CharSequence
, between the given start and end indexes, where to start to split,
or -1 to not split.protected abstract int splitLength()
CharSequence
ranges to not obfuscate when splitting.
All characters with an index between splitStart
and splitStart + splitLength()
, where splitStart
is the result of
calling splitStart(CharSequence, int, int)
, will not be obfuscated.CharSequence
ranges to not obfuscate when splitting.public final Obfuscator splitTo(Obfuscator beforeSplitPoint, Obfuscator afterSplitPoint)
CharSequence
or Reader
before the split
point will be obfuscated by one obfuscator, the part after the split point by another.beforeSplitPoint
- The obfuscator to use before the split point.afterSplitPoint
- The obfuscator to use after the split point.Copyright © 2020–2023. All rights reserved.