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