org.javasimon.utils
Class Replacer

java.lang.Object
  extended by org.javasimon.utils.Replacer

public final class Replacer
extends Object

Replacer stores from regex as pattern and its process(java.lang.String) method than returns string with all from replaced with to. Using the replacer should be faster because the regex is compiled only once. Pattern.matcher(CharSequence) and Matcher.replaceAll(String) is used internally, hence to parameter is regex as well and can reference matched group from the from String.

Behavior can be further modified by Replacer.Modificators.

Author:
Richard "Virgo" Richter

Nested Class Summary
static class Replacer.Modificator
          Flags for modifying the default replacer behavior.
 
Field Summary
private  Pattern from
           
private  boolean ignoreCase
           
private  Replacer.Modificator[] modificators
           
private  boolean repeatUntilUnchanged
           
private  String to
           
 
Constructor Summary
Replacer(String from, String to, Replacer.Modificator... modificators)
          Creates the replacer with from->to regex specification.
 
Method Summary
 String getTo()
          Returns replacement string.
 String process(String in)
          Processes input, replaces all as expected and returns the result.
private  void processModificators(Replacer.Modificator... modificators)
           
 void setTo(String to)
          Sets replacement string - this can be changed anytime with any subsequent process(String) calls reflecting this change immediatelly.
 String toString()
          Returns from, to and untilUnchanged fields as a human readable string.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

from

private final Pattern from

to

private String to

repeatUntilUnchanged

private boolean repeatUntilUnchanged

ignoreCase

private boolean ignoreCase

modificators

private Replacer.Modificator[] modificators
Constructor Detail

Replacer

public Replacer(String from,
                String to,
                Replacer.Modificator... modificators)
Creates the replacer with from->to regex specification.

Parameters:
from - replaced regex
to - replacement string
modificators - list of modificators of the behavior or pattern treatment
Method Detail

processModificators

private void processModificators(Replacer.Modificator... modificators)

getTo

public String getTo()
Returns replacement string.

Returns:
replacement string

setTo

public void setTo(String to)
Sets replacement string - this can be changed anytime with any subsequent process(String) calls reflecting this change immediatelly.

Parameters:
to - new replacement string

process

public String process(String in)
Processes input, replaces all as expected and returns the result.

Parameters:
in - input string
Returns:
replaced string

toString

public String toString()
Returns from, to and untilUnchanged fields as a human readable string.

Overrides:
toString in class Object
Returns:
internal details of the replacer as a string


Copyright © 2013. All Rights Reserved.