0

I have the following code that is duplicated and I'd like to only write it once (please note that it's just a sample of what I want to achieve):

public class A {
    public void doSomething(StringBuffer additional, int start, int end) {
        try {
            StringBuilder strBuilder = callMethodA();
            strBuilder.append(additional, start, end);
            B.doSomething(strBuilder, additional, start, end);             
        } catch (Exception e) {
            // do something
        }
    }

    public void doSomething(StringBuilder additional, int start, int end) {
        try {
            StringBuilder strBuilder = callMethodA();
            strBuilder.append(additional, start, end);
            B.doSomething(strBuilder, additional, start, end);             
        } catch (Exception e) {
            // do something
        }
    }
    ...
}

public class B {
    public static void doSomething(StringBuilder strBuilder, 
                     StringBuilder additional, int start, int end) { 
             // Do some code specific to "additional" StringBuilder
             ...
    }

    public static void doSomething(StringBuilder strBuilder, 
                     StringBuffer additional, int start, int end) {...}
             // Do some code specific to "additional" StringBuffer
             ...
}

As you can see, the code is identical except from the fact that the "additional" parameter is in the first method of class A a StringBuilder and in the second a StringBuffer. How can I modify my code so it will allow me to avoid duplication? The logic in class B is different for a StringBuffer and for a StringBuilder. I cannot use AbstractStringBuilder since it's a protected class. I'm using Java 6.

Thanks

Vlad
  • 18,195
  • 4
  • 41
  • 71
Dana G
  • 64
  • 1
  • 6
  • "The logic in class B is different for a StringBuffer and for a StringBuilder." How is it different? – Kayaman Jan 19 '17 at 10:35
  • For example if I want to create a new StringBuilder in case of a StringBuilder and a new StringBuffer in case of a StringBuffer. – Dana G Jan 19 '17 at 10:36
  • For the difference: http://stackoverflow.com/questions/355089/difference-between-stringbuilder-and-stringbuffer?rq=1 –  Jan 19 '17 at 10:43
  • @RC. I meant the difference in her algorithm that requires the usage of both classes and is apparently essential. – Kayaman Jan 19 '17 at 10:44
  • @Kayaman oops I thought it was an OP comment. If OP really really (I mean **really**) need a `StringBuffer` (I also doubt that) then the answer is probably some wrappers and a common interface –  Jan 19 '17 at 11:03

2 Answers2

2

How about using the Appendable interface to accept both StringBuilder and StringBuffer in the A class, then use instanceof to check for the type in the B class. Like so:

public class A {
    public void doSomething(Appendable additional, int start, int end) {
        try {
            StringBuilder strBuilder = callMethodA();
            strBuilder.append(additional.toString(), start, end);
            B.doSomething(strBuilder, additional, start, end);             
        } catch (Exception e) {
            // do something
        }
    }
}

public class B {
    public static void doSomething(StringBuilder strBuilder, Appendable additional, int start, int end) {
        if (additional instanceof StringBuilder) {
            // Do some code specific to "additional" StringBuilder
        } else if (additional instanceof StringBuffer) {
            // Do some code specific to "additional" StringBuffer
        }
    }
}

Edit: Honestly the Appendable interface is not really needed as only the toString function is applied, it could also be just an Object. If you want to only accept StringBuilders and StringBuffers in your functions, you could overload the doSomething functions, and make the doSomething functions that takes the Appendable (or Object if you prefer) private.

Sore Throat
  • 189
  • 5
0

As StringBuffer and StringBuilder are not related, the only solution I can think of would be something like this:

public void doSomething(StringBuffer additional, int start, int end) {
    StringBuilder ad = new StringBuilder(additional);
    doSomething(ad, start, end);
    additional.replace(0, additional.length(), ad.toString());
}
Maurice Perry
  • 9,261
  • 2
  • 12
  • 24
  • Thanks, but since I have to perform a different operation for each this won't work in my case. – Dana G Jan 19 '17 at 11:24