Reputation: 374
I'm trying to create two different classes that represent different options of exercises the user can choose to sort by. After one class is chosen, the other is no longer an option, because of this I create an ArrayList in both classes of classes which are not allowed to follow it.
The problem is that since they are not able to follow each other, when these classes are constructed it results in an infinite loop, is there any way I can avoid this?
PushPullLegs Class
public class PushPullLegs extends SortingGroup implements Serializable{
public PushPullLegs(){
this.setName("Push,Pull,Legs");
this.addCantFollow(new MuscleGroup());
SortingCategory push = new SortingCategory("Push","PPL","Push");
this.addOption(push);
SortingCategory pull = new SortingCategory("Pull","PPL","Pull");
this.addOption(pull);
SortingCategory legs = new SortingCategory("Legs","PPL","Legs");
this.addOption(legs);
}
}
MuscleGroup Class
public class MuscleGroup extends SortingGroup implements Serializable {
public MuscleGroup(){
this.addCantFollow(new PushPullLegs());
SortingCategory chest = new SortingCategory("Chest","Primary","Chest");
chest.addNewOptions(new ChestMovementPatterns());
this.addOption(chest);
SortingCategory triceps = new SortingCategory("Triceps","Primary","Triceps");
triceps.addNewOptions(new TricepMovementPatterns());
this.addOption(triceps);
SortingCategory lats = new SortingCategory("Lats","Primary","Lats");
this.addOption(lats);
SortingCategory quads = new SortingCategory("Quads","Primary","Quads");
this.addOption(quads);
SortingCategory hamstrings = new SortingCategory("Hamstrings","Primary","Hamstrings");
this.addOption(hamstrings);
}
}
Upvotes: 1
Views: 1211
Reputation: 159086
You would use code like this:
private Set<Class<? extends SortingGroup>> cantFollowClass = new HashSet<>();
public void addCantFollow(Class<? extends SortingGroup> clazz) {
this.cantFollowClass = clazz;
}
public boolean canFollow(SortingGroup group) {
return ! this.cantFollowClass.contains(group.getClass());
}
Upvotes: 1