common
Class SubstringReplacer

java.lang.Object
  extended by common.SubstringReplacer

public class SubstringReplacer
extends java.lang.Object

A SubstringReplacer is an engine for replacing substrings of a given string. The user specifies an original string and a set of (beginIndex, endIndex, replacement) triples; then the getResult method returns a version of the original string where the specified substrings are replaced with the specified replacements.


Constructor Summary
SubstringReplacer()
          Creates a new SubstringReplacer.
SubstringReplacer(java.lang.String orig)
          Creates a new SubstringReplacer with the given original string.
 
Method Summary
 void addReplacement(int begin, int end, java.lang.String repl)
          Adds an operation to replace the substring [begin, end) of the original string with the given replacement string.
 java.lang.String getResult()
          Returns the string obtained by taking the original string specified in the last call to reset, and applying all the substring replacement operations specified using addReplacement since the last call to reset.
static void main(java.lang.String[] args)
           
 void reset(java.lang.String orig)
          Sets the original string to the orig, and clears the set of replacement operations.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SubstringReplacer

public SubstringReplacer()
Creates a new SubstringReplacer. You must specify an original string using the reset method; if you call addReplacement or getResult before reset, an IllegalStateException will be thrown.


SubstringReplacer

public SubstringReplacer(java.lang.String orig)
Creates a new SubstringReplacer with the given original string.

Method Detail

reset

public void reset(java.lang.String orig)
Sets the original string to the orig, and clears the set of replacement operations.


addReplacement

public void addReplacement(int begin,
                           int end,
                           java.lang.String repl)
Adds an operation to replace the substring [begin, end) of the original string with the given replacement string. Throws an exception if the substring is empty, is not a valid substring of the original string, or overlaps with another substring that is to be replaced.


getResult

public java.lang.String getResult()
Returns the string obtained by taking the original string specified in the last call to reset, and applying all the substring replacement operations specified using addReplacement since the last call to reset.


main

public static void main(java.lang.String[] args)