Introductie type ArrayCycle en consequent maken van add en remove methodes: nu zonder type in de naam.
This commit is contained in:
@@ -12,50 +12,47 @@ import pm.exception.ActionException;
|
|||||||
import pm.exception.action.NotImplementedActionException;
|
import pm.exception.action.NotImplementedActionException;
|
||||||
import pm.exception.action.UnknownTargetException;
|
import pm.exception.action.UnknownTargetException;
|
||||||
import pm.listener.ActionListener;
|
import pm.listener.ActionListener;
|
||||||
|
import pm.util.ArrayCycle;
|
||||||
|
|
||||||
public class Main {
|
public class Main {
|
||||||
protected static final int SLEEP = 100;
|
protected static final int SLEEP = 100;
|
||||||
|
|
||||||
ArrayList<Application> applicationList;
|
ArrayCycle<Application> applicationCycle;
|
||||||
ArrayList<Device> deviceList;
|
ArrayList<Device> deviceList;
|
||||||
Queue<Action> actionQueue;
|
Queue<Action> actionQueue;
|
||||||
|
|
||||||
boolean run;
|
boolean run;
|
||||||
Application currentApplication;
|
|
||||||
|
|
||||||
public Main() {
|
public Main() {
|
||||||
applicationList = new ArrayList<Application>();
|
applicationCycle = new ArrayCycle<Application>();
|
||||||
//applicationList.iterator();
|
|
||||||
deviceList = new ArrayList<Device>();
|
deviceList = new ArrayList<Device>();
|
||||||
actionQueue = new ConcurrentLinkedQueue<Action>();
|
actionQueue = new ConcurrentLinkedQueue<Action>();
|
||||||
|
|
||||||
ActionListener.initialise(actionQueue);
|
ActionListener.initialise(actionQueue);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addApplication(Application application) {
|
public void add(Application application) {
|
||||||
applicationList.add(application);
|
applicationCycle.add(application);
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean removeApplication(Application application) {
|
public boolean remove(Application application) {
|
||||||
return applicationList.remove(application);
|
return applicationCycle.remove(application);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addDevice(Device device) {
|
public void add(Device device) {
|
||||||
deviceList.add(device);
|
deviceList.add(device);
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean removeDevie(Device device) {
|
public boolean remove(Device device) {
|
||||||
return deviceList.remove(device);
|
return deviceList.remove(device);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void start() throws Exception {
|
public void start() throws Exception {
|
||||||
//addDevice(new ExampleDevice());
|
//addDevice(new ExampleDevice());
|
||||||
//addDevice(new RumblepadDevice());
|
//addDevice(new RumblepadDevice());
|
||||||
addDevice(new Extreme3DDevice());
|
add(new Extreme3DDevice());
|
||||||
|
|
||||||
Application application = new VoorbeeldApplication();
|
Application application = new VoorbeeldApplication();
|
||||||
addApplication(application);
|
add(application);
|
||||||
currentApplication = application;
|
|
||||||
|
|
||||||
for (Device device : deviceList) {
|
for (Device device : deviceList) {
|
||||||
device.start();
|
device.start();
|
||||||
@@ -67,7 +64,6 @@ public class Main {
|
|||||||
public void run() throws ActionException {
|
public void run() throws ActionException {
|
||||||
run = true;
|
run = true;
|
||||||
while (run) {
|
while (run) {
|
||||||
//System.out.println("Print!");
|
|
||||||
if (actionQueue.isEmpty()) {
|
if (actionQueue.isEmpty()) {
|
||||||
try {
|
try {
|
||||||
Thread.sleep(SLEEP);
|
Thread.sleep(SLEEP);
|
||||||
@@ -80,7 +76,7 @@ public class Main {
|
|||||||
object = this;
|
object = this;
|
||||||
break;
|
break;
|
||||||
case APPLICATION:
|
case APPLICATION:
|
||||||
object = currentApplication;
|
object = applicationCycle.current();
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
throw new UnknownTargetException();
|
throw new UnknownTargetException();
|
||||||
@@ -99,7 +95,6 @@ public class Main {
|
|||||||
for (Device device : deviceList) {
|
for (Device device : deviceList) {
|
||||||
device.exit();
|
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) {
|
public static void main(String[] args) {
|
||||||
|
|||||||
31
java/src/pm/util/ArrayCycle.java
Normal file
31
java/src/pm/util/ArrayCycle.java
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
package pm.util;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
|
public class ArrayCycle<T> extends ArrayList<T> {
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user