1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.commons.net.nntp;
20
21
22
23
24
25
26
27
28
29
30 import java.util.HashMap;
31 import java.util.Iterator;
32
33 public class Threader {
34 private ThreadContainer root;
35 private HashMap<String,ThreadContainer> idTable;
36 private int bogusIdCount = 0;
37
38
39
40
41
42
43
44 public Threadable thread(Threadable[] messages) {
45 if (messages == null)
46 return null;
47
48 idTable = new HashMap<String,ThreadContainer>();
49
50
51 for (int i = 0; i < messages.length; ++i) {
52 if (!messages[i].isDummy())
53 buildContainer(messages[i]);
54 }
55
56 root = findRootSet();
57 idTable.clear();
58 idTable = null;
59
60 pruneEmptyContainers(root);
61
62 root.reverseChildren();
63 gatherSubjects();
64
65 if (root.next != null)
66 throw new RuntimeException("root node has a next:" + root);
67
68 for (ThreadContainer r = root.child; r != null; r = r.next) {
69 if (r.threadable == null)
70 r.threadable = r.child.threadable.makeDummy();
71 }
72
73 Threadable result = (root.child == null ? null : root.child.threadable);
74 root.flush();
75 root = null;
76
77 return result;
78 }
79
80
81
82
83
84 private void buildContainer(Threadable threadable) {
85 String id = threadable.messageThreadId();
86 ThreadContainer container = idTable.get(id);
87
88
89
90 if (container != null) {
91 if (container.threadable != null) {
92 id = "<Bogus-id:" + (bogusIdCount++) + ">";
93 container = null;
94 } else {
95
96
97 container.threadable = threadable;
98 }
99 }
100
101
102 if (container == null) {
103 container = new ThreadContainer();
104 container.threadable = threadable;
105 idTable.put(id, container);
106 }
107
108
109
110 ThreadContainer parentRef = null;
111 {
112 String[] references = threadable.messageThreadReferences();
113 for (int i = 0; i < references.length; ++i) {
114 String refString = references[i];
115 ThreadContainer ref = idTable.get(refString);
116
117
118 if (ref == null) {
119 ref = new ThreadContainer();
120 idTable.put(refString, ref);
121 }
122
123
124
125
126 if ((parentRef != null)
127 && (ref.parent == null)
128 && (parentRef != ref)
129 && !(parentRef.findChild(ref))) {
130
131 ref.parent = parentRef;
132 ref.next = parentRef.child;
133 parentRef.child = ref;
134 }
135 parentRef = ref;
136 }
137 }
138
139
140
141 if (parentRef != null
142 && (parentRef == container || container.findChild(parentRef)))
143 parentRef = null;
144
145
146
147
148 if (container.parent != null) {
149 ThreadContainer rest, prev;
150
151 for (prev = null, rest = container.parent.child;
152 rest != null;
153 prev = rest, rest = rest.next) {
154 if (rest == container)
155 break;
156 }
157
158 if (rest == null) {
159 throw new RuntimeException(
160 "Didnt find "
161 + container
162 + " in parent"
163 + container.parent);
164 }
165
166
167 if (prev == null)
168 container.parent.child = container.next;
169 else
170 prev.next = container.next;
171
172 container.next = null;
173 container.parent = null;
174 }
175
176
177 if (parentRef != null) {
178 container.parent = parentRef;
179 container.next = parentRef.child;
180 parentRef.child = container;
181 }
182 }
183
184
185
186
187
188 private ThreadContainer findRootSet() {
189 ThreadContainer root = new ThreadContainer();
190 Iterator<String> iter = idTable.keySet().iterator();
191
192 while (iter.hasNext()) {
193 Object key = iter.next();
194 ThreadContainer c = idTable.get(key);
195 if (c.parent == null) {
196 if (c.next != null)
197 throw new RuntimeException(
198 "c.next is " + c.next.toString());
199 c.next = root.child;
200 root.child = c;
201 }
202 }
203 return root;
204 }
205
206
207
208
209
210 private void pruneEmptyContainers(ThreadContainer parent) {
211 ThreadContainer container, prev, next;
212 for (prev = null, container = parent.child, next = container.next;
213 container != null;
214 prev = container,
215 container = next,
216 next = (container == null ? null : container.next)) {
217
218
219 if (container.threadable == null && container.child == null) {
220 if (prev == null)
221 parent.child = container.next;
222 else
223 prev.next = container.next;
224
225
226 container = prev;
227 }
228
229
230 else if (
231 container.threadable == null
232 && container.child != null
233 && (container.parent != null
234 || container.child.next == null)) {
235
236 ThreadContainer tail;
237 ThreadContainer kids = container.child;
238
239
240 if (prev == null)
241 parent.child = kids;
242 else
243 prev.next = kids;
244
245
246
247 for (tail = kids; tail.next != null; tail = tail.next)
248 tail.parent = container.parent;
249
250 tail.parent = container.parent;
251 tail.next = container.next;
252
253
254
255 next = kids;
256
257
258 container = prev;
259 } else if (container.child != null) {
260
261
262 pruneEmptyContainers(container);
263 }
264 }
265 }
266
267
268
269
270 private void gatherSubjects() {
271
272 int count = 0;
273
274 for (ThreadContainer c = root.child; c != null; c = c.next)
275 count++;
276
277
278 HashMap<String, ThreadContainer> subjectTable = new HashMap<String, ThreadContainer>((int) (count * 1.2), (float) 0.9);
279 count = 0;
280
281 for (ThreadContainer c = root.child; c != null; c = c.next) {
282 Threadable threadable = c.threadable;
283
284
285
286
287 if (threadable == null)
288 threadable = c.child.threadable;
289
290 String subj = threadable.simplifiedSubject();
291
292 if (subj == null || subj == "")
293 continue;
294
295 ThreadContainer old = subjectTable.get(subj);
296
297
298
299
300
301
302
303
304 if (old == null
305 || (c.threadable == null && old.threadable != null)
306 || (old.threadable != null
307 && old.threadable.subjectIsReply()
308 && c.threadable != null
309 && !c.threadable.subjectIsReply())) {
310 subjectTable.put(subj, c);
311 count++;
312 }
313 }
314
315
316 if (count == 0)
317 return;
318
319
320
321 ThreadContainer prev, c, rest;
322 for (prev = null, c = root.child, rest = c.next;
323 c != null;
324 prev = c, c = rest, rest = (rest == null ? null : rest.next)) {
325 Threadable threadable = c.threadable;
326
327
328 if (threadable == null)
329 threadable = c.child.threadable;
330
331 String subj = threadable.simplifiedSubject();
332
333
334 if (subj == null || subj == "")
335 continue;
336
337 ThreadContainer old = subjectTable.get(subj);
338
339 if (old == c)
340 continue;
341
342
343
344 if (prev == null)
345 root.child = c.next;
346 else
347 prev.next = c.next;
348 c.next = null;
349
350 if (old.threadable == null && c.threadable == null) {
351
352 ThreadContainer tail;
353 for (tail = old.child;
354 tail != null && tail.next != null;
355 tail = tail.next);
356
357 tail.next = c.child;
358
359 for (tail = c.child; tail != null; tail = tail.next)
360 tail.parent = old;
361
362 c.child = null;
363 } else if (
364 old.threadable == null
365 || (c.threadable != null
366 && c.threadable.subjectIsReply()
367 && !old.threadable.subjectIsReply())) {
368
369 c.parent = old;
370 c.next = old.child;
371 old.child = c;
372 } else {
373
374
375 ThreadContainer newc = new ThreadContainer();
376 newc.threadable = old.threadable;
377 newc.child = old.child;
378
379 for (ThreadContainer tail = newc.child;
380 tail != null;
381 tail = tail.next)
382 tail.parent = newc;
383
384 old.threadable = null;
385 old.child = null;
386
387 c.parent = old;
388 newc.parent = old;
389
390
391 old.child = c;
392 c.next = newc;
393 }
394
395 c = prev;
396 }
397
398 subjectTable.clear();
399 subjectTable = null;
400
401 }
402 }
403
404
405
406
407
408
409
410
411 class ThreadContainer {
412 Threadable threadable;
413 ThreadContainer parent;
414 ThreadContainer prev;
415 ThreadContainer next;
416 ThreadContainer child;
417
418
419
420
421
422
423 boolean findChild(ThreadContainer target) {
424 if (child == null)
425 return false;
426
427 else if (child == target)
428 return true;
429 else
430 return child.findChild(target);
431 }
432
433
434
435 void flush() {
436 if (parent != null && threadable == null)
437 throw new RuntimeException("no threadable in " + this.toString());
438
439 parent = null;
440
441 if (threadable != null)
442 threadable.setChild(child == null ? null : child.threadable);
443
444 if (child != null) {
445 child.flush();
446 child = null;
447 }
448
449 if (threadable != null)
450 threadable.setNext(next == null ? null : next.threadable);
451
452 if (next != null) {
453 next.flush();
454 next = null;
455 }
456
457 threadable = null;
458 }
459
460
461
462
463
464 void reverseChildren() {
465 if (child != null) {
466 ThreadContainer kid, prev, rest;
467 for (prev = null, kid = child, rest = kid.next;
468 kid != null;
469 prev = kid,
470 kid = rest,
471 rest = (rest == null ? null : rest.next))
472 kid.next = prev;
473
474 child = prev;
475
476
477 for (kid = child; kid != null; kid = kid.next)
478 kid.reverseChildren();
479 }
480 }
481 }