I would like to have something like this in Java :
import java.util.List;
class Query<T> {
public List<T> fetch() {
// Get the table name, T.tableName()
// Fetch from /api/<table_name>
// Parse and return data, T.parseJSON(jsonData)
}
}
The intention here is to have a generic query builder, while each class T defines its specific tableName and parseJSON functions. For example,
class Article {
public static String tableName() {
return "article";
}
public static List<Article> parseJSON(String jsonData) {
// Parse the json, build the articles
}
}
How do I write the fetch function in the Query class?
Simply pass an instance of type T so you have access to the methods of that type. Notice the comments..
public List<T> fetch(T instance) {
// Get the table name, instance.TableName();
// Fetch from /api/<table_name>
// Parse and return data, instance.parseJSON(jsonData)
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With