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 package com.ivata.mask.web.format;
71 /***
72 * <p>
73 * Format a string by appending a leading character. You can specify either an
74 * absolute length of the final string, or a number of characters to append.
75 * </p>
76 *
77 * @since ivata masks 0.4 (2002-10-24)
78 * @author Colin MacLeod
79 * <a href='mailto:colin.macleod@ivata.com'>colin.macleod@ivata.com</a>
80 * @version $Revision: 1.3 $
81 */
82 public class LeadingCharacterFormat implements HTMLFormat {
83 /***
84 * <p>
85 * If <code>false</code>, the <code>count</code> from {@link #setCount
86 * setCount} represents a total number of characters to prepend. Otherwise (
87 * <code>true</code>- default setting) it represents a maximum string
88 * length for the whole string returned in {@link #format format}.
89 * </p>
90 */
91 private boolean countIsMaximum = true;
92 /***
93 * <p>
94 * This character will be prepended to the string.
95 * </p>
96 */
97 private char character = ' ';
98 /***
99 * <p>
100 * The number of times the character will be prepended.
101 * </p>
102 */
103 private int count = 1;
104 /***
105 * <p>
106 * Takes the text provided and prepends the character defined in
107 * {@link #setCharacter setCharacter}the number of times defined in
108 * {@link #setCount setCount}. If you specified this is a maximum (
109 * {@link #setMaximum setMaximum(true)}), then only the rightmost count of
110 * characters are returned.
111 * </p>
112 *
113 * @param hTMLText Refer to {@link HTMLFormat#format}.
114 * @return Refer to {@link HTMLFormat#format}.
115 */
116 public final String format(final String hTMLText) {
117
118
119 int length = hTMLText.length();
120 if (countIsMaximum && (length > count)) {
121 return hTMLText.substring(length - count - 1);
122 }
123
124 StringBuffer buffer = new StringBuffer(hTMLText).reverse();
125 for (int i = 0; (i < count)
126 && (!countIsMaximum || (buffer.length() < count)); ++i) {
127 buffer.append(character);
128 }
129 return buffer.reverse().toString();
130 }
131 /***
132 * <p>
133 * Get whether the number specified in {@link #setCount setCount}should be
134 * a maximum string length, or the number of characters to prepend.
135 *
136 * @return <code>false</code> if the <code>count</code> from {@link
137 * #setCount setCount} represents a total number of characters to
138 * prepend, or <code>true</code>- default setting - represents a
139 * maximum string length for the whole string returned in
140 * {@link #format}.
141 * </p>
142 */
143 public final boolean getCountIsMaximum() {
144 return countIsMaximum;
145 }
146 /***
147 * <p>
148 * Set whether the number specified in {@link #setCount setCount}should be
149 * a maximum string length, or the number of characters to prepend.
150 *
151 * @param countIsMaximumParam
152 * set to <code>false</code> if the <code>count</code> from
153 * {@link #setCount setCount}represents a total number of
154 * characters to prepend, or <code>true</code>- default
155 * setting - represents a maximum string length for the whole
156 * string returned in {@link #format format}.
157 * </p>
158 */
159 public final void setCountIsMaximum(final boolean countIsMaximumParam) {
160 this.countIsMaximum = countIsMaximumParam;
161 }
162 /***
163 * <p>
164 * This character will be prepended to the string.
165 * </p>
166 *
167 * @return the current value of the character which will prepended.
168 */
169 public final char getCharacter() {
170 return character;
171 }
172 /***
173 * <p>
174 * This character will be prepended to the string.
175 * </p>
176 *
177 * @param characterParam
178 * the new value of the character which will be prepended.
179 */
180 public final void setCharacter(final char characterParam) {
181 this.character = characterParam;
182 }
183 /***
184 * <p>
185 * The number of times the character will be prepended.
186 * </p>
187 *
188 * @return the current value of count.
189 */
190 public final int getCount() {
191 return count;
192 }
193 /***
194 * <p>
195 * The number of times the character will be prepended.
196 * </p>
197 *
198 * @param countParam
199 * the new value of count.
200 */
201 public final void setCount(final int countParam) {
202 this.count = countParam;
203 }
204 }
205