1 package au.gov.amsa.ais.router.model;
2
3 import java.util.ArrayList;
4 import java.util.List;
5 import java.util.regex.Pattern;
6
7 import com.github.davidmoten.guavamini.Preconditions;
8
9 import rx.Observable;
10 import rx.schedulers.Schedulers;
11
12 public final class Group implements GroupMember {
13
14 private final String id;
15 private final List<GroupMember> members;
16 private final boolean enabled;
17 private final boolean addTimestamp;
18 private final boolean addArrivalTime;
19 private final List<Region> filterRegions;
20 private final List<MessageType> filterMessageTypes;
21 private final List<Pattern> filterPatterns;
22 private final Observable<String> lines;
23
24 private Group(String id, List<GroupMember> members, boolean enabled, boolean addTimestamp,
25 boolean addArrivalTime, List<Region> filterRegions,
26 List<MessageType> filterMessageTypes, List<Pattern> filterPatterns) {
27 Util.verifyId(id);
28 Preconditions.checkNotNull(members);
29 Preconditions.checkNotNull(filterRegions);
30 this.id = id;
31 this.members = members;
32 this.enabled = enabled;
33 this.addTimestamp = addTimestamp;
34 this.addArrivalTime = addArrivalTime;
35 this.filterRegions = filterRegions;
36 this.filterMessageTypes = filterMessageTypes;
37 this.filterPatterns = filterPatterns;
38 this.lines = createLines();
39 }
40
41 @Override
42 public Observable<String> lines() {
43 return lines;
44 }
45
46 private Observable<String> createLines() {
47 if (enabled) {
48 return Observable.from(members)
49
50 .flatMap(member -> member.lines().subscribeOn(Schedulers.io()))
51
52
53 .filter(line -> filterPatterns.isEmpty() || filterPatterns.stream()
54 .anyMatch(pattern -> pattern.matcher(line).find()))
55
56 .share();
57 } else {
58 return Observable.empty();
59 }
60 }
61
62 public String id() {
63 return id;
64 }
65
66 public List<GroupMember> members() {
67 return members;
68 }
69
70 public boolean enabled() {
71 return enabled;
72 }
73
74 public boolean addTimestamp() {
75 return addTimestamp;
76 }
77
78 public boolean addArrivalTime() {
79 return addArrivalTime;
80 }
81
82 public List<Region> filterRegions() {
83 return filterRegions;
84 }
85
86 public List<MessageType> filterMessageTypes() {
87 return filterMessageTypes;
88 }
89
90 public List<Pattern> filterPatterns() {
91 return filterPatterns;
92 }
93
94 public static Builder builder() {
95 return new Builder();
96 }
97
98 public static class Builder {
99
100 private String id;
101 private final List<GroupMember> members = new ArrayList<GroupMember>();
102 private boolean enabled = true;
103 private boolean addTimestamp;
104 private boolean addArrivalTime;
105 private final List<Region> filterRegions = new ArrayList<>();
106 private final List<MessageType> filterMessageTypes = new ArrayList<MessageType>();
107 private final List<Pattern> filterPatterns = new ArrayList<Pattern>();
108
109 private Builder() {
110 }
111
112 public Builder id(String id) {
113 this.id = id;
114 return this;
115 }
116
117 public Builder members(List<GroupMember> members) {
118 this.members.addAll(members);
119 return this;
120 }
121
122 public Builder member(GroupMember member) {
123 this.members.add(member);
124 return this;
125 }
126
127 public Builder enabled(boolean enabled) {
128 this.enabled = enabled;
129 return this;
130 }
131
132 public Builder addTimestamp(boolean addTimestamp) {
133 this.addTimestamp = addTimestamp;
134 return this;
135 }
136
137 public Builder addArrivalTime(boolean addArrivalTime) {
138 this.addArrivalTime = addArrivalTime;
139 return this;
140 }
141
142 public Builder filterRegions(List<Region> filterRegions) {
143 this.filterRegions.addAll(filterRegions);
144 return this;
145 }
146
147 public Builder filterRegion(Region filterRegion) {
148 this.filterRegions.add(filterRegion);
149 return this;
150 }
151
152 public Builder filterMessageTypes(List<MessageType> filterMessageTypes) {
153 this.filterMessageTypes.addAll(filterMessageTypes);
154 return this;
155 }
156
157 public Builder filterMessageTypes(MessageType filterMessageType) {
158 this.filterMessageTypes.add(filterMessageType);
159 return this;
160 }
161
162 public Builder filterPatterns(List<Pattern> filterPatterns) {
163 this.filterPatterns.addAll(filterPatterns);
164 return this;
165 }
166
167 public Builder filterPattern(Pattern filterPattern) {
168 this.filterPatterns.add(filterPattern);
169 return this;
170 }
171
172 public Builder filterPattern(String filterPattern) {
173 return filterPattern(Pattern.compile(filterPattern));
174 }
175
176 public Group build() {
177 return new Group(id, members, enabled, addTimestamp, addArrivalTime, filterRegions,
178 filterMessageTypes, filterPatterns);
179 }
180 }
181
182 @Override
183 public String toString() {
184 StringBuilder b = new StringBuilder();
185 b.append("Group [id=");
186 b.append(id);
187 b.append(", members=");
188 b.append(members);
189 b.append(", enabled=");
190 b.append(enabled);
191 b.append(", addTimestamp=");
192 b.append(addTimestamp);
193 b.append(", addArrivalTime=");
194 b.append(addArrivalTime);
195 b.append(", filterRegions=");
196 b.append(filterRegions);
197 b.append(", filterMessageTypes=");
198 b.append(filterMessageTypes);
199 b.append(", filterPatterns=");
200 b.append(filterPatterns);
201 b.append("]");
202 return b.toString();
203 }
204
205 public static void main(String[] args) {
206 System.out.println(Pattern.compile("Kembla")
207 .matcher(
208 "\\s:ISEEK Kembla,c:1461727307*65\\!BSVDM,1,1,,A,177hUM000<:lKg7dNBPJA:GN0<1R,0*50")
209 .find());
210 }
211
212 }