fix: remplacement des for loop par des hasNext

chore: wrote some docstrings
This commit is contained in:
Laureηt 2021-12-28 14:46:33 +01:00
parent c3cbb68709
commit 7b3fe8f998
No known key found for this signature in database
GPG key ID: D88C6B294FD40994

View file

@ -7,6 +7,7 @@ import linda.Tuple;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.locks.Condition;
@ -59,11 +60,21 @@ public class CentralizedLinda implements Linda {
this.reveils = Collections.synchronizedList(new ArrayList<Reveil>());
}
/**
* Insert a tuple into the tuple list. (non-blocking)
*
* @param template the Tuple template your looking for
*/
public void write(Tuple t) {
tuples.add(t);
reveils.forEach(r -> r.reveil(t));
}
/**
* Extract the tuple from the tuple list. (blocking)
*
* @param template the Tuple template your looking for
*/
public Tuple take(Tuple template) {
Reveil reveil = new Reveil(template);
reveils.add(reveil);
@ -79,6 +90,11 @@ public class CentralizedLinda implements Linda {
return result;
}
/**
* Get the tuple from the tuple list. (blocking)
*
* @param template the Tuple template your looking for
*/
public Tuple read(Tuple template) {
Reveil reveil = new Reveil(template);
reveils.add(reveil);
@ -94,36 +110,52 @@ public class CentralizedLinda implements Linda {
return result;
}
/**
* Extract the tuple from the tuple list. (non-blocking)
*
* @param template the Tuple template your looking for
*/
public Tuple tryTake(Tuple template) {
// Extract the tuple from the tuple list
// TODO: faire avec iterator, hasNext, pour être thread safe :)
for (int i = 0; i < tuples.size(); i++) {
if (tuples.get(i).matches(template)) {
return tuples.remove(i);
Iterator<Tuple> iterator = tuples.iterator();
Tuple next = null;
while (iterator.hasNext()) {
next = iterator.next();
if (next.matches(template)) {
iterator.remove();
break;
}
}
return null;
return next;
}
/**
* Get the tuple from the tuple list. (non-blocking)
*
* @param template the Tuple template your looking for
*/
public Tuple tryRead(Tuple template) {
Tuple result = null;
Iterator<Tuple> iterator = tuples.iterator();
Tuple next = null;
// Get the tuple from the tuple list
// TODO: faire avec iterator, hasNext, pour être thread safe :)
for (int i = 0; i < tuples.size(); i++) {
result = tuples.get(i);
if (result.matches(template)) {
return result;
while (iterator.hasNext()) {
next = iterator.next();
if (next.matches(template)) {
break;
}
}
return result;
return next;
}
/**
* Extract the tuples in the tuple list. (blocking)
*
* @param template the Tuple template your looking for
*/
public Collection<Tuple> takeAll(Tuple template) {
List<Tuple> results = new ArrayList<Tuple>();
Tuple result = new Tuple();
// Extract the tuples in the tuple list
while (result != null) {
result = tryTake(template);
if (result != null) {
@ -133,16 +165,21 @@ public class CentralizedLinda implements Linda {
return results;
}
/**
* Extract the tuples in the tuple list. (blocking)
*
* @param template the Tuple template your looking for
*/
public Collection<Tuple> readAll(Tuple template) {
List<Tuple> results = new ArrayList<Tuple>();
Tuple result;
Iterator<Tuple> iterator = tuples.iterator();
Tuple next = null;
// Extract the tuples in the tuple list
// TODO: faire avec iterator, hasNext, pour être thread safe :)
for (int i = 0; i < tuples.size(); i++) {
result = tuples.get(i);
if (result.matches(template))
results.add(result);
while (iterator.hasNext()) {
next = iterator.next();
if (next.matches(template)) {
results.add(next);
}
}
return results;
}