I have an API that returns JSON lists of entities, such as Gene, Protein, Resource, etc. For example, the Protein endpoint returns:
{
next: "/api/1.0/protein?cursor=200",
entities: [
{
symbol: "TARSH_HUMAN",
href: "/api/1.0/protein/TARSH_HUMAN"
},
{
symbol: "ABL1_HUMAN",
href: "/api/1.0/protein/ABL1_HUMAN"
},
...
The Resource endpoint is exactly the same, except symbol is name.
Solution so far
What I'd like to do is create some sort of EntityListSchema that can generate such lists no matter what the entity is. Here's what I have:
public class EntityListSchema<T extends JsonModel> {
private String next;
private List<T> entities;
public EntityListSchema(Class<T> klass, int startAt) {
int nextInt = startAt + Constant.API_MAX_RESULTS;
this.next = "/"
+ Constant.API_URL + "/"
+ klass.getEndpoint() + "?" // <-- This is the problem
+ Constant.API_CURSOR + "=" + nextInt;
}
...
In principle, each Entity can then just extend JsonModel to ensure that getEndpoint is available, and we're done. But I'm seeing the error:
The method getEndpoint() is undefined for the type Class<T>
My understanding is that this error occurs because the method getEndpoint is not static. But if it were static (I'm using Java 8, so I can have static interfaces) I could not return the correct endpoint depending on the class.
Is it possible to achieve what I want? This would reduce a ton of duplicate code.
Class<T> klassdoesn't represent an instance of the class, you need only change for:T klass