package brute;
import java.util.ArrayList;
import java.util.Iterator;
import linda.ILinda;
import linda.LindaArgs;
public class BruteLinda implements ILinda {
ArrayList<LindaArgs> tuplespace = new ArrayList<LindaArgs>();
@Override
public void output(LindaArgs args) {
synchronized (tuplespace) {
tuplespace.add(args);
}
}
@Override
public void input(LindaArgs pattern) {
while (true) {
synchronized (tuplespace) {
for (Iterator<LindaArgs> iterator = tuplespace.iterator(); iterator.hasNext();) {
LindaArgs tuple = iterator.next();
if (tuple.matchTuples(pattern)) {
LindaArgs result = new LindaArgs(tuple);
iterator.remove();
pattern.applyResult(result);
return;
}
}
}
}
}
@Override
public void read(LindaArgs pattern) {
while (true) {
synchronized (tuplespace) {
for (Iterator<LindaArgs> iterator = tuplespace.iterator(); iterator.hasNext();) {
LindaArgs tuple = iterator.next();
if (tuple.matchTuples(pattern)) {
LindaArgs result = new LindaArgs(tuple);
pattern.applyResult(result);
return;
}
}
}
}
}
@Override
public boolean tryInput(LindaArgs pattern) {
synchronized (tuplespace) {
for (Iterator<LindaArgs> iterator = tuplespace.iterator(); iterator.hasNext();) {
LindaArgs tuple = iterator.next();
if (tuple.matchTuples(pattern)) {
LindaArgs result = new LindaArgs(tuple);
iterator.remove();
pattern.applyResult(result);
return true;
}
}
}
return false;
}
@Override
public boolean tryRead(LindaArgs pattern) {
synchronized (tuplespace) {
for (Iterator<LindaArgs> iterator = tuplespace.iterator(); iterator.hasNext();) {
LindaArgs tuple = iterator.next();
if (tuple.matchTuples(pattern)) {
LindaArgs result = new LindaArgs(tuple);
pattern.applyResult(result);
return true;
}
}
}
return false;
}
}