2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.apex.auth.clieditor;
23 import java.util.ArrayList;
24 import java.util.List;
26 import java.util.TreeMap;
27 import java.util.TreeSet;
29 import org.onap.policy.apex.model.utilities.Assertions;
32 * The Class KeywordNode holds the structure of a command keyword for the Apex CLI editor. The
33 * keyword itself and all its children are held as a recursive tree. This class is used to manage
34 * interactive sub-modes in the Apex CLI editor.
36 public class KeywordNode implements Comparable<KeywordNode> {
37 private final String keyword;
38 private final TreeMap<String, KeywordNode> children;
39 private CLICommand command;
42 * This Constructor creates a keyword node with the given keyword and no command.
44 * @param keyword the keyword of the node
46 public KeywordNode(final String keyword) {
51 * This Constructor creates a keyword node with the given keyword and command.
53 * @param keyword the keyword of the keyword node
54 * @param command the command associated with this keyword
56 public KeywordNode(final String keyword, final CLICommand command) {
57 Assertions.argumentNotNull(keyword, "commands may not be null");
59 this.keyword = keyword;
60 children = new TreeMap<>();
61 this.command = command;
65 * Process a list of keywords on this keyword node, recursing the keyword node tree, creating
66 * new branches for the keyword list if required. When the end of a branch has been reached,
67 * store the command in that keyword node..
69 * @param keywordList the list of keywords to process on this keyword node
70 * @param incomingCommand the command
72 public void processKeywords(final List<String> keywordList, final CLICommand incomingCommand) {
73 if (keywordList.size() <= 0) {
74 this.command = incomingCommand;
78 if (!children.containsKey(keywordList.get(0))) {
79 children.put(keywordList.get(0), new KeywordNode(keywordList.get(0)));
82 final ArrayList<String> nextLevelKeywordList = new ArrayList<>(keywordList);
83 nextLevelKeywordList.remove(0);
84 children.get(keywordList.get(0)).processKeywords(nextLevelKeywordList, incomingCommand);
88 * Adds the system commands to the keyword node.
90 * @param systemCommandNodes the system command nodes to add to the keyword node
92 public void addSystemCommandNodes(final Set<KeywordNode> systemCommandNodes) {
93 if (children.isEmpty()) {
97 for (final KeywordNode node : children.values()) {
98 node.addSystemCommandNodes(systemCommandNodes);
101 for (final KeywordNode systemCommandNode : systemCommandNodes) {
102 children.put(systemCommandNode.getKeyword(), systemCommandNode);
108 * Gets the keyword of this keyword node.
110 * @return the keyword of this keyword node
112 public String getKeyword() {
117 * Gets the children of this keyword node.
119 * @return the children of this keyword node
121 public TreeMap<String, KeywordNode> getChildren() {
126 * Gets the command of this keyword node.
128 * @return the command of this keyword node
130 public CLICommand getCommand() {
137 * @see java.lang.Object#toString()
140 public String toString() {
141 return "CommandKeywordNode [keyword=" + keyword + ", children=" + children + ", command=" + command + "]";
147 * @return the commands
149 public Set<CLICommand> getCommands() {
150 final Set<CLICommand> commandSet = new TreeSet<>();
152 for (final KeywordNode child : children.values()) {
153 if (child.getCommand() != null) {
154 commandSet.add(child.getCommand());
156 commandSet.addAll(child.getCommands());
165 * @see java.lang.Comparable#compareTo(java.lang.Object)
168 public int compareTo(final KeywordNode otherKeywordNode) {
169 Assertions.argumentNotNull(otherKeywordNode, "comparison object may not be null");
171 if (this == otherKeywordNode) {
174 if (getClass() != otherKeywordNode.getClass()) {
175 return this.hashCode() - otherKeywordNode.hashCode();
178 final KeywordNode other = otherKeywordNode;
180 if (!keyword.equals(other.keyword)) {
181 return keyword.compareTo(other.keyword);
183 if (!children.equals(other.children)) {
184 return (children.hashCode() - other.children.hashCode());
186 return command.compareTo(otherKeywordNode.command);