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;
27 import org.onap.policy.common.utils.validation.Assertions;
30 * This class represents a single Apex CLI command that is issued to the Apex Editor Java API
31 * {@link org.onap.policy.apex.model.modelapi.ApexEditorApi}.
33 * @author Liam Fallon (liam.fallon@ericsson.com)
35 public class CommandLineCommand implements Comparable<CommandLineCommand> {
36 private String name = "";
37 private final List<String> keywordlist = new ArrayList<>();
38 private final List<CommandLineArgument> argumentList = new ArrayList<>();
39 private String apiMethod = "";
40 private boolean systemCommand = false;
41 private String description = "";
44 * Gets the class name of the class that executes this command in the Java API.
46 * @return the class name of the class that executes this command in the Java API
48 public String getApiClassName() {
49 final int lastDotPos = apiMethod.lastIndexOf('.');
50 if (lastDotPos == -1) {
51 throw new CommandLineException("invalid API method name specified on command \"" + name
52 + "\", class name not found: " + apiMethod);
54 return apiMethod.substring(0, lastDotPos);
58 * Gets the method name of the method that executes this command in the Java API.
60 * @return the the method name of the method that executes this command in the Java API
62 public String getApiMethodName() {
63 final int lastDotPos = apiMethod.lastIndexOf('.');
64 if (lastDotPos == -1) {
65 throw new CommandLineException("invalid API method name specified on command \"" + name
66 + "\", class name not found: " + apiMethod);
68 if (lastDotPos == apiMethod.length() - 1) {
69 throw new CommandLineException("no API method name specified on command \"" + name + "\": " + apiMethod);
71 return apiMethod.substring(lastDotPos + 1);
75 * Gets the name of the editor command.
77 * @return the name of the editor command
79 public String getName() {
84 * Sets the name of the editor command.
86 * @param name the name of the editor command
88 public void setName(final String name) {
93 * Gets the list of keywords for this command.
95 * @return the list of keywords for this command
97 public List<String> getKeywordlist() {
102 * Gets the list of arguments for this command.
104 * @return the list of arguments for this command
106 public List<CommandLineArgument> getArgumentList() {
111 * Gets the method of the method that executes this command in the Java API.
113 * @return the method of the method that executes this command in the Java API
115 public String getApiMethod() {
120 * Sets the method of the method that executes this command in the Java API.
122 * @param apiMethod the method of the method that executes this command in the Java API
124 public void setApiMethod(final String apiMethod) {
125 this.apiMethod = apiMethod;
129 * Gets the description of the command.
131 * @return the description of the command
133 public String getDescription() {
138 * Sets the description of the command.
140 * @param description the description of the command
142 public void setDescription(final String description) {
143 this.description = description;
147 * Checks if this command is a system command.
149 * @return true, if this command is a system command
151 public boolean isSystemCommand() {
152 return systemCommand;
156 * Sets whether this command is a system command.
158 * @param systemCommand whether this command is a system command
160 public void setSystemCommand(final boolean systemCommand) {
161 this.systemCommand = systemCommand;
165 * Gets help for this command.
167 * @return the help for this command
169 public String getHelp() {
170 final StringBuilder builder = new StringBuilder();
171 for (final String keyword : keywordlist) {
172 builder.append(keyword);
176 builder.append(name);
177 builder.append("}: ");
178 builder.append(description);
180 for (final CommandLineArgument argument : argumentList) {
181 if (argument == null) {
184 builder.append("\n\t");
185 builder.append(argument.getHelp());
187 return builder.toString();
194 public String toString() {
195 return "CLICommand [name=" + name + ",keywordlist=" + keywordlist + ", argumentList=" + argumentList
196 + ", apiMethod=" + apiMethod + ", systemCommand=" + systemCommand + ", description=" + description
204 public int compareTo(final CommandLineCommand otherCommand) {
205 Assertions.argumentNotNull(otherCommand, "comparison object may not be null");
207 if (this == otherCommand) {
210 if (getClass() != otherCommand.getClass()) {
211 return this.hashCode() - otherCommand.hashCode();
214 int result = compareKeywordList(otherCommand);
219 if (!argumentList.equals(otherCommand.argumentList)) {
220 return (argumentList.hashCode() - otherCommand.argumentList.hashCode());
223 if (systemCommand != otherCommand.systemCommand) {
224 return (this.hashCode() - otherCommand.hashCode());
227 return apiMethod.compareTo(otherCommand.apiMethod);
231 * Compare the keyword lists of the commands.
233 * @param otherCommand the command to compare with
236 private int compareKeywordList(final CommandLineCommand otherCommand) {
237 for (int i = 0, j = 0;; i++, j++) {
238 if (i < keywordlist.size() && j < otherCommand.keywordlist.size()) {
239 if (!keywordlist.get(i).equals(otherCommand.keywordlist.get(j))) {
240 return keywordlist.get(i).compareTo(otherCommand.keywordlist.get(j));
242 } else if (i == keywordlist.size() && j == otherCommand.keywordlist.size()) {
244 } else if (i == keywordlist.size()) {
258 public int hashCode() {
259 final int prime = 31;
261 result = prime * result + ((apiMethod == null) ? 0 : apiMethod.hashCode());
262 result = prime * result + argumentList.hashCode();
263 result = prime * result + ((description == null) ? 0 : description.hashCode());
264 result = prime * result + keywordlist.hashCode();
265 result = prime * result + ((name == null) ? 0 : name.hashCode());
266 result = prime * result + (systemCommand ? 1231 : 1237);
274 public boolean equals(Object obj) {
283 if (getClass() != obj.getClass()) {
287 return this.compareTo((CommandLineCommand) obj) == 0;