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

5
  • "The logic in class B is different for a StringBuffer and for a StringBuilder." How is it different? Commented Jan 19, 2017 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. Commented Jan 19, 2017 at 10:36
  • For the difference: stackoverflow.com/questions/355089/… Commented Jan 19, 2017 at 10:43
  • @RC. I meant the difference in her algorithm that requires the usage of both classes and is apparently essential. Commented Jan 19, 2017 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 Commented Jan 19, 2017 at 11:03

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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());
}

1 Comment

Thanks, but since I have to perform a different operation for each this won't work in my case.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.