2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2019 Nordix Foundation.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.apex.auth.clieditor;
24 import java.util.ArrayList;
25 import java.util.List;
26 import java.util.NavigableMap;
28 import java.util.TreeMap;
29 import java.util.TreeSet;
30 import org.onap.policy.common.utils.validation.Assertions;
33 * The Class KeywordNode holds the structure of a command keyword for the Apex CLI editor. The
34 * keyword itself and all its children are held as a recursive tree. This class is used to manage
35 * interactive sub-modes in the Apex CLI editor.
37 public class KeywordNode implements Comparable<KeywordNode> {
38 private final String keyword;
39 private final TreeMap<String, KeywordNode> children;
40 private CommandLineCommand command;
43 * This Constructor creates a keyword node with the given keyword and no command.
45 * @param keyword the keyword of the node
47 public KeywordNode(final String keyword) {
52 * This Constructor creates a keyword node with the given keyword and command.
54 * @param keyword the keyword of the keyword node
55 * @param command the command associated with this keyword
57 public KeywordNode(final String keyword, final CommandLineCommand command) {
58 Assertions.argumentNotNull(keyword, "commands may not be null");
60 this.keyword = keyword;
61 children = new TreeMap<>();
62 this.command = command;
66 * Process a list of keywords on this keyword node, recursing the keyword node tree, creating
67 * new branches for the keyword list if required. When the end of a branch has been reached,
68 * store the command in that keyword node..
70 * @param keywordList the list of keywords to process on this keyword node
71 * @param incomingCommand the command
73 public void processKeywords(final List<String> keywordList, final CommandLineCommand incomingCommand) {
74 if (keywordList.isEmpty()) {
75 this.command = incomingCommand;
79 if (!children.containsKey(keywordList.get(0))) {
80 children.put(keywordList.get(0), new KeywordNode(keywordList.get(0)));
83 final ArrayList<String> nextLevelKeywordList = new ArrayList<>(keywordList);
84 nextLevelKeywordList.remove(0);
85 children.get(keywordList.get(0)).processKeywords(nextLevelKeywordList, incomingCommand);
89 * Adds the system commands to the keyword node.
91 * @param systemCommandNodes the system command nodes to add to the keyword node
93 public void addSystemCommandNodes(final Set<KeywordNode> systemCommandNodes) {
94 if (children.isEmpty()) {
98 for (final KeywordNode node : children.values()) {
99 node.addSystemCommandNodes(systemCommandNodes);
102 for (final KeywordNode systemCommandNode : systemCommandNodes) {
103 children.put(systemCommandNode.getKeyword(), systemCommandNode);
109 * Gets the keyword of this keyword node.
111 * @return the keyword of this keyword node
113 public String getKeyword() {
118 * Gets the children of this keyword node.
120 * @return the children of this keyword node
122 public NavigableMap<String, KeywordNode> getChildren() {
127 * Gets the command of this keyword node.
129 * @return the command of this keyword node
131 public CommandLineCommand getCommand() {
139 public String toString() {
140 return "CommandKeywordNode [keyword=" + keyword + ", children=" + children + ", command=" + command + "]";
146 * @return the commands
148 public Set<CommandLineCommand> getCommands() {
149 final Set<CommandLineCommand> commandSet = new TreeSet<>();
151 for (final KeywordNode child : children.values()) {
152 if (child.getCommand() != null) {
153 commandSet.add(child.getCommand());
155 commandSet.addAll(child.getCommands());
165 public int compareTo(final KeywordNode otherKeywordNode) {
166 Assertions.argumentNotNull(otherKeywordNode, "comparison object may not be null");
168 if (this == otherKeywordNode) {
171 if (getClass() != otherKeywordNode.getClass()) {
172 return this.hashCode() - otherKeywordNode.hashCode();
175 final KeywordNode other = otherKeywordNode;
177 if (!keyword.equals(other.keyword)) {
178 return keyword.compareTo(other.keyword);
180 if (!children.equals(other.children)) {
181 return (children.hashCode() - other.children.hashCode());
183 return command.compareTo(otherKeywordNode.command);
187 public int hashCode() {
188 final int prime = 31;
190 result = prime * result + ((children == null) ? 0 : children.hashCode());
191 result = prime * result + ((command == null) ? 0 : command.hashCode());
192 result = prime * result + ((keyword == null) ? 0 : keyword.hashCode());
197 public boolean equals(Object obj) {
206 if (getClass() != obj.getClass()) {
210 return this.compareTo((KeywordNode) obj) == 0;