airdump
airdump

Reputation: 653

Avoid cast in a generics hierarchy

I have some difficulty to simplify more the problem. Sorry if they are too many code here. I try to improve the architecture of the code above because I hate warning and cast and I feel something wrong.

Now, the code. I have a util class with these two parametrized methods (same signature as OpenJPA's CriteriaBuilder...)

public class MyUtil {
    public void equal(List<?> l, Object value) {
        // do something (see CriteriaBuilder.equal method)
    }

    public <Y extends Comparable<? super Y>> void greaterThan(List<? extends Y> l, Y value) {
        // do something (see CriteriaBuilder.greaterThan method)
    }
}

Then, I want to be able to abstract them to call it via an interface.

public interface IOperation<T> {
    // maybe make this method generic ? but how ?
    public abstract void doOp(List<T> l, T value); 
}

public abstract class AbstractOperation<T> implements IOperation<T> {
    protected MyUtil myUtil;
}

public class EqualOp extends AbstractOperation<Object> {
    @Override
    public void doOp(List<Object> path, Object value) {
        myUtil.equal(path, value);
    }
}

public class GreaterThanOp<T extends Comparable<? super T>> extends AbstractOperation<T> {
    @Override
    public void doOp(List<T> path, T value) {
        myUtil.greaterThan(path, value);
    }
}

I create a factory

public class OperationFactory {
    private static OperationFactory instance;
    public static OperationFactory getInstance() {...}

    public IOperation<?> get(String op) {
        if ("=".equals(op)) {
            return new EqualOp();
        } else if (">".equals(op)) {
            return new GreaterThanOp<Comparable<? super Object>>();
        }
        throw new InvalidParameterException();
    }
}

Then I use it :

public class Client {
    public void needOp(String op) {
        IOperation<String> operation = (IOperation<String>) OperationFactory.getInstance().get(op); // How to avoid this cast ?
        List<String> l = null;
        operation.doOp(l, "a string");
    }
}

My question is : is it possible to avoid this cast in the Client class ? How ? Is there a way to have a better architecture ?

Thanks for reading

Upvotes: 0

Views: 120

Answers (1)

TimK
TimK

Reputation: 4835

I'm assuming you can require your type to be Comparable.

Parameterize EqualOp like GreaterThanOp:

public class EqualOp<T extends Comparable<T>> extends AbstractOperation<T> {
    @Override public void doOp(List<T> path, T value) ...

And define get() like this:

public <T extends Comparable<T>> IOperation<T> get(String op) {
    if ("=".equals(op)) {
        return new EqualOp<T>();
    } else if (">".equals(op)) {
        return new GreaterThanOp<T>();
    }
    ...

Upvotes: 2

Related Questions