java - Priority Queue with a complex enum? -
i writing agent in java receives requests services various other objects in program. restriction 1 process can done @ once, means priorityqueue best way represent requests services.
unfortunately, these processes stored enum many different states. there easy way write comparator order these states in way want? is,
public enum agentprocess { action1, action2, action3, action4, action20 }
with comparator
public class processcomparator<process> { public int compare(process a, process b) { //some arbitrary ordering of processes, e.g., action3 > action19 > action4... } }
i'm stuck doing like
public static int getvalue(process p) { switch(p) case action1: return 5; case action2: return 29; case action3: return 18; //etc }
is there way rewrite enum naturally ordered, without having define weights or switch each?
three solutions come mind:
- you specify enum constants in order need , use p.ordinal() in comparator.
- you specify ordering index parameter enum constant, e.g. action1(3), action2(4), action3(1), , provide constructor in enum stores parameter field, use in comparator.
- create list of processes add constants in order want, , use list.indexof in comparator. slower approach 1 or 2, might not problem depending using it.
Comments
Post a Comment