38ff980101f55a7803c246f36d954b67f6aa9c3b
[policy/apex-pdp.git] / auth / cli-editor / src / main / java / org / onap / policy / apex / auth / clieditor / CommandLineCommand.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
27 import org.onap.policy.common.utils.validation.Assertions;
28
29 /**
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}.
32  *
33  * @author Liam Fallon (liam.fallon@ericsson.com)
34  */
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 = "";
42
43     /**
44      * Gets the class name of the class that executes this command in the Java API.
45      *
46      * @return the class name of the class that executes this command in the Java API
47      */
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);
53         }
54         return apiMethod.substring(0, lastDotPos);
55     }
56
57     /**
58      * Gets the method name of the method that executes this command in the Java API.
59      *
60      * @return the the method name of the method that executes this command in the Java API
61      */
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);
67         }
68         if (lastDotPos == apiMethod.length() - 1) {
69             throw new CommandLineException("no API method name specified on command \"" + name + "\": " + apiMethod);
70         }
71         return apiMethod.substring(lastDotPos + 1);
72     }
73
74     /**
75      * Gets the name of the editor command.
76      *
77      * @return the name of the editor command
78      */
79     public String getName() {
80         return name;
81     }
82
83     /**
84      * Sets the name of the editor command.
85      *
86      * @param name the name of the editor command
87      */
88     public void setName(final String name) {
89         this.name = name;
90     }
91
92     /**
93      * Gets the list of keywords for this command.
94      *
95      * @return the list of keywords for this command
96      */
97     public List<String> getKeywordlist() {
98         return keywordlist;
99     }
100
101     /**
102      * Gets the list of arguments for this command.
103      *
104      * @return the list of arguments for this command
105      */
106     public List<CommandLineArgument> getArgumentList() {
107         return argumentList;
108     }
109
110     /**
111      * Gets the method of the method that executes this command in the Java API.
112      *
113      * @return the method of the method that executes this command in the Java API
114      */
115     public String getApiMethod() {
116         return apiMethod;
117     }
118
119     /**
120      * Sets the method of the method that executes this command in the Java API.
121      *
122      * @param apiMethod the method of the method that executes this command in the Java API
123      */
124     public void setApiMethod(final String apiMethod) {
125         this.apiMethod = apiMethod;
126     }
127
128     /**
129      * Gets the description of the command.
130      *
131      * @return the description of the command
132      */
133     public String getDescription() {
134         return description;
135     }
136
137     /**
138      * Sets the description of the command.
139      *
140      * @param description the description of the command
141      */
142     public void setDescription(final String description) {
143         this.description = description;
144     }
145
146     /**
147      * Checks if this command is a system command.
148      *
149      * @return true, if this command is a system command
150      */
151     public boolean isSystemCommand() {
152         return systemCommand;
153     }
154
155     /**
156      * Sets whether this command is a system command.
157      *
158      * @param systemCommand whether this command is a system command
159      */
160     public void setSystemCommand(final boolean systemCommand) {
161         this.systemCommand = systemCommand;
162     }
163
164     /**
165      * Gets help for this command.
166      *
167      * @return the help for this command
168      */
169     public String getHelp() {
170         final StringBuilder builder = new StringBuilder();
171         for (final String keyword : keywordlist) {
172             builder.append(keyword);
173             builder.append(' ');
174         }
175         builder.append('{');
176         builder.append(name);
177         builder.append("}: ");
178         builder.append(description);
179
180         for (final CommandLineArgument argument : argumentList) {
181             if (argument == null) {
182                 continue;
183             }
184             builder.append("\n\t");
185             builder.append(argument.getHelp());
186         }
187         return builder.toString();
188     }
189
190     /**
191      * {@inheritDoc}.
192      */
193     @Override
194     public String toString() {
195         return "CLICommand [name=" + name + ",keywordlist=" + keywordlist + ", argumentList=" + argumentList
196                 + ", apiMethod=" + apiMethod + ", systemCommand=" + systemCommand + ", description=" + description
197                 + "]";
198     }
199
200     /**
201      * {@inheritDoc}.
202      */
203     @Override
204     public int compareTo(final CommandLineCommand otherCommand) {
205         Assertions.argumentNotNull(otherCommand, "comparison object may not be null");
206
207         if (this == otherCommand) {
208             return 0;
209         }
210         if (getClass() != otherCommand.getClass()) {
211             return this.hashCode() - otherCommand.hashCode();
212         }
213
214         int result = compareKeywordList(otherCommand);
215         if (result != 0) {
216             return result;
217         }
218
219         if (!argumentList.equals(otherCommand.argumentList)) {
220             return (argumentList.hashCode() - otherCommand.argumentList.hashCode());
221         }
222
223         if (systemCommand != otherCommand.systemCommand) {
224             return (this.hashCode() - otherCommand.hashCode());
225         }
226
227         return apiMethod.compareTo(otherCommand.apiMethod);
228     }
229
230     /**
231      * Compare the keyword lists of the commands.
232      *
233      * @param otherCommand the command to compare with
234      * @return the int
235      */
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));
241                 }
242             } else if (i == keywordlist.size() && j == otherCommand.keywordlist.size()) {
243                 break;
244             } else if (i == keywordlist.size()) {
245                 return -1;
246             } else {
247                 return 1;
248             }
249         }
250
251         return 0;
252     }
253
254     /**
255      * {@inheritDoc}.
256      */
257     @Override
258     public int hashCode() {
259         final int prime = 31;
260         int result = 1;
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);
267         return result;
268     }
269
270     /**
271      * {@inheritDoc}.
272      */
273     @Override
274     public boolean equals(Object obj) {
275         if (this == obj) {
276             return true;
277         }
278
279         if (obj == null) {
280             return false;
281         }
282
283         if (getClass() != obj.getClass()) {
284             return false;
285         }
286
287         return this.compareTo((CommandLineCommand) obj) == 0;
288     }
289 }