blob: 940a0ad8737e857f4da34e245ab0d41fb7a8119e (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
|
package de.ostfalia.algo.ws18.s1;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.function.Predicate;
import de.ostfalia.algo.ws18.base.IManagement;
import de.ostfalia.algo.ws18.base.IMember;
import de.ostfalia.algo.ws18.base.KindOfSport;
import de.ostfalia.algo.ws18.base.Member;
public class Management implements IManagement {
/**
* Track the number of operations.
*/
protected int numberOfOperations;
/**
* Track the execution time.
*/
private long lastCheckpointNs;
/**
* Current head node.
*/
protected LinkedListNode<IMember> head;
/**
* Length of the list.
*/
protected int size = 0;
/**
* Whether the list is reversed, i. e. the head is the tail.
*/
private boolean reversed = false;
/**
* Empty constructor.
*/
public Management() {
}
/**
* Insert from an array of strings.
*
* @param membersCsv Array of CSV rows.
*/
public Management(String[] membersCsv) {
for (String memberCsv : membersCsv) {
this.insert(new Member(memberCsv));
}
}
/**
* Read from the CSV file and insert.
*
* @param filename Path to the CSV file.
*/
public Management(String filename) {
this.importFromFile(filename);
}
/**
* Read from the CSV file and insert.
*
* @param filename Path to the CSV file.
*/
public void importFromFile(String filename) {
File file = new File(filename);
try (BufferedReader bufferedReader = new BufferedReader(new FileReader(file))) {
bufferedReader.lines().forEach(line -> insert(new Member(line)));
} catch (IOException exception) {
throw new IllegalArgumentException(exception);
}
}
/**
* Create a filtered version of this list.
*
* @param criteria Criteria to filter for.
* @param breakAfterFirstMatch If true, return after the first match.
* @return A new list where all elements meet the given criteria.
*/
protected Management filter(Predicate<IMember> criteria, boolean breakAfterFirstMatch) {
Management result = new Management();
if (this.size == 0) {
return result;
}
// searches from head, inserts at head -> result is reversed
result.reverse();
LinkedListNode<IMember> currentNode = this.head;
do {
IMember currentValue = currentNode.getValue();
this.numberOfOperations++;
if (criteria.test(currentValue)) {
result.insert(currentValue);
if (breakAfterFirstMatch) {
break;
}
}
} while ((currentNode = currentNode.getNext()) != null);
return result;
}
/**
* Find the first element that matches the given criteria.
*
* @param criteria Criteria to search for.
* @return The first element that matches the given criteria or null.
*/
protected LinkedListNode<IMember> search(Predicate<IMember> criteria) {
Management filteredList = this.filter(criteria, true);
if (filteredList.size() == 0) {
return null;
} else {
return filteredList.head;
}
}
/**
* Count the number of elements that match the given criteria.
*
* @param criteria Criteria to filter for.
* @return The number of elements that match the given criteria.
*/
private long size(Predicate<IMember> criteria) {
return this.filter(criteria, false).size;
}
/**
* Reverse the order of the list.
*/
public void reverse() {
this.reversed = !this.reversed;
}
/**
* @return The length of this list.
*/
@Override
public int size() {
return this.size;
}
/**
* Add an element at the start of the list.
* Replace the current head with a new node and link this node to the old head.
*
* @return TODO?
*/
@Override
public boolean insert(IMember value) {
LinkedListNode<IMember> newHead = new LinkedListNode<IMember>(value);
this.numberOfOperations++;
if (this.head != null) {
newHead.setNext(this.head);
}
this.head = newHead;
this.size++;
return true; // TODO?
}
/**
* Find the first member with the given key.
*
* @param key The key to search for.
* @return The member or null.
*/
@Override
public IMember search(long key) {
LinkedListNode<IMember> result = this.search(member -> member.getKey() == key);
if (result != null) {
return result.getValue();
} else {
return null;
}
}
/**
* Find the first member with the given name.
*
* @param name The name of the member.
* @param firstName first name of the member.
* @return The member or null.
*/
@Override
public IMember search(String name, String firstName) {
LinkedListNode<IMember> result = this.search(member -> member.getName().equals(name)
&& member.getFirstName().equals(firstName));
if (result != null) {
return result.getValue();
} else {
return null;
}
}
/**
* Count the number of members which have an association to the given kindOfSport.
*
* @param kindOfSport The kindOfSport of the members.
* @return The number of members with the given kindOfSport.
*/
@Override
public int size(KindOfSport kindOfSport) {
this.numberOfOperations = 0;
return (int) this.size(member -> member.getKindOfSport().equals(kindOfSport));
}
/**
* Return the members which have an association to the given kindOfSport.
*
* @param kindOfSport The kindOfSport of the members.
* @return An array of the members with the given kindOfSport.
*/
@Override
public IMember[] discipline(KindOfSport kindOfSport) {
return this.filter(member -> member.getKindOfSport().equals(kindOfSport), false)
.toArray();
}
/**
* Convert the list to an array.
*
* @return The elements of this list as array.
*/
@Override
public IMember[] toArray() {
IMember[] array = new IMember[this.size];
if (this.size() == 0) {
return array;
}
LinkedListNode<IMember> currentNode = this.head;
for (int index = 0; index < this.size; index++) {
if (reversed) {
array[this.size - index - 1] = currentNode.getValue();
} else {
array[index] = currentNode.getValue();
}
currentNode = currentNode.getNext();
}
return array;
}
/**
* TODO
*
* @return The number of operations recorded since the last call.
*/
@Override
public int numberOfOperations() {
int numberOfOperations = this.numberOfOperations;
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;
}
}
|