Generic Method Java

I need use this method with three distinct classes: Orders, Customers, Suppliers

public void saveObjectKey(String filename, Long start) {
    Query<Orders> query = datastore.createQuery(Orders.class)
            .field("O_ORDERKEY").greaterThan(start);
    for (Orders orders : query.fetch()) {
        Util.writeFile(orders.getPrimaryKey().toString(), filename);
    }
}

I had thought in an ugly solution, a if else if solution:

if(instanceof Orders).... else if(instanceof Customers)..... else if(instanceof Suppliers)....

Now i need a generic solution, how can i do it?

Answers


Can this make sense in your application:

public <T> void saveObjectKey(String filename, Long start, Class<T> clazz, String keyName) {
    Query<T> query = datastore.createQuery(clazz).field(keyName).greaterThan(start);
    for (T o : query.fetch()) {
        Util.writeFile(o.getPrimaryKey().toString(), filename);
    }
}

Then you can use it like:

saveObjectKey(filename, start, Customers.class, customersKeyName);
saveObjectKey(filename, start, Suppliers.class, suppliersKeyName);
saveObjectKey(filename, start, Orders.class, ordersKeyName);

Need Your Help

Using both Eclipse and NetBeans on the same project

java eclipse netbeans

Eclipse is a really great editor, which I prefer to use, but the GUI design tools for Eclipse are lacking. On the other hand, NetBeans works really well for GUI design.

How do I docuSign multiple documents in an envelope once using docuSign Rest API?

docusignapi

I am creating composite templates that work correctly and I'm running into this issue.