0

I have an enum, e.g.

public enum TimerCommandType {
   DATAPUBLISH,
   CLEARCACHE
}

and I want to be able to generate a class using Generics, so it looks like:

TimerCommandClass<DATAPUBLISH> 

which will use the enum value inside the class when relevant.

Is this possible?

The reason I would like to have it is that I currently have many calls to Quartz Jobs like:

 public JobDetailFactoryBean publishingJobDetail() {
    return QuartzSchedulerHelper.createJobDetail(QuartzPublishingJob.class);
}

and I want them to be replaced to:

QuartzSchedulerHelper.createJobDetail(TimerCommandClass<DATAPUBLISH>.class);

where createJobDetail is simply:

static JobDetailFactoryBean createJobDetail(Class jobClass) {
    JobDetailFactoryBean factoryBean = new JobDetailFactoryBean();
    factoryBean.setJobClass(jobClass);
    return factoryBean;
}
TimeToCode
  • 901
  • 2
  • 16
  • 34
onkami
  • 8,791
  • 17
  • 90
  • 176
  • 3
    Generic types are types. `DATAPUBLISH` is not a type; it's an instance of `TimerCommandType`. – khelwood Jan 20 '17 at 10:49
  • "A generic type is a generic class or interface that is parameterized over types" As stated in the javadoc Thus DATAPUBLISH will not work as you stated above. – Abhishekkumar Jan 20 '17 at 10:54
  • Why don't you simply define `class DataPublish` and `class ClearCache`? – Andy Turner Jan 20 '17 at 10:56
  • Why do you think that you need generics in your case ? Do you have a method that returns a `TimeCommandType` or takes a `TimeCommandType` as argument ? – Raphaël Jan 20 '17 at 10:59

2 Answers2

4

DATAPUBLISH is a value, not a type, so it can not be used as a type parameter.

Even if it was possible, type parameters are erased at runtime, so TimerCommandClass<DATAPUBLISH>.class would be the same as TimerCommandClass.class.

WilQu
  • 7,131
  • 6
  • 30
  • 38
0

TimerCommandClass<DATAPUBLISH> this doesn't make sense it should be parametrized with the class so in our case TimerCommandType not with the instance of the class DATAPUBLISH. To pass the instance you can use the methods or constructor parameters.

Lukas
  • 49
  • 1
  • 5