Booker Chan
Booker Chan

Reputation: 200

Java different type objects as same parameter

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

Upvotes: 2

Views: 90

Answers (3)

MadProgrammer
MadProgrammer

Reputation: 347204

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...

Upvotes: 4

Elliott Frisch
Elliott Frisch

Reputation: 201447

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

Upvotes: 1

frgnvola
frgnvola

Reputation: 518

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

Upvotes: 2

Related Questions