Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Controller {
- private static final Comparator<Node> PRIORITY_COMPARATOR = new Comparator<Node>() {
- @Override
- public int compare(Node o1, Node o2) {
- return o1.priority().getPriority().ordinal() - o2.priority().getPriority().ordinal();
- }
- };
- private final PriorityQueue<Node> children = new PriorityQueue<Node>(4, PRIORITY_COMPARATOR);
- public Controller(Node... nodes) {
- for (Node n : nodes) {
- children.add(n);
- }
- }
- public void addNodes(Node... nodes) {
- for (Node n : nodes) {
- children.add(n);
- }
- }
- public void clearNodes() {
- children.clear();
- }
- public void removeNodes(Node... nodes) {
- for (Node n : nodes) {
- children.remove(n);
- }
- }
- public PriorityQueue<Node> getNodes() {
- return children;
- }
- public Node getCurrentNode() {
- List<Node> exeucuteableNodes = new ArrayList<Node>();
- for (Node n : children) {
- if (n.validate()) {
- exeucuteableNodes.add(n);
- }
- }
- for (Node n : exeucuteableNodes) {
- if (getHighest(exeucuteableNodes) == n.priority().getPriority().ordinal()) {
- return n;
- }
- }
- return null;
- }
- private int getHighest(List<Node> exeucuteableNodes) {
- int highest = exeucuteableNodes.get(0).priority().getPriority().ordinal();
- for (Node n : exeucuteableNodes) {
- if (n.priority().getPriority().ordinal() > highest) {
- highest = n.priority().getPriority().ordinal();
- }
- }
- return highest;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement