1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51 package org.eclipse.jgit.lib;
52
53 import static org.eclipse.jgit.util.StringUtils.compareIgnoreCase;
54 import static org.eclipse.jgit.util.StringUtils.compareWithCase;
55 import static org.eclipse.jgit.util.StringUtils.toLowerCase;
56
57 import java.util.AbstractSet;
58 import java.util.ArrayList;
59 import java.util.Collections;
60 import java.util.Comparator;
61 import java.util.HashMap;
62 import java.util.Iterator;
63 import java.util.LinkedHashMap;
64 import java.util.LinkedHashSet;
65 import java.util.List;
66 import java.util.Map;
67 import java.util.Set;
68 import java.util.concurrent.ConcurrentHashMap;
69
70 import org.eclipse.jgit.util.StringUtils;
71
72 class ConfigSnapshot {
73 final List<ConfigLine> entryList;
74 final Map<Object, Object> cache;
75 final ConfigSnapshot baseState;
76 volatile List<ConfigLine> sorted;
77 volatile SectionNames names;
78
79 ConfigSnapshot(List<ConfigLine> entries, ConfigSnapshot base) {
80 entryList = entries;
81 cache = new ConcurrentHashMap<Object, Object>(16, 0.75f, 1);
82 baseState = base;
83 }
84
85 Set<String> getSections() {
86 return names().sections;
87 }
88
89 Set<String> getSubsections(String section) {
90 Map<String, Set<String>> m = names().subsections;
91 Set<String> r = m.get(section);
92 if (r == null)
93 r = m.get(toLowerCase(section));
94 if (r == null)
95 return Collections.emptySet();
96 return Collections.unmodifiableSet(r);
97 }
98
99 Set<String> getNames(String section, String subsection) {
100 return getNames(section, subsection, false);
101 }
102
103 Set<String> getNames(String section, String subsection, boolean recursive) {
104 Map<String, String> m = getNamesInternal(section, subsection, recursive);
105 return new CaseFoldingSet(m);
106 }
107
108 private Map<String, String> getNamesInternal(String section,
109 String subsection, boolean recursive) {
110 List<ConfigLine> s = sorted();
111 int idx = find(s, section, subsection, "");
112 if (idx < 0)
113 idx = -(idx + 1);
114
115 Map<String, String> m = new LinkedHashMap<String, String>();
116 while (idx < s.size()) {
117 ConfigLine e = s.get(idx++);
118 if (!e.match(section, subsection))
119 break;
120 if (e.name == null)
121 continue;
122 String l = toLowerCase(e.name);
123 if (!m.containsKey(l))
124 m.put(l, e.name);
125 }
126 if (recursive && baseState != null)
127 m.putAll(baseState.getNamesInternal(section, subsection, recursive));
128 return m;
129 }
130
131 String[] get(String section, String subsection, String name) {
132 List<ConfigLine> s = sorted();
133 int idx = find(s, section, subsection, name);
134 if (idx < 0)
135 return null;
136 int end = end(s, idx, section, subsection, name);
137 String[] r = new String[end - idx];
138 for (int i = 0; idx < end;)
139 r[i++] = s.get(idx++).value;
140 return r;
141 }
142
143 private int find(List<ConfigLine> s, String s1, String s2, String name) {
144 int low = 0;
145 int high = s.size();
146 while (low < high) {
147 int mid = (low + high) >>> 1;
148 ConfigLine e = s.get(mid);
149 int cmp = compare2(
150 s1, s2, name,
151 e.section, e.subsection, e.name);
152 if (cmp < 0)
153 high = mid;
154 else if (cmp == 0)
155 return first(s, mid, s1, s2, name);
156 else
157 low = mid + 1;
158 }
159 return -(low + 1);
160 }
161
162 private int first(List<ConfigLine> s, int i, String s1, String s2, String n) {
163 while (0 < i) {
164 if (s.get(i - 1).match(s1, s2, n))
165 i--;
166 else
167 return i;
168 }
169 return i;
170 }
171
172 private int end(List<ConfigLine> s, int i, String s1, String s2, String n) {
173 while (i < s.size()) {
174 if (s.get(i).match(s1, s2, n))
175 i++;
176 else
177 return i;
178 }
179 return i;
180 }
181
182 private List<ConfigLine> sorted() {
183 List<ConfigLine> r = sorted;
184 if (r == null)
185 sorted = r = sort(entryList);
186 return r;
187 }
188
189 private static List<ConfigLine> sort(List<ConfigLine> in) {
190 List<ConfigLine> sorted = new ArrayList<ConfigLine>(in.size());
191 for (ConfigLine line : in) {
192 if (line.section != null && line.name != null)
193 sorted.add(line);
194 }
195 Collections.sort(sorted, new LineComparator());
196 return sorted;
197 }
198
199 private static int compare2(
200 String aSection, String aSubsection, String aName,
201 String bSection, String bSubsection, String bName) {
202 int c = compareIgnoreCase(aSection, bSection);
203 if (c != 0)
204 return c;
205
206 if (aSubsection == null && bSubsection != null)
207 return -1;
208 if (aSubsection != null && bSubsection == null)
209 return 1;
210 if (aSubsection != null) {
211 c = compareWithCase(aSubsection, bSubsection);
212 if (c != 0)
213 return c;
214 }
215
216 return compareIgnoreCase(aName, bName);
217 }
218
219 private static class LineComparator implements Comparator<ConfigLine> {
220 public int compare(ConfigLine a, ConfigLine b) {
221 return compare2(
222 a.section, a.subsection, a.name,
223 b.section, b.subsection, b.name);
224 }
225 }
226
227 private SectionNames names() {
228 SectionNames n = names;
229 if (n == null)
230 names = n = new SectionNames(this);
231 return n;
232 }
233
234 private static class SectionNames {
235 final CaseFoldingSet sections;
236 final Map<String, Set<String>> subsections;
237
238 SectionNames(ConfigSnapshot cfg) {
239 Map<String, String> sec = new LinkedHashMap<String, String>();
240 Map<String, Set<String>> sub = new HashMap<String, Set<String>>();
241 while (cfg != null) {
242 for (ConfigLine e : cfg.entryList) {
243 if (e.section == null)
244 continue;
245
246 String l1 = toLowerCase(e.section);
247 if (!sec.containsKey(l1))
248 sec.put(l1, e.section);
249
250 if (e.subsection == null)
251 continue;
252
253 Set<String> m = sub.get(l1);
254 if (m == null) {
255 m = new LinkedHashSet<String>();
256 sub.put(l1, m);
257 }
258 m.add(e.subsection);
259 }
260 cfg = cfg.baseState;
261 }
262
263 sections = new CaseFoldingSet(sec);
264 subsections = sub;
265 }
266 }
267
268 private static class CaseFoldingSet extends AbstractSet<String> {
269 private final Map<String, String> names;
270
271 CaseFoldingSet(Map<String, String> names) {
272 this.names = names;
273 }
274
275 @Override
276 public boolean contains(Object needle) {
277 if (needle instanceof String) {
278 String n = (String) needle;
279 return names.containsKey(n)
280 || names.containsKey(StringUtils.toLowerCase(n));
281 }
282 return false;
283 }
284
285 @Override
286 public Iterator<String> iterator() {
287 final Iterator<String> i = names.values().iterator();
288 return new Iterator<String>() {
289 public boolean hasNext() {
290 return i.hasNext();
291 }
292
293 public String next() {
294 return i.next();
295 }
296
297 public void remove() {
298 throw new UnsupportedOperationException();
299 }
300 };
301 }
302
303 @Override
304 public int size() {
305 return names.size();
306 }
307 }
308 }