-2

I am trying to separate return types from database function calls with subclasses of an abstract Output class. I am running into an issue where my abstract method (getResults) return type needs to be different for a ResultSet from a Date, or char, or string but they should all fall under an Output class or interface. The subclass that returns the ResultSet needs to return a List<Map<String,Object>>, Date should return List<Date>, String List<String>, etc...

Client Code:

LoadableStatement loadedStatementObject= new LoadableStatement.Builder()
            .addParam(new In(2, 111111))
            .addParam(new Out(1, OracleTypes.CURSOR))
            .addStatement(DBPackage.LOOKUP_RECORD.getStatement())
            .load();
Output cursor = new Cursor(loadedStatementObject);

abstract method in Output.java and subclasses of.

public class Output{
    //Constructor, fields etc...
    public abstract Collection getResults();
}


public class Cursor extends Output {
    private List<Map<String, Object>> results;

    public Cursor(LoadableStatement statement) {
        super(statement);
    }

    @Override
    public Collection getResults() {
        //Logic to convert some type of list from DB into a 
        //List<Map<String,Object>>
        return new ArrayList<Map<String, Object>>();
    }
}

public class StringResult extends Output {

    public StringResult(LoadableStatement statement) {
        super(statement);
    }

    @Override
    public Collection getResults() {
    //Logic that converts a list from DB into a List<String>
    }

}

How can I accomplish this?

4
  • What have you tried so far? Commented Oct 1, 2019 at 2:21
  • I have tried using generics, albeit incorrectly i am sure. I have tried List<?>, Collection<? extends E>, List<Object>. I am pretty sure generics could solve my problem, but I don’t know how to use them correctly.
    – Anden
    Commented Oct 1, 2019 at 2:26
  • Then share what you have done too. Commented Oct 1, 2019 at 2:26
  • Ravindra, I have added code.
    – Anden
    Commented Oct 1, 2019 at 21:15

2 Answers 2

1

Try something like this:

public class LoadableStatement {}

public abstract class Output<E> {
  //Constructor, fields etc...
  public abstract Collection<E> getResults();
  public Output(LoadableStatement statement) {
    /* Some implementation ... */
  }

}

public class Cursor extends Output<Map<String, Object>> {
  private List<Map<String, Object>> results;

  public Cursor(LoadableStatement statement) {
    super(statement);
  }

  @Override
  public Collection<Map<String, Object>> getResults() {
    //Logic to convert some type of list from DB into a 
    //List<Map<String,Object>>
    return new ArrayList<>();
  }
}

public class StringResult extends Output<String> {

  public StringResult(LoadableStatement statement) {
    super(statement);
  }

  @Override
  public Collection<String> getResults() {
    //Logic that converts a list from DB into a List<String>
    return new ArrayList<>();
  }

}
0

You should define a generic version of Output class. Which has a generic function with a generic return type. Here is some sample code:

public class Output<T>{
//fields and constructor
public T getResults(); 
}

here T is for Type you can use any identifier.

Note: While using collections please keep in mind that primitives are not allowed in a collection.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.