1
2
3
4
5
6
7
8
9 package jline.console.completer;
10
11 import java.util.ArrayList;
12 import java.util.Arrays;
13 import java.util.Collection;
14 import java.util.LinkedList;
15 import java.util.List;
16
17 import static jline.internal.Preconditions.checkNotNull;
18
19
20
21
22
23
24
25 public class AggregateCompleter
26 implements Completer
27 {
28 private final List<Completer> completers = new ArrayList<Completer>();
29
30 public AggregateCompleter() {
31
32 }
33
34
35
36
37
38
39
40 public AggregateCompleter(final Collection<Completer> completers) {
41 checkNotNull(completers);
42 this.completers.addAll(completers);
43 }
44
45
46
47
48
49
50
51 public AggregateCompleter(final Completer... completers) {
52 this(Arrays.asList(completers));
53 }
54
55
56
57
58
59
60 public Collection<Completer> getCompleters() {
61 return completers;
62 }
63
64
65
66
67
68
69
70 public int complete(final String buffer, final int cursor, final List<CharSequence> candidates) {
71
72 checkNotNull(candidates);
73
74 List<Completion> completions = new ArrayList<Completion>(completers.size());
75
76
77 int max = -1;
78 for (Completer completer : completers) {
79 Completion completion = new Completion(candidates);
80 completion.complete(completer, buffer, cursor);
81
82
83 max = Math.max(max, completion.cursor);
84
85 completions.add(completion);
86 }
87
88
89 for (Completion completion : completions) {
90 if (completion.cursor == max) {
91 candidates.addAll(completion.candidates);
92 }
93 }
94
95 return max;
96 }
97
98
99
100
101 @Override
102 public String toString() {
103 return getClass().getSimpleName() + "{" +
104 "completers=" + completers +
105 '}';
106 }
107
108 private class Completion
109 {
110 public final List<CharSequence> candidates;
111
112 public int cursor;
113
114 public Completion(final List<CharSequence> candidates) {
115 checkNotNull(candidates);
116 this.candidates = new LinkedList<CharSequence>(candidates);
117 }
118
119 public void complete(final Completer completer, final String buffer, final int cursor) {
120 checkNotNull(completer);
121 this.cursor = completer.complete(buffer, cursor, candidates);
122 }
123 }
124 }