-
Notifications
You must be signed in to change notification settings - Fork 0
/
GroupQueryTest.java
94 lines (82 loc) · 3.28 KB
/
GroupQueryTest.java
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
package info.kgeorgiy.java.advanced.student;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runners.MethodSorters;
import java.util.*;
/**
* Tests for hard version
* of <a href="https://www.kgeorgiy.info/courses/java-advanced/homeworks.html#homework-student">Student</a> homework
* for <a href="https://www.kgeorgiy.info/courses/java-advanced/">Java Advanced</a> course.
*
* @author Georgiy Korneev (kgeorgiy@kgeorgiy.info)
*/
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class GroupQueryTest extends StudentQueryTest implements GroupQuery {
private final GroupQuery db = createCUT();
@Test
public void test21_testGetGroupsByName() {
test(this::getGroupsByName, db::getGroupsByName);
}
@Test
public void test22_testGetGroupsById() {
test(this::getGroupsById, db::getGroupsById);
}
@Test
public void test23_testGetLargestGroup() {
test(this::getLargestGroup, db::getLargestGroup);
}
@Test
public void test24_testGetLargestGroupByFirstName() {
test(this::getLargestGroupFirstName, db::getLargestGroupFirstName);
}
// Reference implementation follows
// This implementation is intentionally poorly-written and contains a lot of copy-and-paste
@Override
public List<Group> getGroupsByName(final Collection<Student> students) {
final SortedMap<GroupName, Group> groups = new TreeMap<>();
for (final Student student : students) {
groups.computeIfAbsent(student.getGroup(), group -> new Group(group, findStudentsByGroup(students, group)));
}
return List.copyOf(groups.values());
}
@Override
public List<Group> getGroupsById(final Collection<Student> students) {
final SortedMap<GroupName, Group> groups = new TreeMap<>();
for (final Student student : students) {
groups.computeIfAbsent(student.getGroup(), group -> {
final ArrayList<Student> result = new ArrayList<>(students);
result.removeIf(s -> !s.getGroup().equals(group));
Collections.sort(result);
return new Group(group, result);
});
}
return List.copyOf(groups.values());
}
@Override
public GroupName getLargestGroup(final Collection<Student> students) {
int maxSize = -1;
GroupName name = null;
for (final Group group : getGroupsByName(students)) {
final int size = group.getStudents().size();
if (maxSize < size || maxSize == size && name.compareTo(group.getName()) < 0) {
maxSize = size;
name = group.getName();
}
}
return name;
}
@Override
public GroupName getLargestGroupFirstName(final Collection<Student> students) {
int maxSize = -1;
GroupName name = null;
for (final Group group : getGroupsByName(students)) {
final Set<String> firstNames = new HashSet<>();
group.getStudents().forEach(student -> firstNames.add(student.getFirstName()));
if (maxSize < firstNames.size() || maxSize == firstNames.size() && name.compareTo(group.getName()) > 0) {
maxSize = firstNames.size();
name = group.getName();
}
}
return name;
}
}