1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package io.earcam.instrumental.module.osgi;
20
21 import static io.earcam.instrumental.fluent.Role.AUX;
22 import static io.earcam.instrumental.module.osgi.ClauseParameters.EMPTY_PARAMETERS;
23 import static java.util.stream.Collectors.joining;
24
25 import java.io.IOException;
26 import java.util.Arrays;
27 import java.util.List;
28 import java.util.Objects;
29 import java.util.SortedSet;
30 import java.util.TreeSet;
31
32 import javax.annotation.ParametersAreNonnullByDefault;
33
34 import io.earcam.instrumental.fluent.Fluent;
35 import io.earcam.unexceptional.Exceptional;
36
37
38
39
40
41
42
43 @ParametersAreNonnullByDefault
44 public class Clause {
45
46 private final SortedSet<String> uniqueNames;
47 private final ClauseParameters parameters;
48
49
50
51
52
53
54
55
56
57
58 public Clause(String uniqueName, ClauseParameters parameters)
59 {
60 uniqueNames = new TreeSet<>();
61 uniqueNames.add(uniqueName);
62 this.parameters = parameters;
63 }
64
65
66
67
68
69
70
71
72
73
74 public Clause(SortedSet<String> uniqueNames, ClauseParameters parameters)
75 {
76 this.uniqueNames = uniqueNames;
77 this.parameters = parameters;
78 }
79
80
81
82
83
84
85
86
87
88
89 public static Clause clause(String uniqueName)
90 {
91 return clause(uniqueName, EMPTY_PARAMETERS);
92 }
93
94
95
96
97
98
99
100
101
102
103
104 public static Clause clause(String uniqueName, ClauseParameters parameters)
105 {
106 return new Clause(uniqueName, parameters);
107 }
108
109
110
111
112
113
114
115
116
117
118
119 @Fluent(role = AUX)
120 public static SortedSet<String> sortedSet(String... elements)
121 {
122 return new TreeSet<>(Arrays.asList(elements));
123 }
124
125
126 @Override
127 public String toString()
128 {
129 return Exceptional.apply(this::appendTo, new StringBuilder()).toString();
130 }
131
132
133 @Override
134 public boolean equals(Object other)
135 {
136 return other instanceof Clause && equals((Clause) other);
137 }
138
139
140
141
142
143
144
145
146
147
148 @SuppressWarnings("squid:S2589")
149 public boolean equals(Clause that)
150 {
151 return that != null
152
153 && that.uniqueNames().size() == this.uniqueNames.size()
154 && that.uniqueNames().containsAll(this.uniqueNames)
155 && that.parameters().equals(this.parameters);
156 }
157
158
159 @Override
160 public int hashCode()
161 {
162 return Objects.hash(uniqueNames, parameters);
163 }
164
165
166
167
168
169
170
171
172
173 public SortedSet<String> uniqueNames()
174 {
175 return uniqueNames;
176 }
177
178
179
180
181
182
183
184
185
186 public ClauseParameters parameters()
187 {
188 return parameters;
189 }
190
191
192
193
194
195
196
197
198
199
200
201 public Appendable appendTo(Appendable appendix) throws IOException
202 {
203 appendix.append(uniqueNames().stream().collect(joining(";")));
204 return parameters.appendTo(appendix);
205 }
206
207
208 public static String allToString(List<Clause> clauses)
209 {
210 StringBuilder santa = new StringBuilder();
211 for(int i = 0; i < clauses.size(); i++) {
212 Exceptional.accept(clauses.get(i)::appendTo, santa);
213 if(i < clauses.size() - 1) {
214 santa.append(',');
215 }
216 }
217 return santa.toString();
218 }
219 }