1
2
3
4
5
6
7 package com.varmateo.yawg.core;
8
9 import java.nio.file.Path;
10
11 import io.vavr.Tuple;
12 import io.vavr.collection.HashMap;
13 import io.vavr.collection.Map;
14 import io.vavr.collection.Seq;
15 import io.vavr.control.Option;
16
17 import com.varmateo.yawg.util.GlobMatcher;
18
19
20
21
22
23 final class BakerMatcher {
24
25
26 private final Seq<Entry> _bakerTypes;
27
28
29
30
31
32 private BakerMatcher(final Builder builder) {
33
34 _bakerTypes = builder._bakerTypes
35 .map(x -> new Entry(x._1, x._2));
36 }
37
38
39
40
41
42
43
44 public static Builder builder() {
45
46 return new Builder();
47 }
48
49
50
51
52
53
54
55
56
57
58 public static Builder builder(final BakerMatcher data) {
59
60 return new Builder(data);
61 }
62
63
64
65
66
67 public Option<String> bakerTypeFor(final Path path) {
68
69 return _bakerTypes
70 .filter(entry -> entry.matcher().test(path))
71 .map(Entry::mapType)
72 .headOption();
73 }
74
75
76
77
78
79
80
81 @Override
82 public String toString() {
83
84 return _bakerTypes
85 .sorted((o1, o2) -> o1.mapType().compareTo(o2.mapType()))
86 .map(e -> e.mapType() + ":" + e.matcher().toString())
87 .mkString(";");
88 }
89
90
91
92
93
94 private static final class Entry {
95
96
97 private final String _mapType;
98 private final GlobMatcher _matcher;
99
100
101
102
103
104 Entry(
105 final String mapType,
106 final GlobMatcher matcher) {
107
108 _mapType = mapType;
109 _matcher = matcher;
110 }
111
112
113
114
115
116 public String mapType() {
117
118 return _mapType;
119 }
120
121
122
123
124
125 public GlobMatcher matcher() {
126
127 return _matcher;
128 }
129
130
131 }
132
133
134
135
136
137 public static final class Builder {
138
139
140 private Map<String, GlobMatcher> _bakerTypes;
141
142
143
144
145
146 Builder() {
147
148 _bakerTypes = HashMap.empty();
149 }
150
151
152
153
154
155 Builder(final BakerMatcher data) {
156
157 _bakerTypes = HashMap.ofEntries(
158 data._bakerTypes.map(e -> Tuple.of(e.mapType(), e.matcher())));
159 }
160
161
162
163
164
165 public Builder addBakerType(
166 final String bakerType,
167 final GlobMatcher matcher) {
168
169 _bakerTypes = _bakerTypes.merge(
170 HashMap.of(bakerType, matcher),
171 Builder::extendGlobMatcher);
172
173 return this;
174 }
175
176
177 private static GlobMatcher extendGlobMatcher(
178 final GlobMatcher oldMatcher,
179 final GlobMatcher newMatcher) {
180
181 return GlobMatcher.builder(oldMatcher)
182 .addGlobMatcher(newMatcher)
183 .build();
184 }
185
186
187
188
189
190 public Builder addBakerType(
191 final String bakerType,
192 final String... fileNames) {
193
194 final GlobMatcher matcher = GlobMatcher.create(fileNames);
195
196 addBakerType(bakerType, matcher);
197
198 return this;
199 }
200
201
202
203
204
205 public Builder addBakerTypes(final BakerMatcher that) {
206
207 that._bakerTypes.forEach(e -> addBakerType(e.mapType(), e.matcher()));
208
209 return this;
210 }
211
212
213
214
215
216 public BakerMatcher build() {
217
218 return new BakerMatcher(this);
219 }
220
221
222 }
223
224
225 }