feat: changed pipeline return type to optional for better clarity, and added comments

This commit is contained in:
Yannik Bretschneider 2021-05-26 13:57:47 +02:00
parent 0acc0732f6
commit 741f907348
2 changed files with 22 additions and 10 deletions

@ -1 +1 @@
Subproject commit a4c510279a35bfd10da62b3026eb2ed4c3c369ad
Subproject commit 65c33d886c9c8f2d4a06356f32ad6d772b60e7ee

View File

@ -7,6 +7,7 @@ import uulm.teamname.marvelous.gamelibrary.requests.Request;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicBoolean;
/**
@ -32,22 +33,33 @@ public class Pipeline {
* {@link Event events} relevant to the {@link Segment segment}. The {@link Event Events} are returned at the
* end of the pipeline.
* @param requests are the requests that are being pipelined through the pipeline
* @return a {@link Tuple}<{@link Boolean}, {@link Event}[]>, whereby the {@link Boolean} declares whether the
* execution of the {@link Pipeline} is successful. <b>If that is the case, the events can be ignored,
* and an error message can be sent to the client</b>.
* @return a {@link Optional}<{@link Event}[]>, whereby the state of the {@link Optional} declares whether the
* execution of the {@link Pipeline} was successful. <b>If the optional is empty, the input requests were
* invalid, and an error message can be sent to the client</b>. To get the {@link Event Events}
* out of the {@link Optional}, first check whether the {@link Optional} is empty by doing
* {@link Optional#isEmpty()} or {@link Optional#isPresent()}, and act accordingly.
*/
public Tuple<Boolean, Event[]> processRequests(Request[] requests) {
public Optional<Event[]> processRequests(Request[] requests) {
// The packet carries the requests, and gets smaller per segment
List<Request> packet = Arrays.asList(requests);
// The packet is filled by the requests resulting from events per segment
List<Event> carrier = new ArrayList<>();
AtomicBoolean abort = new AtomicBoolean();
abort.set(false);
// The abort boolean describes whether an abort happened in a segment
AtomicBoolean abort = new AtomicBoolean(false);
// Loop through all segments
for (Segment segment: segments) {
// Give the segment the packet, carrier and abort, and let it process requests
segment.processRequests(packet, carrier, abort);
if (packet.size() == 0 || abort.get()) {
break;
if (packet.size() == 0 || abort.get()) { // if packet is empty (all requests processed) or abort initiated
break; // (abort boolean true), break out of the loop
}
}
return new Tuple<Boolean, Event[]> (abort.get(), carrier.toArray(new Event[0]));
if (abort.get()) { // if abort is true, return empty optional
return Optional.empty();
} else { // else return an optional of the array
return Optional.of(carrier.toArray(new Event[0]));
}
}
/**