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 public class NoticeCollator extends Visitor {
32
33 private final Map<String, Collection<Resource>> resourcesByNoticeId = new HashMap<String, Collection<Resource>>();
34
35
36
37
38 public Set<String> getNoticeIds() {
39 return this.resourcesByNoticeId.keySet();
40 }
41
42
43
44
45 @Override
46 public void visit(final Resource resource) {
47 final String noticeId = resource.getNoticeId();
48 if (noticeId != null) {
49 if (!this.resourcesByNoticeId.containsKey(noticeId)) {
50 this.resourcesByNoticeId.put(noticeId, new TreeSet<Resource>());
51 }
52 this.resourcesByNoticeId.get(noticeId).add(resource);
53 }
54 }
55
56 public Map<String, Collection<Resource>> resourceNotices(
57 final Map<String, String> notices) {
58 final Map<String, Collection<Resource>> results = new HashMap<String, Collection<Resource>>();
59 for (final Map.Entry<String, Collection<Resource>> entry : this.resourcesByNoticeId
60 .entrySet()) {
61 if (notices.containsKey(entry.getKey())) {
62 results.put(notices.get(entry.getKey()), new TreeSet<Resource>(
63 entry.getValue()));
64 } else {
65 throw new IllegalArgumentException("Notice missing for id "
66 + entry.getKey());
67 }
68 }
69 return results;
70 }
71
72
73
74
75
76 public Set<String> notices(final Map<String, String> notices) {
77 final Set<String> results = new HashSet<String>();
78 for (final String id : getNoticeIds()) {
79 if (notices.containsKey(id)) {
80 results.add(notices.get(id));
81 } else {
82 throw new IllegalArgumentException("Notice missing for id "
83 + id);
84 }
85 }
86 return results;
87 }
88 }