Let\'s say I have an abstract class
public abstract class Trainer{}
I have specific trainers like :
pub
To specify a bound to an interface and that it be an enum, you need a generic intersection, which looks like:
class MyClass & SomeInterface> {}
Note that when intersecting a class and an interface(s), the class must appear before the interface(s).
In this case, the generics Kung Fu you want is one level more complicated, because the interface of the TrainingActions enum must itself refer back to the animal type.
class Trainer & TrainingActions> {}
A complete working example, based on your posted code, that compiles is:
public class Animal {}
public interface TrainingActions {}
/**
* A trainer that can teach an animal a suitable set of tricks
* @param The type of Animal
* @param The enum of TrainingActions that can be taught to the specified Animal
*/
public abstract class Trainer & TrainingActions> {
private Set completed = new HashSet();
public void trainingComplete(T t) {
completed.add(t);
}
}
public class Dog extends Animal {};
public class DogTrainer extends Trainer {
public enum Trainables implements TrainingActions {
BARK, BITE, ROLLOVER, FETCH;
}
}
But I would go one step further and define several TrainingActions
enums in the class of the particular Animal
to which they apply:
public class Dog extends Animal {
public enum BasicTrainables implements TrainingActions {
SIT, COME, STAY, HEEL;
}
public enum IntermediateTrainables implements TrainingActions {
BARK, BITE, ROLLOVER, FETCH;
}
public enum AdvacedTrainables implements TrainingActions {
SNIFF_DRUGS, FIND_PERSON, ATTACK, GUARD;
}
};
public class PuppyTrainer extends Trainer {}
public class ObedienceTrainer extends Trainer {}
public class PoliceTrainer extends Trainer {}