2

I'm attempting to make a class that will convert ArrayLists of objects into ArrayLists of other objects. i.e.

ArrayList<Foo> convert(ArrayList<Bar> input){
     //conversion logic
}

ArrayList<Bar> convert(ArrayList<Foo> input){
     //conversion logic
}

Unfortunately Java doesn't want to have two functions with the same name and what it believes to be the same inputs and outputs.

I'm attempting to go a different route. Instead of multiple functions with the same name, I want to make one function that accepts an ArrayList, determines which type of object is inside, does the proper conversion, and returns an ArrayList:

ArrayList convert(ArrayList input){
     //conversion logic for Foo

     //conversion logic for Bar
}

Is something like this possible?

4
  • Is there any subtype relationship between Bar and Foo for your purposes? Commented Jun 24, 2009 at 18:56
  • Side issue: For flexibility the input should be an interface, such as List or even better Collection. Actually the return type should also be an interface. Commented Jun 24, 2009 at 19:07
  • Could you please clarify your goal? Do you want to create a generic converter or conversion between few types? In the latter case, you could just include the source or target type name in the method name. Commented Jun 24, 2009 at 19:23
  • I'm using GWT and google app engine, since GWT doesn't play nicely with GAE I need to have two copies of my entities: One for GWT (just pojo), one for GAE (with annotations). My end goal is to make converting the objects as easy as possible, since there will be A LOT of converting back and forth. Commented Jun 24, 2009 at 20:07

3 Answers 3

4

How about an interface:

public class Converter<From, To> {
    List<To> convert(List<From> input);
}

And then have as many implementations as you want. For example:

private static final Converter<Foo, Bar> fooToBarConverter = new Converter<Foo, Bar>() {
    public List<Bar> convert(List<Foo> input) {
        ...
    }
}
Sign up to request clarification or add additional context in comments.

Comments

4

Try:

public <T, U> ArrayList<U> convert(Class<T> typeIn, ArrayList<T> input){     
    // dispatch on typeIn
}

Or better yet

public <T, U, V extends ArrayList<U>> V convert(Class<T> typeIn, 
Class<V> typeOut, ArrayList<T> input){     
    // dispatch on typeIn
    return typeOut.cast(yourConversionResult);
}

Because you might return ArrayList<Foo> or ArrayList<Bar> within the same method and having the proper cast will help you return them without compiler warnings.

Edit: The return type cast for the second sample wasn't going to work. Tried to fix it

4 Comments

I'm thinking this might be better than my answer. (By the way, you can add a "public" in there to get the syntax highlighting off of XML mode).
I'm not familiar with the <T, U> syntax. Could you provide a more verbose example, or perhaps a link?
@KevMo: It means the method is generic, even though the containing class might not be.
Thanks. Not sure about the return type cast though.
0

You are basically describing a "map" in terms of functional programming. Take a list of objects, apply some operation to each object and accumulate the results in another list. There are libraries out there that implement this stuff already, although i haven't looked recently. I know commons collections has this for pre-generics collections.

the gist of the solution is (similar to mmeyers solution):

public interface Function<From,To> {
  public To apply(From);
}

public <From,To> List<To> map(List<From> fromList, Function<From,To> fun)  {
  // call fun.apply() on every element in fromList and return a new result list ...
}

1 Comment

You are #18 to misspell my name on SO. Congratulations. ;)

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.