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; } 
4
  • 3
    Generic types are types. DATAPUBLISH is not a type; it's an instance of TimerCommandType. Commented Jan 20, 2017 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. Commented Jan 20, 2017 at 10:54
  • Why don't you simply define class DataPublish and class ClearCache? Commented Jan 20, 2017 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 ? Commented Jan 20, 2017 at 10:59

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.