1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
package org.apache.creadur.whisker.model; |
20 | |
|
21 | |
import java.util.Collection; |
22 | |
import java.util.HashMap; |
23 | |
import java.util.HashSet; |
24 | |
import java.util.Map; |
25 | |
import java.util.Set; |
26 | |
import java.util.TreeSet; |
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | 18 | public class NoticeCollator extends Visitor { |
32 | |
|
33 | 18 | private final Map<String, Collection<Resource>> resourcesByNoticeId = new HashMap<String, Collection<Resource>>(); |
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
public Set<String> getNoticeIds() { |
39 | 0 | return this.resourcesByNoticeId.keySet(); |
40 | |
} |
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
@Override |
46 | |
public void visit(final Resource resource) { |
47 | 6 | final String noticeId = resource.getNoticeId(); |
48 | 6 | if (noticeId != null) { |
49 | 6 | if (!this.resourcesByNoticeId.containsKey(noticeId)) { |
50 | 6 | this.resourcesByNoticeId.put(noticeId, new TreeSet<Resource>()); |
51 | |
} |
52 | 6 | this.resourcesByNoticeId.get(noticeId).add(resource); |
53 | |
} |
54 | 6 | } |
55 | |
|
56 | |
public Map<String, Collection<Resource>> resourceNotices( |
57 | |
final Map<String, String> notices) { |
58 | 18 | final Map<String, Collection<Resource>> results = new HashMap<String, Collection<Resource>>(); |
59 | 18 | for (final Map.Entry<String, Collection<Resource>> entry : this.resourcesByNoticeId |
60 | |
.entrySet()) { |
61 | 6 | if (notices.containsKey(entry.getKey())) { |
62 | 6 | results.put(notices.get(entry.getKey()), new TreeSet<Resource>( |
63 | |
entry.getValue())); |
64 | |
} else { |
65 | 0 | throw new IllegalArgumentException("Notice missing for id " |
66 | |
+ entry.getKey()); |
67 | |
} |
68 | |
} |
69 | 18 | return results; |
70 | |
} |
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
public Set<String> notices(final Map<String, String> notices) { |
77 | 0 | final Set<String> results = new HashSet<String>(); |
78 | 0 | for (final String id : getNoticeIds()) { |
79 | 0 | if (notices.containsKey(id)) { |
80 | 0 | results.add(notices.get(id)); |
81 | |
} else { |
82 | 0 | throw new IllegalArgumentException("Notice missing for id " |
83 | |
+ id); |
84 | |
} |
85 | |
} |
86 | 0 | return results; |
87 | |
} |
88 | |
} |