Advertisement
Guest User

Untitled

a guest
Dec 26th, 2014
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.74 KB | None | 0 0
  1. public class Controller {
  2.  
  3. private static final Comparator<Node> PRIORITY_COMPARATOR = new Comparator<Node>() {
  4. @Override
  5. public int compare(Node o1, Node o2) {
  6. return o1.priority().getPriority().ordinal() - o2.priority().getPriority().ordinal();
  7. }
  8. };
  9. private final PriorityQueue<Node> children = new PriorityQueue<Node>(4, PRIORITY_COMPARATOR);
  10.  
  11.  
  12.  
  13. public Controller(Node... nodes) {
  14. for (Node n : nodes) {
  15. children.add(n);
  16. }
  17. }
  18.  
  19. public void addNodes(Node... nodes) {
  20. for (Node n : nodes) {
  21. children.add(n);
  22. }
  23. }
  24.  
  25. public void clearNodes() {
  26. children.clear();
  27. }
  28.  
  29. public void removeNodes(Node... nodes) {
  30. for (Node n : nodes) {
  31. children.remove(n);
  32. }
  33. }
  34.  
  35. public PriorityQueue<Node> getNodes() {
  36. return children;
  37. }
  38.  
  39. public Node getCurrentNode() {
  40. List<Node> exeucuteableNodes = new ArrayList<Node>();
  41. for (Node n : children) {
  42. if (n.validate()) {
  43. exeucuteableNodes.add(n);
  44. }
  45. }
  46. for (Node n : exeucuteableNodes) {
  47. if (getHighest(exeucuteableNodes) == n.priority().getPriority().ordinal()) {
  48. return n;
  49. }
  50. }
  51. return null;
  52. }
  53.  
  54. private int getHighest(List<Node> exeucuteableNodes) {
  55. int highest = exeucuteableNodes.get(0).priority().getPriority().ordinal();
  56. for (Node n : exeucuteableNodes) {
  57. if (n.priority().getPriority().ordinal() > highest) {
  58. highest = n.priority().getPriority().ordinal();
  59. }
  60. }
  61. return highest;
  62. }
  63.  
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement