Changes for checkstyle 8.32
[policy/apex-pdp.git] / auth / cli-editor / src / main / java / org / onap / policy / apex / auth / clieditor / KeywordNode.java
1 /*-
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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.apex.auth.clieditor;
23
24 import java.util.ArrayList;
25 import java.util.List;
26 import java.util.NavigableMap;
27 import java.util.Set;
28 import java.util.TreeMap;
29 import java.util.TreeSet;
30 import org.onap.policy.common.utils.validation.Assertions;
31
32 /**
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.
36  */
37 public class KeywordNode implements Comparable<KeywordNode> {
38     private final String keyword;
39     private final TreeMap<String, KeywordNode> children;
40     private CommandLineCommand command;
41
42     /**
43      * This Constructor creates a keyword node with the given keyword and no command.
44      *
45      * @param keyword the keyword of the node
46      */
47     public KeywordNode(final String keyword) {
48         this(keyword, null);
49     }
50
51     /**
52      * This Constructor creates a keyword node with the given keyword and command.
53      *
54      * @param keyword the keyword of the keyword node
55      * @param command the command associated with this keyword
56      */
57     public KeywordNode(final String keyword, final CommandLineCommand command) {
58         Assertions.argumentNotNull(keyword, "commands may not be null");
59
60         this.keyword = keyword;
61         children = new TreeMap<>();
62         this.command = command;
63     }
64
65     /**
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..
69      *
70      * @param keywordList the list of keywords to process on this keyword node
71      * @param incomingCommand the command
72      */
73     public void processKeywords(final List<String> keywordList, final CommandLineCommand incomingCommand) {
74         if (keywordList.isEmpty()) {
75             this.command = incomingCommand;
76             return;
77         }
78
79         if (!children.containsKey(keywordList.get(0))) {
80             children.put(keywordList.get(0), new KeywordNode(keywordList.get(0)));
81         }
82
83         final ArrayList<String> nextLevelKeywordList = new ArrayList<>(keywordList);
84         nextLevelKeywordList.remove(0);
85         children.get(keywordList.get(0)).processKeywords(nextLevelKeywordList, incomingCommand);
86     }
87
88     /**
89      * Adds the system commands to the keyword node.
90      *
91      * @param systemCommandNodes the system command nodes to add to the keyword node
92      */
93     public void addSystemCommandNodes(final Set<KeywordNode> systemCommandNodes) {
94         if (children.isEmpty()) {
95             return;
96         }
97
98         for (final KeywordNode node : children.values()) {
99             node.addSystemCommandNodes(systemCommandNodes);
100         }
101
102         for (final KeywordNode systemCommandNode : systemCommandNodes) {
103             children.put(systemCommandNode.getKeyword(), systemCommandNode);
104         }
105
106     }
107
108     /**
109      * Gets the keyword of this keyword node.
110      *
111      * @return the keyword of this keyword node
112      */
113     public String getKeyword() {
114         return keyword;
115     }
116
117     /**
118      * Gets the children of this keyword node.
119      *
120      * @return the children of this keyword node
121      */
122     public NavigableMap<String, KeywordNode> getChildren() {
123         return children;
124     }
125
126     /**
127      * Gets the command of this keyword node.
128      *
129      * @return the command of this keyword node
130      */
131     public CommandLineCommand getCommand() {
132         return command;
133     }
134
135     /**
136      * {@inheritDoc}.
137      */
138     @Override
139     public String toString() {
140         return "CommandKeywordNode [keyword=" + keyword + ", children=" + children + ", command=" + command + "]";
141     }
142
143     /**
144      * Gets the commands.
145      *
146      * @return the commands
147      */
148     public Set<CommandLineCommand> getCommands() {
149         final Set<CommandLineCommand> commandSet = new TreeSet<>();
150
151         for (final KeywordNode child : children.values()) {
152             if (child.getCommand() != null) {
153                 commandSet.add(child.getCommand());
154             }
155             commandSet.addAll(child.getCommands());
156         }
157
158         return commandSet;
159     }
160
161     /**
162      * {@inheritDoc}.
163      */
164     @Override
165     public int compareTo(final KeywordNode otherKeywordNode) {
166         Assertions.argumentNotNull(otherKeywordNode, "comparison object may not be null");
167
168         if (this == otherKeywordNode) {
169             return 0;
170         }
171         if (getClass() != otherKeywordNode.getClass()) {
172             return this.hashCode() - otherKeywordNode.hashCode();
173         }
174
175         final KeywordNode other = otherKeywordNode;
176
177         if (!keyword.equals(other.keyword)) {
178             return keyword.compareTo(other.keyword);
179         }
180         if (!children.equals(other.children)) {
181             return (children.hashCode() - other.children.hashCode());
182         }
183         return command.compareTo(otherKeywordNode.command);
184     }
185
186     @Override
187     public int hashCode() {
188         final int prime = 31;
189         int result = 1;
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());
193         return result;
194     }
195
196     @Override
197     public boolean equals(Object obj) {
198         if (this == obj) {
199             return true;
200         }
201
202         if (obj == null) {
203             return false;
204         }
205
206         if (getClass() != obj.getClass()) {
207             return false;
208         }
209
210         return this.compareTo((KeywordNode) obj) == 0;
211     }
212 }