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