blob: 2be04c71c064e39fbb87683de828d04a6c324710 (
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
|
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.Optional;
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;
public class Management implements IManagement {
/**
* Tracks the number of operations.
*/
private int numberOfOperations = 0;
/**
* Current head node.
*/
private Optional<LinkedListNode<IMember>> head = Optional.empty();
/**
* Length of the list.
*/
private 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.
*/
private 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.
*/
private Management filter(Predicate<IMember> criteria, boolean breakAfterFirstMatch) {
Management result = new Management();
if (!this.head.isPresent()) {
return result;
}
// searches from head, inserts at head -> result is reversed
result.reverse();
LinkedListNode<IMember> currentNode = this.head.get();
do {
// traverse = 1 operation
this.numberOfOperations++;
IMember currentValue = currentNode.getValue();
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 An optional containing the first element that matches the given criteria.
*/
private Optional<IMember> search(Predicate<IMember> criteria) {
return this.filter(criteria, true).head.map(head -> head.getValue());
}
/**
* 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) {
this.numberOfOperations++;
LinkedListNode<IMember> newHead = new LinkedListNode<IMember>(value);
if (this.head.isPresent()) {
newHead.setNext(this.head.get());
}
this.head = Optional.of(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) {
return this.search(member -> member.getKey() == key).orElse(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) {
return this.search(member -> member.getName().equals(name)
&& member.getFirstName().equals(firstName)).orElse(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) {
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.head.isPresent()) {
return array;
}
LinkedListNode<IMember> currentNode = this.head.get();
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;
}
/**
* Reset the number of operation counter and get the last value.
*
* @return The number of operations recorded since the last call.
*/
@Override
public int numberOfOperations() {
int numberOfOperations = this.numberOfOperations;
this.numberOfOperations = 0;
return numberOfOperations;
}
}
|