Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
HelpLineImpl |
|
| 1.8;1.8 |
1 | /** | |
2 | * Licensed to the Apache Software Foundation (ASF) under one or more | |
3 | * contributor license agreements. See the NOTICE file distributed with | |
4 | * this work for additional information regarding copyright ownership. | |
5 | * The ASF licenses this file to You under the Apache License, Version 2.0 | |
6 | * (the "License"); you may not use this file except in compliance with | |
7 | * the License. You may obtain a copy of the License at | |
8 | * | |
9 | * http://www.apache.org/licenses/LICENSE-2.0 | |
10 | * | |
11 | * Unless required by applicable law or agreed to in writing, software | |
12 | * distributed under the License is distributed on an "AS IS" BASIS, | |
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
14 | * See the License for the specific language governing permissions and | |
15 | * limitations under the License. | |
16 | */ | |
17 | package org.apache.commons.cli2.option; | |
18 | ||
19 | import java.util.Comparator; | |
20 | import java.util.Set; | |
21 | ||
22 | import org.apache.commons.cli2.HelpLine; | |
23 | import org.apache.commons.cli2.Option; | |
24 | ||
25 | /** | |
26 | * Represents a line in the help screen. | |
27 | */ | |
28 | public class HelpLineImpl implements HelpLine { | |
29 | ||
30 | /** The option that this HelpLineImpl describes */ | |
31 | private final Option option; | |
32 | ||
33 | /** The level of indenting for this item */ | |
34 | private final int indent; | |
35 | ||
36 | /** The help settings used to obtain the previous usage */ | |
37 | private transient Set cachedHelpSettings; | |
38 | ||
39 | /** The comparator used to obtain the previous usage */ | |
40 | private transient Comparator cachedComparator; | |
41 | ||
42 | /** The previously obtained usage */ | |
43 | private transient String cachedUsage; | |
44 | ||
45 | /** | |
46 | * Creates a new HelpLineImpl to represent a particular Option in the online | |
47 | * help. | |
48 | * | |
49 | * @param option | |
50 | * Option that the HelpLineImpl describes | |
51 | * @param indent | |
52 | * Level of indentation for this line | |
53 | */ | |
54 | 144 | public HelpLineImpl(final Option option, final int indent) { |
55 | 144 | this.option = option; |
56 | 144 | this.indent = indent; |
57 | 144 | } |
58 | ||
59 | /** | |
60 | * @return The description of the option | |
61 | */ | |
62 | public String getDescription() { | |
63 | 119 | return option.getDescription(); |
64 | } | |
65 | ||
66 | /** | |
67 | * @return The level of indentation for this line | |
68 | */ | |
69 | public int getIndent() { | |
70 | 25 | return indent; |
71 | } | |
72 | ||
73 | /** | |
74 | * @return The Option that the help line relates to | |
75 | */ | |
76 | public Option getOption() { | |
77 | 25 | return option; |
78 | } | |
79 | ||
80 | /** | |
81 | * Builds a usage string for the option using the specified settings and | |
82 | * comparator. | |
83 | * | |
84 | * | |
85 | * @param helpSettings the settings to apply | |
86 | * @param comparator a comparator to sort options when applicable | |
87 | * @return the usage string | |
88 | */ | |
89 | public String usage(final Set helpSettings, final Comparator comparator) { | |
90 | 238 | if (cachedUsage == null |
91 | || cachedHelpSettings != helpSettings | |
92 | || cachedComparator != comparator) { | |
93 | ||
94 | // cache the arguments to avoid redoing work | |
95 | 119 | cachedHelpSettings = helpSettings; |
96 | 119 | cachedComparator = comparator; |
97 | ||
98 | // build the new buffer | |
99 | 119 | final StringBuffer buffer = new StringBuffer(); |
100 | 240 | for (int i = 0; i < indent; ++i) { |
101 | 121 | buffer.append(" "); |
102 | } | |
103 | 119 | option.appendUsage(buffer, helpSettings, comparator); |
104 | ||
105 | // cache the usage string | |
106 | 119 | cachedUsage = buffer.toString(); |
107 | } | |
108 | 238 | return cachedUsage; |
109 | } | |
110 | } |