0

I'm just new in the spring architecture and I am wondering if it is possible to use a generic T (o what you want) in an ItemStreamReader.

Something like this:

public class Reader implements ItemStreamReader<T extends SomeClass>{
    public T read() {
    .......
    }
    public void open() {
    .......
    }
    public void update() {
    .......
    }
    public void close() {
    .......
    }
}

So I pass to the reader various objects that extends SomeClass.

2 Answers 2

1

this should work:

public class Reader<T extends SomeClass> implements ItemStreamReader<T>{
    public T read() {
    .......
    }
    public void open() {
    .......
    }
    public void update() {
    .......
    }
    public void close() {
    .......
    }
}

use it like:

Reader<SomeClass> reader = new Reader<>();
Reader<ExtendedFromSomeClass> reader2 = new Reader<>();
Sign up to request clarification or add additional context in comments.

Comments

0

I recommend you to read first about java generics.

http://www.angelikalanger.com/GenericsFAQ/FAQSections/ParameterizedTypes.html#What is a parameterized (or generic) type?

But if you define your class

public class Reader implements ItemStreamReader<SomeClass>{
    public SomeClass read() {
    .......
    }
    public void open() {
    .......
    }
    public void update() {
    .......
    }
    public void close() {
    .......
    }
}

Your method can return any object that is Subclass of SomeClass.

For example

Reader a = new Reader();
Subclass b = (Subclass)a.read();

1 Comment

Mmm.. my goal was to avoid the casting.. working on that I saw the more than one generic was necessary to implement correctly the class. It become something like public abstract class Reader<T extends XInterface<R>, R extends Y, S extends Z<S, R>> implements ItemStreamReader<T> where XYZ are classes or interfaces. But thanks the guide was helpful!

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.