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 org.onap.policy.common.utils.validation.Assertions;
29 * This class represents a single Apex CLI command that is issued to the Apex Editor Java API
30 * {@link org.onap.policy.apex.model.modelapi.ApexEditorApi}.
32 * @author Liam Fallon (liam.fallon@ericsson.com)
34 public class CommandLineCommand implements Comparable<CommandLineCommand> {
35 private String name = "";
36 private final List<String> keywordlist = new ArrayList<>();
37 private final List<CommandLineArgument> argumentList = new ArrayList<>();
38 private String apiMethod = "";
39 private boolean systemCommand = false;
40 private String description = "";
43 * Gets the class name of the class that executes this command in the Java API.
45 * @return the class name of the class that executes this command in the Java API
47 public String getApiClassName() {
48 final int lastDotPos = apiMethod.lastIndexOf('.');
49 if (lastDotPos == -1) {
50 throw new CommandLineException("invalid API method name specified on command \"" + name
51 + "\", class name not found: " + apiMethod);
53 return apiMethod.substring(0, lastDotPos);
57 * Gets the method name of the method that executes this command in the Java API.
59 * @return the the method name of the method that executes this command in the Java API
61 public String getApiMethodName() {
62 final int lastDotPos = apiMethod.lastIndexOf('.');
63 if (lastDotPos == -1) {
64 throw new CommandLineException("invalid API method name specified on command \"" + name
65 + "\", class name not found: " + apiMethod);
67 if (lastDotPos == apiMethod.length() - 1) {
68 throw new CommandLineException("no API method name specified on command \"" + name + "\": " + apiMethod);
70 return apiMethod.substring(lastDotPos + 1);
74 * Gets the name of the editor command.
76 * @return the name of the editor command
78 public String getName() {
83 * Sets the name of the editor command.
85 * @param name the name of the editor command
87 public void setName(final String name) {
92 * Gets the list of keywords for this command.
94 * @return the list of keywords for this command
96 public List<String> getKeywordlist() {
101 * Gets the list of arguments for this command.
103 * @return the list of arguments for this command
105 public List<CommandLineArgument> getArgumentList() {
110 * Gets the method of the method that executes this command in the Java API.
112 * @return the method of the method that executes this command in the Java API
114 public String getApiMethod() {
119 * Sets the method of the method that executes this command in the Java API.
121 * @param apiMethod the method of the method that executes this command in the Java API
123 public void setApiMethod(final String apiMethod) {
124 this.apiMethod = apiMethod;
128 * Gets the description of the command.
130 * @return the description of the command
132 public String getDescription() {
137 * Sets the description of the command.
139 * @param description the description of the command
141 public void setDescription(final String description) {
142 this.description = description;
146 * Checks if this command is a system command.
148 * @return true, if this command is a system command
150 public boolean isSystemCommand() {
151 return systemCommand;
155 * Sets whether this command is a system command.
157 * @param systemCommand whether this command is a system command
159 public void setSystemCommand(final boolean systemCommand) {
160 this.systemCommand = systemCommand;
164 * Gets help for this command.
166 * @return the help for this command
168 public String getHelp() {
169 final StringBuilder builder = new StringBuilder();
170 for (final String keyword : keywordlist) {
171 builder.append(keyword);
175 builder.append(name);
176 builder.append("}: ");
177 builder.append(description);
179 for (final CommandLineArgument argument : argumentList) {
180 if (argument == null) {
183 builder.append("\n\t");
184 builder.append(argument.getHelp());
186 return builder.toString();
193 public String toString() {
194 return "CLICommand [name=" + name + ",keywordlist=" + keywordlist + ", argumentList=" + argumentList
195 + ", apiMethod=" + apiMethod + ", systemCommand=" + systemCommand + ", description=" + description
203 public int compareTo(final CommandLineCommand otherCommand) {
204 Assertions.argumentNotNull(otherCommand, "comparison object may not be null");
206 if (this == otherCommand) {
209 if (getClass() != otherCommand.getClass()) {
210 return this.hashCode() - otherCommand.hashCode();
213 int result = compareKeywordList(otherCommand);
218 if (!argumentList.equals(otherCommand.argumentList)) {
219 return (argumentList.hashCode() - otherCommand.argumentList.hashCode());
222 if (systemCommand != otherCommand.systemCommand) {
223 return (this.hashCode() - otherCommand.hashCode());
226 return apiMethod.compareTo(otherCommand.apiMethod);
230 * Compare the keyword lists of the commands.
232 * @param otherCommand the command to compare with
235 private int compareKeywordList(final CommandLineCommand otherCommand) {
236 for (int i = 0, j = 0;; i++, j++) {
237 if (i < keywordlist.size() && j < otherCommand.keywordlist.size()) {
238 if (!keywordlist.get(i).equals(otherCommand.keywordlist.get(j))) {
239 return keywordlist.get(i).compareTo(otherCommand.keywordlist.get(j));
241 } else if (i == keywordlist.size() && j == otherCommand.keywordlist.size()) {
243 } else if (i == keywordlist.size()) {
257 public int hashCode() {
258 final int prime = 31;
260 result = prime * result + ((apiMethod == null) ? 0 : apiMethod.hashCode());
261 result = prime * result + argumentList.hashCode();
262 result = prime * result + ((description == null) ? 0 : description.hashCode());
263 result = prime * result + keywordlist.hashCode();
264 result = prime * result + ((name == null) ? 0 : name.hashCode());
265 result = prime * result + (systemCommand ? 1231 : 1237);
273 public boolean equals(Object obj) {
282 if (getClass() != obj.getClass()) {
286 return this.compareTo((CommandLineCommand) obj) == 0;