aboutsummaryrefslogtreecommitdiffstats
path: root/auth/cli-editor/src/main/java/org/onap/policy/apex/auth/clieditor/CommandLineCommand.java
blob: 38ff980101f55a7803c246f36d954b67f6aa9c3b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/*-
 * ============LICENSE_START=======================================================
 *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
 *  Modifications Copyright (C) 2019 Nordix Foundation.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * SPDX-License-Identifier: Apache-2.0
 * ============LICENSE_END=========================================================
 */

package org.onap.policy.apex.auth.clieditor;

import java.util.ArrayList;
import java.util.List;

import org.onap.policy.common.utils.validation.Assertions;

/**
 * This class represents a single Apex CLI command that is issued to the Apex Editor Java API
 * {@link org.onap.policy.apex.model.modelapi.ApexEditorApi}.
 *
 * @author Liam Fallon (liam.fallon@ericsson.com)
 */
public class CommandLineCommand implements Comparable<CommandLineCommand> {
    private String name = "";
    private final List<String> keywordlist = new ArrayList<>();
    private final List<CommandLineArgument> argumentList = new ArrayList<>();
    private String apiMethod = "";
    private boolean systemCommand = false;
    private String description = "";

    /**
     * Gets the class name of the class that executes this command in the Java API.
     *
     * @return the class name of the class that executes this command in the Java API
     */
    public String getApiClassName() {
        final int lastDotPos = apiMethod.lastIndexOf('.');
        if (lastDotPos == -1) {
            throw new CommandLineException("invalid API method name specified on command \"" + name
                    + "\", class name not found: " + apiMethod);
        }
        return apiMethod.substring(0, lastDotPos);
    }

    /**
     * Gets the method name of the method that executes this command in the Java API.
     *
     * @return the the method name of the method that executes this command in the Java API
     */
    public String getApiMethodName() {
        final int lastDotPos = apiMethod.lastIndexOf('.');
        if (lastDotPos == -1) {
            throw new CommandLineException("invalid API method name specified on command \"" + name
                    + "\", class name not found: " + apiMethod);
        }
        if (lastDotPos == apiMethod.length() - 1) {
            throw new CommandLineException("no API method name specified on command \"" + name + "\": " + apiMethod);
        }
        return apiMethod.substring(lastDotPos + 1);
    }

    /**
     * Gets the name of the editor command.
     *
     * @return the name of the editor command
     */
    public String getName() {
        return name;
    }

    /**
     * Sets the name of the editor command.
     *
     * @param name the name of the editor command
     */
    public void setName(final String name) {
        this.name = name;
    }

    /**
     * Gets the list of keywords for this command.
     *
     * @return the list of keywords for this command
     */
    public List<String> getKeywordlist() {
        return keywordlist;
    }

    /**
     * Gets the list of arguments for this command.
     *
     * @return the list of arguments for this command
     */
    public List<CommandLineArgument> getArgumentList() {
        return argumentList;
    }

    /**
     * Gets the method of the method that executes this command in the Java API.
     *
     * @return the method of the method that executes this command in the Java API
     */
    public String getApiMethod() {
        return apiMethod;
    }

    /**
     * Sets the method of the method that executes this command in the Java API.
     *
     * @param apiMethod the method of the method that executes this command in the Java API
     */
    public void setApiMethod(final String apiMethod) {
        this.apiMethod = apiMethod;
    }

    /**
     * Gets the description of the command.
     *
     * @return the description of the command
     */
    public String getDescription() {
        return description;
    }

    /**
     * Sets the description of the command.
     *
     * @param description the description of the command
     */
    public void setDescription(final String description) {
        this.description = description;
    }

    /**
     * Checks if this command is a system command.
     *
     * @return true, if this command is a system command
     */
    public boolean isSystemCommand() {
        return systemCommand;
    }

    /**
     * Sets whether this command is a system command.
     *
     * @param systemCommand whether this command is a system command
     */
    public void setSystemCommand(final boolean systemCommand) {
        this.systemCommand = systemCommand;
    }

    /**
     * Gets help for this command.
     *
     * @return the help for this command
     */
    public String getHelp() {
        final StringBuilder builder = new StringBuilder();
        for (final String keyword : keywordlist) {
            builder.append(keyword);
            builder.append(' ');
        }
        builder.append('{');
        builder.append(name);
        builder.append("}: ");
        builder.append(description);

        for (final CommandLineArgument argument : argumentList) {
            if (argument == null) {
                continue;
            }
            builder.append("\n\t");
            builder.append(argument.getHelp());
        }
        return builder.toString();
    }

    /**
     * {@inheritDoc}.
     */
    @Override
    public String toString() {
        return "CLICommand [name=" + name + ",keywordlist=" + keywordlist + ", argumentList=" + argumentList
                + ", apiMethod=" + apiMethod + ", systemCommand=" + systemCommand + ", description=" + description
                + "]";
    }

    /**
     * {@inheritDoc}.
     */
    @Override
    public int compareTo(final CommandLineCommand otherCommand) {
        Assertions.argumentNotNull(otherCommand, "comparison object may not be null");

        if (this == otherCommand) {
            return 0;
        }
        if (getClass() != otherCommand.getClass()) {
            return this.hashCode() - otherCommand.hashCode();
        }

        int result = compareKeywordList(otherCommand);
        if (result != 0) {
            return result;
        }

        if (!argumentList.equals(otherCommand.argumentList)) {
            return (argumentList.hashCode() - otherCommand.argumentList.hashCode());
        }

        if (systemCommand != otherCommand.systemCommand) {
            return (this.hashCode() - otherCommand.hashCode());
        }

        return apiMethod.compareTo(otherCommand.apiMethod);
    }

    /**
     * Compare the keyword lists of the commands.
     *
     * @param otherCommand the command to compare with
     * @return the int
     */
    private int compareKeywordList(final CommandLineCommand otherCommand) {
        for (int i = 0, j = 0;; i++, j++) {
            if (i < keywordlist.size() && j < otherCommand.keywordlist.size()) {
                if (!keywordlist.get(i).equals(otherCommand.keywordlist.get(j))) {
                    return keywordlist.get(i).compareTo(otherCommand.keywordlist.get(j));
                }
            } else if (i == keywordlist.size() && j == otherCommand.keywordlist.size()) {
                break;
            } else if (i == keywordlist.size()) {
                return -1;
            } else {
                return 1;
            }
        }

        return 0;
    }

    /**
     * {@inheritDoc}.
     */
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((apiMethod == null) ? 0 : apiMethod.hashCode());
        result = prime * result + argumentList.hashCode();
        result = prime * result + ((description == null) ? 0 : description.hashCode());
        result = prime * result + keywordlist.hashCode();
        result = prime * result + ((name == null) ? 0 : name.hashCode());
        result = prime * result + (systemCommand ? 1231 : 1237);
        return result;
    }

    /**
     * {@inheritDoc}.
     */
    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }

        if (obj == null) {
            return false;
        }

        if (getClass() != obj.getClass()) {
            return false;
        }

        return this.compareTo((CommandLineCommand) obj) == 0;
    }
}