diff options
| author | schneefux <schneefux+commit@schneefux.xyz> | 2018-09-27 12:51:37 +0200 |
|---|---|---|
| committer | schneefux <schneefux+commit@schneefux.xyz> | 2018-09-27 12:51:37 +0200 |
| commit | be1ce64c5aa213ee92594f3afcdef7a08c775df7 (patch) | |
| tree | 111c907c1cd36f0107b5d77043a9815a1e604129 /AlgoAufgabe1/src/de/ostfalia/algo/ws18/s1/Management.java | |
| parent | 72cc9abf555a7ad0609f3f5fa520e967915d893a (diff) | |
| download | algo-labor-be1ce64c5aa213ee92594f3afcdef7a08c775df7.tar.gz algo-labor-be1ce64c5aa213ee92594f3afcdef7a08c775df7.zip | |
Remove Optionals and refactor
Diffstat (limited to 'AlgoAufgabe1/src/de/ostfalia/algo/ws18/s1/Management.java')
| -rw-r--r-- | AlgoAufgabe1/src/de/ostfalia/algo/ws18/s1/Management.java | 51 |
1 files changed, 35 insertions, 16 deletions
diff --git a/AlgoAufgabe1/src/de/ostfalia/algo/ws18/s1/Management.java b/AlgoAufgabe1/src/de/ostfalia/algo/ws18/s1/Management.java index 2be04c7..59c5d20 100644 --- a/AlgoAufgabe1/src/de/ostfalia/algo/ws18/s1/Management.java +++ b/AlgoAufgabe1/src/de/ostfalia/algo/ws18/s1/Management.java @@ -4,7 +4,6 @@ import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.IOException; -import java.util.Optional; import java.util.function.Predicate; import de.ostfalia.algo.ws18.base.IManagement; @@ -13,14 +12,18 @@ import de.ostfalia.algo.ws18.base.KindOfSport; public class Management implements IManagement { /** - * Tracks the number of operations. + * Track the number of operations. */ private int numberOfOperations = 0; + /** + * Track the execution time. + */ + private long lastCheckpointNs; /** * Current head node. */ - private Optional<LinkedListNode<IMember>> head = Optional.empty(); + private LinkedListNode<IMember> head; /** * Length of the list. */ @@ -61,7 +64,7 @@ public class Management implements IManagement { * * @param filename Path to the CSV file. */ - private void importFromFile(String filename) { + public void importFromFile(String filename) { File file = new File(filename); try (BufferedReader bufferedReader = new BufferedReader(new FileReader(file))) { @@ -80,14 +83,14 @@ public class Management implements IManagement { */ private Management filter(Predicate<IMember> criteria, boolean breakAfterFirstMatch) { Management result = new Management(); - if (!this.head.isPresent()) { + if (this.size == 0) { return result; } // searches from head, inserts at head -> result is reversed result.reverse(); - LinkedListNode<IMember> currentNode = this.head.get(); + LinkedListNode<IMember> currentNode = this.head; do { // traverse = 1 operation this.numberOfOperations++; @@ -108,10 +111,15 @@ public class Management implements IManagement { * Find the first element that matches the given criteria. * * @param criteria Criteria to search for. - * @return An optional containing the first element that matches the given criteria. + * @return The first element that matches the given criteria or null. */ - private Optional<IMember> search(Predicate<IMember> criteria) { - return this.filter(criteria, true).head.map(head -> head.getValue()); + private IMember search(Predicate<IMember> criteria) { + Management filteredList = this.filter(criteria, true); + if (filteredList.size() == 0) { + return null; + } else { + return filteredList.head.getValue(); + } } /** @@ -150,10 +158,10 @@ public class Management implements IManagement { this.numberOfOperations++; LinkedListNode<IMember> newHead = new LinkedListNode<IMember>(value); - if (this.head.isPresent()) { - newHead.setNext(this.head.get()); + if (this.head != null) { + newHead.setNext(this.head); } - this.head = Optional.of(newHead); + this.head = newHead; this.size++; return true; // TODO? @@ -167,7 +175,7 @@ public class Management implements IManagement { */ @Override public IMember search(long key) { - return this.search(member -> member.getKey() == key).orElse(null); + return this.search(member -> member.getKey() == key); } /** @@ -180,7 +188,7 @@ public class Management implements IManagement { @Override public IMember search(String name, String firstName) { return this.search(member -> member.getName().equals(name) - && member.getFirstName().equals(firstName)).orElse(null); + && member.getFirstName().equals(firstName)); } /** @@ -214,11 +222,11 @@ public class Management implements IManagement { @Override public IMember[] toArray() { IMember[] array = new IMember[this.size]; - if (!this.head.isPresent()) { + if (this.size() == 0) { return array; } - LinkedListNode<IMember> currentNode = this.head.get(); + LinkedListNode<IMember> currentNode = this.head; for (int index = 0; index < this.size; index++) { if (reversed) { @@ -243,4 +251,15 @@ public class Management implements IManagement { this.numberOfOperations = 0; return numberOfOperations; } + + /** + * Reset the timer and get the time difference in ns. + * + * @return Nanoseconds since the last call. + */ + public long checkpoint() { + long nanoseconds = this.lastCheckpointNs; + this.lastCheckpointNs = System.nanoTime(); + return this.lastCheckpointNs - nanoseconds; + } } |
