// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
public class ReplaceTest {
   private final static int N = 10000;
   public static void main(String[] args) {
      test(new ReplaceRecursiveString());
      test(new ReplaceSmartRecursiveString());
      test(new ReplaceRecursiveStringBuffer());
      test(new ReplaceRecursiveGlobalStringBuffer());
      test(new ReplaceLoopString());
      test(new ReplaceLoopStringBuffer());
      return;
   }
   private static void test(Replacer r) {
      verify(r);
      perf(r);
      return;
   }
   private static void verify(Replacer r) {
      if (!r.replaceAll("123<P>456<P>789", "<P>", "\n").equals("123\n456\n789")
         || !r.replaceAll("<P>123<P>456<P>789<P>", "<P>", "\n").equals("\n123\n456\n789\n")
         || !r.replaceAll("123<P><P>456", "<P>", "\n").equals("123\n\n456")
         || !r.replaceAll("<P>", "<P>", "\n").equals("\n")
         || !r.replaceAll("", "<P>", "\n").equals("")) {
         System.out.println(r.getMethod() + " failed");
      }
      return;
   }
   private static void perf(Replacer r) {
      System.out.println(r.getMethod() + ":");
      perf("short text - few replaces", r, 1000, 5);
      perf("short text - many replaces", r, 1000, 25);
      perf("long text - few replaces", r, 10000, 5);
      perf("long text - many replaces", r, 10000, 25);
      return;
   }
   private static void perf(String title, Replacer r, int txtlen, int nrepl) {
      StringBuffer sb = new StringBuffer("");
      for (int i = 0; i < txtlen; i++) {
         sb.append('A');
         if ((i % (txtlen / nrepl)) == 0) {
            sb.append("<P>");
         }
      }
      perf(title, r, sb.toString());
      return;
   }
   private static void perf(String title, Replacer r, String s) {
      long t1 = System.currentTimeMillis();
      for (int i = 0; i < N; i++) {
         r.replaceAll(s, "<P>", "\n");
      }
      long t2 = System.currentTimeMillis();
      System.out.println(title + " = " + (t2 - t1));
      return;
   }
}
interface Replacer {
   public String getMethod();
   public String replaceAll(String s, String s1, String s2);
}
class ReplaceRecursiveString implements Replacer {
   public String getMethod() {
      return "Recursive with String";
   }
   public String replaceAll(String s, String s1, String s2) {
      int ix = s.indexOf(s1);
      if (ix >= 0) {
         return replaceAll(s.substring(0, ix) + s2 + s.substring(ix + s1.length()), s1, s2);
      } else {
         return s;
      }
   }
}
class ReplaceSmartRecursiveString implements Replacer {
   public String getMethod() {
      return "Smart recursive with String";
   }
   public String replaceAll(String s, String s1, String s2) {
      int ix = s.indexOf(s1);
      if (ix >= 0) {
         return (s.substring(0, ix) + s2 + replaceAll(s.substring(ix + s1.length()), s1, s2));
      } else {
         return s;
      }
   }
}
class ReplaceRecursiveStringBuffer implements Replacer {
   public String getMethod() {
      return "Recursive with StringBuffer";
   }
   public String replaceAll(String s, String s1, String s2) {
      int ix = s.indexOf(s1);
      if (ix >= 0) {
         StringBuffer tmp = new StringBuffer("");
         tmp.append(s.substring(0, ix));
         tmp.append(s2);
         tmp.append(s.substring(ix + s1.length()));
         return replaceAll(tmp.toString(), s1, s2);
      } else {
         return s;
      }
   }
}
class ReplaceRecursiveGlobalStringBuffer implements Replacer {
   public String getMethod() {
      return "Recursive with global StringBuffer";
   }
   public String replaceAll(String s, String s1, String s2) {
      StringBuffer tmp = new StringBuffer("");
      replaceAllHelp(tmp, s, s1, s2);
      return tmp.toString();
   }
   private void replaceAllHelp(StringBuffer res, String s, String s1, String s2) {
      int ix = s.indexOf(s1);
      if (ix >= 0) {
         res.append(s.substring(0, ix));
         res.append(s2);
         replaceAllHelp(res, s.substring(ix + s1.length()), s1, s2);
      } else {
         res.append(s);
      }
      return;
   }
}
class ReplaceLoopString implements Replacer {
   public String getMethod() {
      return "Loop with String";
   }
   public String replaceAll(String s, String s1, String s2) {
      String tmp = new String("");
      int pos = 0;
      while (pos < s.length()) {
         int ix = s.indexOf(s1, pos);
         if (ix >= 0) {
            tmp = tmp + s.substring(pos, ix) + s2;
            pos = ix + s1.length();
         } else {
            tmp = tmp + s.substring(pos);
            pos = s.length();
         }
      }
      return tmp;
   }
}
class ReplaceLoopStringBuffer implements Replacer {
   public String getMethod() {
      return "Loop with StringBuffer";
   }
   public String replaceAll(String s, String s1, String s2) {
      StringBuffer tmp = new StringBuffer("");
      int pos = 0;
      while (pos < s.length()) {
         int ix = s.indexOf(s1, pos);
         if (ix >= 0) {
            tmp.append(s.substring(pos, ix));
            tmp.append(s2);
            pos = ix + s1.length();
         } else {
            tmp.append(s.substring(pos));
            pos = s.length();
         }
      }
      return tmp.toString();
   }
}
