Method: HelpCommandCompleter(CommandRegistry)

1: /*******************************************************************************
2: * Copyright (c) 2008, 2010 VMware Inc.
3: * All rights reserved. This program and the accompanying materials
4: * are made available under the terms of the Eclipse Public License v1.0
5: * which accompanies this distribution, and is available at
6: * http://www.eclipse.org/legal/epl-v10.html
7: *
8: * Contributors:
9: * VMware Inc. - initial contribution
10: *******************************************************************************/
11:
12: package org.eclipse.virgo.shell.internal.completers;
13:
14: import java.util.ArrayList;
15: import java.util.Collections;
16: import java.util.List;
17:
18: import org.eclipse.virgo.shell.CommandCompleter;
19: import org.eclipse.virgo.shell.internal.CommandDescriptor;
20: import org.eclipse.virgo.shell.internal.CommandRegistry;
21:
22:
23: /**
24: * A <code>CommandCompleter<code> for the <code>help</code> command that offers completions based on all the commands in
25: * a {@link CommandRegistry}.
26: * <p />
27: *
28: * <strong>Concurrent Semantics</strong><br />
29: *
30: * Thread-safe.
31: *
32: */
33: final class HelpCommandCompleter implements CommandCompleter {
34:
35: private final CommandRegistry commandRegistry;
36:
37: HelpCommandCompleter(CommandRegistry commandRegistry) {
38: this.commandRegistry = commandRegistry;
39: }
40:
41: /**
42: * {@inheritDoc}
43: */
44: public List<String> getCompletionCandidates(String subCommand, String... arguments) {
45: List<String> candidates = new ArrayList<String>();
46: if (arguments.length == 0) {
47: // only complete first argument (subCommand)
48: List<CommandDescriptor> commandDescriptors = this.commandRegistry.getCommandDescriptors();
49: for (CommandDescriptor commandDescriptor : commandDescriptors) {
50: if (commandDescriptor.getCommandName().startsWith(subCommand)) {
51: candidates.add(commandDescriptor.getCommandName());
52: }
53: }
54: }
55: Collections.sort(candidates);
56: return candidates;
57: }
58: }