2

I am currently writing an function for manipulate several ArrayList.

All the elements have same constructor, how can I do it like below?

Class A{
public A (Cursor data){...}
}

Class B{
public B (Cursor data){...}
}

Class C{
public C (Cursor data){...}
}

public void dataManipulation(ArrayList<**?**> list, Cursor cursor){
cursor.moveToFirst();
for (int i = 0; i < cursor.getCount(); i++) {
            list(new **?**(cursor));
            if (!cursor.isAfterLast())
                cursor.moveToNext();

}

public void main(){
ArrayList<A> listA = new ArrayList<A>();
ArrayList<B> listB = new ArrayList<B>();
ArrayList<C> listC = new ArrayList<C>();

Cursor cursor = IMPORT_CURSOR_FROM_FILE;

dataManipulation(listA, cursor);
dataManipulation(listB, cursor);
dataManipulation(listC, cursor);
}
4
  • 3
    Create a common interface for A, B and C to implement which guarantees compatibility for the functionality that you require Commented Nov 28, 2014 at 5:04
  • Thanks for reply, could you show some sample code? As I am a newbie of programming Commented Nov 28, 2014 at 5:05
  • 1
    You don't call your example right. I'm not sure where you're getting your cursor from that main method (which isn't the same as public static void main(String[] args), mind you). Also, what is list(new **?**(cursor)) meant to represent? Commented Nov 28, 2014 at 5:15
  • My fault, I just forgot cursor is needed Commented Nov 28, 2014 at 6:54

3 Answers 3

4

Start by taking a look at Interfaces and Inheritance, basically an interface is a contract that gurentees that any object that implements that interface will provide the contractual functionality...

For example...

public interface CursorContainer {
    public Cursor getCursor();
}

You "common" class would implement this interface and provide implementations for the getCursor (and any other required) method...

Class A implements CursorContainer {
    public A (Cursor data){...}
}

Class B implements CursorContainer  {
    public B (Cursor data){...}
}

Class C implements CursorContainer  {
    public C (Cursor data){...}
}

Then you could use generics...

public void dataManipulation(ArrayList<CursorContainer> list, Cursor cursor){

Your next problem is know how to create a particular implementation, for this, you will need a factory...

public interface CursorContainerFactory {
    public CursorContainer create(Cursor cursor);
}

You would need a factory for each type of container you want to create...

public Class CursorContainerAFactory implements CursorContainerFactory {
    public CursorContainer create(Cursor cursor) {
        return new A(cursor);
    }
}

You would then need to supply the factory to your dataManipulation method...

public void dataManipulation(ArrayList<CursorContainer> list, CursorContainerFactory factory, Cursor cursor){
    cursor.moveToFirst();
    for (int i = 0; i < cursor.getCount(); i++) {
        list(factory.create(cursor));
        if (!cursor.isAfterLast())
            cursor.moveToNext();

}

And finally, call it...

dataManipulation(listA, new CursorContainerAFactory(), cursor);

Remember, classes can implement many interfaces, but only extend from one...

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

6 Comments

Is there any reason that it couldn't be List<? extends CursorContainer>?
If that's the case, then it could be shifted to List<? super CursorContainer>. I didn't see anywhere that we were writing to the list at first (I guess that's what list(new **?**(cursor)); means), but I also don't see anywhere that we read from the list, either.
Just to spoil your fun even further, implements has an s on the end.
@ajb I also implemented CursorContainer on the factory implementation, but at least I caught that one :P
@Makoto Sure, but what's the benefit (of <? super CursorContainer> over <CursorContainer>)? I'm assuming the OP wants to read it later...
|
2

You would need to create an interface for each of your classes (A, B, C) to implement.

See this: https://docs.oracle.com/javase/tutorial/java/concepts/interface.html

4 Comments

Hmm, and what about list(new **?**(cursor));?
You could just substitute cursor for A,B, or C
But how do you know which one is to be used?
I don't, since I have no clue what the program is being used for. I would imagine you could use a switch statement for this, however.
1

You might use reflection, but you'll need to pass in the Class because of type-erasure. Something like,

public <T> void dataManipulation(Class<T> cls, ArrayList<T> list,
        Cursor cursor) {
    cursor.moveToFirst();
    for (int i = 0; i < cursor.getCount(); i++) {
        Constructor<T>[] ctors = (Constructor<T>[]) cls.getConstructors();
        for (Constructor<T> c : ctors) {
            Type[] types = c.getGenericParameterTypes();
            if (types.length == 1) {
                if (types[0].getClass().equals(Cursor.class)) {
                    list.add(c.newInstance(cursor));
                    break;
                }
            }
        }
        if (!cursor.isAfterLast()) {
            cursor.moveToNext();
        }
    }
}

Comments

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.