0

I have a class that should accept different datatypes as the second constructor parameter:

public abstract class QueryMatch {
String key;
Object input;

public <T> QueryMatch(String key, T o) {
    this.key = key;
    input = o;
}

public String getKey() {
    return key;
}

public Object getValue() {
    return input;
}
}

I don't want to use type parameters, like

public abstract class QueryMatch<T>{
String key;
T input;
...

As this way I'm getting raw types warnings when declaring retrieving QueryMatch as a generic (as I don't know the datatype it contains). But the problem is that I need to return the value and I'm not totally comfortable by returning an Object (is that just me, but it doesn't seem like a good practice?).

Additionally, another class inherits from it:

public class QueryMatchOr extends QueryMatch {
public QueryMatchOr() {
    super("title", new ArrayList<String>());
}

public void addMatch(String match) {
    ((ArrayList<String>) input).add(match);
}

}

And of course I'm getting a Unchecked cast warning (which I can avoid with @SuppressWarnings(“unchecked”)).

So, my question is... is there a better way to achieve what I'm trying to do? An abstract class that contains an object (which could be bounded), and returning the datatype it contains (instead of an Object) without using a type parameter in the class declaration?

4

3 に答える 3