1
public interface UnivariateOperator<T> {
    public TimeSeries<T> operateOn(TimeSeries<T> timeseries);
}

public class SamplingOperator<T> implements UnivariateOperator<T>  {    
    @Override
    public TimeSeries<T> sample(TimeSeries<T> timeseries) {
        ...
    }
}

Is there a way to use wildcards so the sampling operator can work with any type? I don't really want to have to specify the type for the sampling operator...it should work with any typed timeseries.

Jeff Axelrod
  • 27,676
  • 31
  • 147
  • 246
DD.
  • 21,498
  • 52
  • 157
  • 246
  • 1
    http://stackoverflow.com/questions/252055/java-generics-wildcards Does this not do something similar to what you want ? – Ender Wiggin Mar 13 '12 at 23:32
  • In your code example T is the type of of the TimeSeries so you wouldn't be specifying the type of the SamplingOperator, just the TimeSeries. – Caps Mar 13 '12 at 23:38
  • Your question is confusing: The interface has a `operateOn` method, but the subclass has a `sample` method which overrides something. You want the whole subclass or just `sample` to be independent of type? Maybe SamplingOperator should not be an implementation of UnivariateOperator. – toto2 Mar 14 '12 at 00:46
  • Show us some code that demonstrates how you would like to call/use your desired class. – Bohemian Mar 14 '12 at 08:44

3 Answers3

0

You can't, because you need to specify the generic of UnivariateOperator. If you just want a generic method that samples TimeSeries, you will need something like

public class TimeSeriesSampler {
    public static <T> TimeSeries<T> sample(TimeSeries<T> timeseries) {
        ...
    }
}

but if you want a SamplingOperator to implements UnivariantOperator, you will need to specify the generic. If you still don't want to specify, you could use something as

public class SamplingOperator implements UnivariateOperatior<Object> {
    private SamplingOperator(){
    }
    public <T> TimeSeries<T> sample(TimeSeries<T> timeseries) {
        return null;
    }
    @Override
    public TimeSeries<Object> operateOn(TimeSeries<Object> timeseries) {
        ...
    }
 }

but you will lose the power of the generic. Another way is

public class SamplingOperator<S> implements UnivariateOperatior<S> {
    private SamplingOperator(){
    }
    public <T> TimeSeries<T> sample(TimeSeries<T> timeseries) {
        return null;
    }
    @Override
    public TimeSeries<S> operateOn(TimeSeries<S> timeseries) {
        return timeseries;
    }
}

but it "smells" bad, as the sample method gives a feeling of a class method, instead of an instance one. It's your choice what's bst to do.

Caesar Ralf
  • 2,203
  • 1
  • 18
  • 36
  • You forgot your return type for your factory method. Also, you cannot `implement UnivariateOperator extends Object>` because you're not allowed to use a wildcard when specifying a supertype. – Jeffrey Mar 13 '12 at 23:43
  • Just don't use in this way then. Anyway, the factory works. You didn't need to negative the answer u.u – Caesar Ralf Mar 14 '12 at 19:13
  • Your answer is wrong. You cannot even compile your first suggestion. If you edit that part out I'd gladly flip my vote around. – Jeffrey Mar 14 '12 at 21:08
0

What if you did something like this:

public class SamplingOperator<T> implements UnivariateOperator<T>  {   
    private SamplingOperator(){
    }

    @Override
    public TimeSeries<T> sample(TimeSeries<T> timeseries) {
        ...
    }

    public static SamplingOperator<? extends Object> getInstance() {
        return new SamplingOperator<Object>();
    }
}

This ensures that any instance of SamplingOperator will be able to accept any type of TimeSeries as an argument to its sample method.

There are probably better solutions out there, but this one will work.

Jeffrey
  • 44,417
  • 8
  • 90
  • 141
-2
implements UnivariateOperator<Object>
Jakub Zaverka
  • 8,816
  • 3
  • 32
  • 48