1 /*
2  * Copyright (c) 2002-2019, the original author or authors.
3  *
4  * This software is distributable under the BSD license. See the terms of the
5  * BSD license in the documentation provided with this software.
6  *
7  * https://opensource.org/licenses/BSD-3-Clause
8  */
9 package jdk.internal.org.jline.reader.impl.completer;
10 
11 import java.util.ArrayList;
12 import java.util.Arrays;
13 import java.util.Collection;
14 import java.util.List;
15 import java.util.function.Supplier;
16 
17 import jdk.internal.org.jline.reader.Candidate;
18 import jdk.internal.org.jline.reader.Completer;
19 import jdk.internal.org.jline.reader.LineReader;
20 import jdk.internal.org.jline.reader.ParsedLine;
21 import jdk.internal.org.jline.utils.AttributedString;
22 
23 /**
24  * Completer for a set of strings.
25  *
26  * @author <a href="mailto:jason@planet57.com">Jason Dillon</a>
27  * @since 2.3
28  */
29 public class StringsCompleter implements Completer
30 {
31     protected Collection<Candidate> candidates = new ArrayList<>();
32     protected Supplier<Collection<String>> stringsSupplier;
33 
StringsCompleter()34     public StringsCompleter() {
35     }
36 
StringsCompleter(Supplier<Collection<String>> stringsSupplier)37     public StringsCompleter(Supplier<Collection<String>> stringsSupplier) {
38         assert stringsSupplier != null;
39         candidates = null;
40         this.stringsSupplier = stringsSupplier;
41     }
42 
StringsCompleter(String... strings)43     public StringsCompleter(String... strings) {
44         this(Arrays.asList(strings));
45     }
46 
StringsCompleter(Iterable<String> strings)47     public StringsCompleter(Iterable<String> strings) {
48         assert strings != null;
49         for (String string : strings) {
50             candidates.add(new Candidate(AttributedString.stripAnsi(string), string, null, null, null, null, true));
51         }
52     }
53 
StringsCompleter(Candidate .... candidates)54     public StringsCompleter(Candidate ... candidates) {
55         this(Arrays.asList(candidates));
56     }
57 
StringsCompleter(Collection<Candidate> candidates)58     public StringsCompleter(Collection<Candidate> candidates) {
59         assert candidates != null;
60         this.candidates.addAll(candidates);
61     }
62 
complete(LineReader reader, final ParsedLine commandLine, final List<Candidate> candidates)63     public void complete(LineReader reader, final ParsedLine commandLine, final List<Candidate> candidates) {
64         assert commandLine != null;
65         assert candidates != null;
66         if (this.candidates != null) {
67             candidates.addAll(this.candidates);
68         } else {
69             for (String string : stringsSupplier.get()) {
70                 candidates.add(new Candidate(AttributedString.stripAnsi(string), string, null, null, null, null, true));
71             }
72         }
73     }
74 
75 }
76