From 091dcdf855a3f07b6cf6663cbfd18c4794370b19 Mon Sep 17 00:00:00 2001 From: Rik Veenboer Date: Tue, 8 Feb 2011 14:38:20 +0000 Subject: [PATCH] Introductie type ArrayCycle en consequent maken van add en remove methodes: nu zonder type in de naam. --- java/src/pm/Main.java | 29 ++++++++++++----------------- java/src/pm/util/ArrayCycle.java | 31 +++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 17 deletions(-) create mode 100644 java/src/pm/util/ArrayCycle.java diff --git a/java/src/pm/Main.java b/java/src/pm/Main.java index 099db23..8d190eb 100644 --- a/java/src/pm/Main.java +++ b/java/src/pm/Main.java @@ -12,50 +12,47 @@ import pm.exception.ActionException; import pm.exception.action.NotImplementedActionException; import pm.exception.action.UnknownTargetException; import pm.listener.ActionListener; +import pm.util.ArrayCycle; public class Main { protected static final int SLEEP = 100; - ArrayList applicationList; + ArrayCycle applicationCycle; ArrayList deviceList; Queue actionQueue; boolean run; - Application currentApplication; public Main() { - applicationList = new ArrayList(); - //applicationList.iterator(); + applicationCycle = new ArrayCycle(); deviceList = new ArrayList(); actionQueue = new ConcurrentLinkedQueue(); - ActionListener.initialise(actionQueue); } - public void addApplication(Application application) { - applicationList.add(application); + public void add(Application application) { + applicationCycle.add(application); } - public boolean removeApplication(Application application) { - return applicationList.remove(application); + public boolean remove(Application application) { + return applicationCycle.remove(application); } - public void addDevice(Device device) { + public void add(Device device) { deviceList.add(device); } - public boolean removeDevie(Device device) { + public boolean remove(Device device) { return deviceList.remove(device); } public void start() throws Exception { //addDevice(new ExampleDevice()); //addDevice(new RumblepadDevice()); - addDevice(new Extreme3DDevice()); + add(new Extreme3DDevice()); Application application = new VoorbeeldApplication(); - addApplication(application); - currentApplication = application; + add(application); for (Device device : deviceList) { device.start(); @@ -67,7 +64,6 @@ public class Main { public void run() throws ActionException { run = true; while (run) { - //System.out.println("Print!"); if (actionQueue.isEmpty()) { try { Thread.sleep(SLEEP); @@ -80,7 +76,7 @@ public class Main { object = this; break; case APPLICATION: - object = currentApplication; + object = applicationCycle.current(); break; default: throw new UnknownTargetException(); @@ -99,7 +95,6 @@ public class Main { for (Device device : deviceList) { device.exit(); } - System.out.println("Als ie nu niet uit gaat, dan hebben we een verstekeling! Dat is vervelend ende naar!"); } public static void main(String[] args) { diff --git a/java/src/pm/util/ArrayCycle.java b/java/src/pm/util/ArrayCycle.java new file mode 100644 index 0000000..f562ebd --- /dev/null +++ b/java/src/pm/util/ArrayCycle.java @@ -0,0 +1,31 @@ +package pm.util; + +import java.util.ArrayList; + +public class ArrayCycle extends ArrayList { + protected static final long serialVersionUID = 1L; + + protected int index = 0; + + public T current() { + return this.get(index); + } + + public T previous() { + if (--index < 0) { + index = Math.max(0, size() - 1); + } + return get(index); + } + + public T next() { + if (++index >= size()) { + index = 0; + } + return get(index); + } + + public T reset() { + return get(index = 0); + } +}