I am writing an Agent in Java that receives requests for its services from various other objects in the program. The restriction is that only one process can be done at once, which means that a PriorityQueue is probably the best way to represent requests for its services.
Unfortunately, these processes are stored as a enum with many different states. Is there an easy way to write a Comparator to order these states in the way I want? That is,
public enum AgentProcess
{
ACTION1, ACTION2, ACTION3, ACTION4, ACTION20
}
with some Comparator
public class ProcessComparator<Process>
{
public int compare(Process a, Process b)
{
//some arbitrary ordering of the processes, e.g., ACTION3 > ACTION19 > ACTION4...
}
}
I’m currently stuck with doing something like
public static int getValue(Process p)
{
switch(p)
case ACTION1:
return 5;
case ACTION2:
return 29;
case ACTION3:
return 18;
//etc
}
Is there a way I could rewrite my enum so that it is naturally ordered, without having to define weights or a switch for each?
Three solutions come to my mind: