Commit | Line | Data |
---|---|---|
a3b510ab NR |
1 | package be.nikiroo.jvcard; |
2 | ||
26d2bd05 NR |
3 | import java.io.File; |
4 | import java.io.IOException; | |
5 | import java.util.ArrayList; | |
a3b510ab NR |
6 | import java.util.HashMap; |
7 | import java.util.LinkedList; | |
8 | import java.util.List; | |
9 | import java.util.Map; | |
e253bd50 | 10 | import java.util.UUID; |
a3b510ab NR |
11 | |
12 | import be.nikiroo.jvcard.parsers.Format; | |
13 | import be.nikiroo.jvcard.parsers.Parser; | |
7da41ecd | 14 | import be.nikiroo.jvcard.resources.StringUtils; |
a3b510ab NR |
15 | |
16 | /** | |
17 | * A contact is the information that represent a contact person or organisation. | |
18 | * | |
19 | * @author niki | |
20 | * | |
21 | */ | |
26d2bd05 | 22 | public class Contact extends BaseClass<Data> { |
a3b510ab NR |
23 | private int nextBKey = 1; |
24 | private Map<Integer, Data> binaries; | |
a3b510ab NR |
25 | |
26 | /** | |
27 | * Create a new Contact from the given information. Note that the BKeys data | |
28 | * will be reset. | |
29 | * | |
30 | * @param content | |
31 | * the information about the contact | |
32 | */ | |
33 | public Contact(List<Data> content) { | |
26d2bd05 | 34 | super(load(content)); |
a3b510ab NR |
35 | updateBKeys(true); |
36 | } | |
37 | ||
a3b510ab NR |
38 | /** |
39 | * Return the preferred Data field with the given name, or NULL if none. | |
40 | * | |
41 | * @param name | |
42 | * the name to look for | |
43 | * @return the Data field, or NULL | |
44 | */ | |
45 | public Data getPreferredData(String name) { | |
46 | Data first = null; | |
47 | for (Data data : getData(name)) { | |
48 | if (first == null) | |
49 | first = data; | |
3634193b NR |
50 | |
51 | if (data.isPreferred()) | |
52 | return data; | |
a3b510ab NR |
53 | } |
54 | ||
55 | return first; | |
56 | } | |
57 | ||
58 | /** | |
59 | * Return the value of the preferred data field with this name, or NULL if | |
60 | * none (you cannot differentiate a NULL value and no value). | |
61 | * | |
62 | * @param name | |
63 | * the name to look for | |
64 | * @return the value (which can be NULL), or NULL | |
65 | */ | |
66 | public String getPreferredDataValue(String name) { | |
67 | Data data = getPreferredData(name); | |
68 | if (data != null && data.getValue() != null) | |
69 | return data.getValue().trim(); | |
70 | return null; | |
71 | } | |
72 | ||
73 | /** | |
74 | * Get the Data fields that share the given name. | |
75 | * | |
76 | * @param name | |
77 | * the name to ook for | |
78 | * @return a list of Data fields with this name | |
79 | */ | |
80 | public List<Data> getData(String name) { | |
81 | List<Data> found = new LinkedList<Data>(); | |
82 | ||
26d2bd05 | 83 | for (Data data : this) { |
a3b510ab NR |
84 | if (data.getName().equals(name)) |
85 | found.add(data); | |
86 | } | |
87 | ||
88 | return found; | |
89 | } | |
90 | ||
0b0b2b0f NR |
91 | /** |
92 | * Return a {@link String} representation of this contact formated | |
93 | * accordingly to the given format. | |
94 | * | |
26d254a3 | 95 | * <p> |
0b0b2b0f | 96 | * The format is basically a list of field names separated by a pipe and |
30a4aa17 NR |
97 | * optionally parametrised with the 'at' (@) symbol. The parameters allows |
98 | * you to: | |
0b0b2b0f NR |
99 | * <ul> |
100 | * <li>@x: show only a present/not present info</li> | |
101 | * <li>@n: limit the size to a fixed value 'n'</li> | |
102 | * <li>@+: expand the size of this field as much as possible</li> | |
103 | * </ul> | |
26d254a3 | 104 | * </p> |
0b0b2b0f | 105 | * |
26d254a3 | 106 | * <p> |
30a4aa17 NR |
107 | * In case of lists or multiple-fields values, you can select a specific |
108 | * list or field with: | |
109 | * <ul> | |
110 | * <li>FIELD@(0): select the first value in a list</li> | |
111 | * <li>FIELD@[1]: select the second field in a multiple-fields value</li> | |
112 | * </ul> | |
113 | * </p> | |
114 | * | |
115 | * <p> | |
26d254a3 NR |
116 | * You can also add a fixed text if it starts with a simple-quote ('). |
117 | * </p> | |
118 | * | |
119 | * <p> | |
120 | * Example: "'Contact: |N@10|FN@20|NICK@+|PHOTO@x" | |
121 | * </p> | |
0b0b2b0f NR |
122 | * |
123 | * @param format | |
124 | * the format to use | |
26d254a3 NR |
125 | * @param separator |
126 | * the separator {@link String} to use between fields | |
0b0b2b0f NR |
127 | * |
128 | * @return the {@link String} representation | |
129 | */ | |
26d254a3 NR |
130 | public String toString(String format, String separator) { |
131 | return toString(format, separator, null, -1, true, false); | |
0b0b2b0f NR |
132 | } |
133 | ||
a3b510ab NR |
134 | /** |
135 | * Return a {@link String} representation of this contact formated | |
136 | * accordingly to the given format. | |
137 | * | |
26d254a3 | 138 | * <p> |
a3b510ab NR |
139 | * The format is basically a list of field names separated by a pipe and |
140 | * optionally parametrised. The parameters allows you to: | |
141 | * <ul> | |
26d254a3 | 142 | * <li>@x: show only a present/not present info</li> |
a3b510ab NR |
143 | * <li>@n: limit the size to a fixed value 'n'</li> |
144 | * <li>@+: expand the size of this field as much as possible</li> | |
145 | * </ul> | |
26d254a3 NR |
146 | * </p> |
147 | * | |
148 | * <p> | |
30a4aa17 NR |
149 | * In case of lists or multiple-fields values, you can select a specific |
150 | * list or field with: | |
151 | * <ul> | |
152 | * <li>FIELD@(0): select the first value in a list</li> | |
153 | * <li>FIELD@[1]: select the second field in a multiple-fields value</li> | |
154 | * </ul> | |
155 | * </p> | |
156 | * | |
157 | * <p> | |
26d254a3 NR |
158 | * You can also add a fixed text if it starts with a simple-quote ('). |
159 | * </p> | |
a3b510ab | 160 | * |
26d254a3 NR |
161 | * <p> |
162 | * Example: "'Contact: |N@10|FN@20|NICK@+|PHOTO@x" | |
163 | * </p> | |
a3b510ab NR |
164 | * |
165 | * @param format | |
166 | * the format to use | |
167 | * @param separator | |
168 | * the separator {@link String} to use between fields | |
0b0b2b0f NR |
169 | * @param padding |
170 | * the {@link String} to use for left and right padding | |
a3b510ab NR |
171 | * @param width |
172 | * a fixed width or -1 for "as long as needed" | |
173 | * | |
296a0b75 NR |
174 | * @param unicode |
175 | * allow Uniode or only ASCII characters | |
176 | * | |
a3b510ab NR |
177 | * @return the {@link String} representation |
178 | */ | |
0b0b2b0f | 179 | public String toString(String format, String separator, String padding, |
296a0b75 | 180 | int width, boolean unicode, boolean removeAccents) { |
9c8baf0c | 181 | StringBuilder builder = new StringBuilder(); |
a3b510ab | 182 | |
296a0b75 NR |
183 | for (String str : toStringArray(format, separator, padding, width, |
184 | unicode)) { | |
0b0b2b0f NR |
185 | builder.append(str); |
186 | } | |
a3b510ab | 187 | |
0b0b2b0f NR |
188 | return builder.toString(); |
189 | } | |
a3b510ab | 190 | |
0b0b2b0f NR |
191 | /** |
192 | * Return a {@link String} representation of this contact formated | |
193 | * accordingly to the given format, part by part. | |
194 | * | |
26d254a3 | 195 | * <p> |
0b0b2b0f NR |
196 | * The format is basically a list of field names separated by a pipe and |
197 | * optionally parametrised. The parameters allows you to: | |
198 | * <ul> | |
199 | * <li>@x: show only a present/not present info</li> | |
200 | * <li>@n: limit the size to a fixed value 'n'</li> | |
201 | * <li>@+: expand the size of this field as much as possible</li> | |
202 | * </ul> | |
26d254a3 | 203 | * </p> |
0b0b2b0f | 204 | * |
26d254a3 | 205 | * <p> |
30a4aa17 NR |
206 | * In case of lists or multiple-fields values, you can select a specific |
207 | * list or field with: | |
208 | * <ul> | |
209 | * <li>FIELD@(0): select the first value in a list</li> | |
210 | * <li>FIELD@[1]: select the second field in a multiple-fields value</li> | |
211 | * </ul> | |
212 | * </p> | |
213 | * | |
214 | * <p> | |
26d254a3 NR |
215 | * You can also add a fixed text if it starts with a simple-quote ('). |
216 | * </p> | |
217 | * | |
218 | * <p> | |
219 | * Example: "'Contact: |N@10|FN@20|NICK@+|PHOTO@x" | |
220 | * </p> | |
0b0b2b0f NR |
221 | * |
222 | * @param format | |
223 | * the format to use | |
224 | * @param separator | |
225 | * the separator {@link String} to use between fields | |
226 | * @param padding | |
227 | * the {@link String} to use for left and right padding | |
228 | * @param width | |
229 | * a fixed width or -1 for "as long as needed" | |
230 | * | |
296a0b75 NR |
231 | * @param unicode |
232 | * allow Uniode or only ASCII characters | |
233 | * | |
0b0b2b0f NR |
234 | * @return the {@link String} representation |
235 | */ | |
236 | public String[] toStringArray(String format, String separator, | |
296a0b75 | 237 | String padding, int width, boolean unicode) { |
0b0b2b0f NR |
238 | if (width > -1) { |
239 | int numOfFields = format.split("\\|").length; | |
240 | if (separator != null) | |
241 | width -= (numOfFields - 1) * separator.length(); | |
242 | if (padding != null) | |
243 | width -= (numOfFields) * (2 * padding.length()); | |
244 | ||
245 | if (width < 0) | |
246 | width = 0; | |
a3b510ab NR |
247 | } |
248 | ||
0b0b2b0f NR |
249 | List<String> str = new LinkedList<String>(); |
250 | ||
251 | boolean first = true; | |
296a0b75 | 252 | for (String s : toStringArray(format, width, unicode)) { |
0b0b2b0f NR |
253 | if (!first) { |
254 | str.add(separator); | |
255 | } | |
256 | ||
257 | if (padding != null) | |
258 | str.add(padding + s + padding); | |
259 | else | |
260 | str.add(s); | |
261 | ||
262 | first = false; | |
9c8baf0c NR |
263 | } |
264 | ||
0b0b2b0f | 265 | return str.toArray(new String[] {}); |
9c8baf0c NR |
266 | } |
267 | ||
268 | /** | |
269 | * Return a {@link String} representation of this contact formated | |
270 | * accordingly to the given format, part by part. | |
271 | * | |
26d254a3 | 272 | * <p> |
9c8baf0c | 273 | * The format is basically a list of field names separated by a pipe and |
0b0b2b0f NR |
274 | * optionally parametrised. The parameters allows you to: |
275 | * <ul> | |
276 | * <li>@x: show only a present/not present info</li> | |
277 | * <li>@n: limit the size to a fixed value 'n'</li> | |
278 | * <li>@+: expand the size of this field as much as possible</li> | |
279 | * </ul> | |
26d254a3 NR |
280 | * </p> |
281 | * | |
282 | * <p> | |
30a4aa17 NR |
283 | * In case of lists or multiple-fields values, you can select a specific |
284 | * list or field with: | |
285 | * <ul> | |
286 | * <li>FIELD@(0): select the first value in a list</li> | |
287 | * <li>FIELD@[1]: select the second field in a multiple-fields value</li> | |
288 | * </ul> | |
289 | * </p> | |
290 | * | |
291 | * <p> | |
26d254a3 NR |
292 | * You can also add a fixed text if it starts with a simple-quote ('). |
293 | * </p> | |
0b0b2b0f | 294 | * |
26d254a3 NR |
295 | * <p> |
296 | * Example: "'Contact: |N@10|FN@20|NICK@+|PHOTO@x" | |
297 | * </p> | |
9c8baf0c NR |
298 | * |
299 | * @param format | |
300 | * the format to use | |
301 | * @param width | |
302 | * a fixed width or -1 for "as long as needed" | |
296a0b75 NR |
303 | * @param unicode |
304 | * allow Uniode or only ASCII characters | |
305 | * | |
9c8baf0c NR |
306 | * @return the {@link String} representation |
307 | */ | |
296a0b75 | 308 | public String[] toStringArray(String format, int width, boolean unicode) { |
9c8baf0c NR |
309 | List<String> str = new LinkedList<String>(); |
310 | ||
311 | String[] formatFields = format.split("\\|"); | |
312 | String[] values = new String[formatFields.length]; | |
313 | Boolean[] expandedFields = new Boolean[formatFields.length]; | |
314 | Boolean[] fixedsizeFields = new Boolean[formatFields.length]; | |
315 | int numOfFieldsToExpand = 0; | |
316 | int totalSize = 0; | |
317 | ||
318 | if (width == 0) { | |
0b0b2b0f NR |
319 | for (int i = 0; i < formatFields.length; i++) { |
320 | str.add(""); | |
321 | } | |
bcb54330 | 322 | |
9c8baf0c NR |
323 | return str.toArray(new String[] {}); |
324 | } | |
325 | ||
a3b510ab NR |
326 | for (int i = 0; i < formatFields.length; i++) { |
327 | String field = formatFields[i]; | |
328 | ||
329 | int size = -1; | |
330 | boolean binary = false; | |
331 | boolean expand = false; | |
30a4aa17 NR |
332 | int fieldNum = -1; |
333 | int valueNum = -1; | |
a3b510ab | 334 | |
26d254a3 NR |
335 | if (field.length() > 0 && field.charAt(0) != '\'' |
336 | && field.contains("@")) { | |
a3b510ab NR |
337 | String[] opts = field.split("@"); |
338 | if (opts.length > 0) | |
339 | field = opts[0]; | |
340 | for (int io = 1; io < opts.length; io++) { | |
341 | String opt = opts[io]; | |
342 | if (opt.equals("x")) { | |
343 | binary = true; | |
344 | } else if (opt.equals("+")) { | |
345 | expand = true; | |
346 | numOfFieldsToExpand++; | |
30a4aa17 NR |
347 | } else if (opt.length() > 0 && opt.charAt(0) == '(') { |
348 | try { | |
349 | opt = opt.substring(1, opt.length() - 1); | |
350 | valueNum = Integer.parseInt(opt); | |
351 | } catch (Exception e) { | |
352 | } | |
353 | } else if (opt.length() > 0 && opt.charAt(0) == '[') { | |
354 | try { | |
355 | opt = opt.substring(1, opt.length() - 1); | |
356 | fieldNum = Integer.parseInt(opt); | |
357 | } catch (Exception e) { | |
358 | } | |
a3b510ab NR |
359 | } else { |
360 | try { | |
361 | size = Integer.parseInt(opt); | |
30a4aa17 | 362 | } catch (NumberFormatException e) { |
a3b510ab NR |
363 | } |
364 | } | |
365 | } | |
366 | } | |
367 | ||
26d254a3 NR |
368 | String value = null; |
369 | if (field.length() > 0 && field.charAt(0) == '\'') { | |
370 | value = field.substring(1); | |
30a4aa17 NR |
371 | } else if (valueNum >= 0) { |
372 | List<String> vv = getPreferredData(field).getValues(); | |
373 | if (valueNum < vv.size()) { | |
374 | value = vv.get(valueNum); | |
375 | } | |
376 | } else if (fieldNum >= 0) { | |
377 | List<String> ff = getPreferredData(field).getFields(); | |
378 | if (fieldNum < ff.size()) { | |
379 | value = ff.get(fieldNum); | |
380 | } | |
26d254a3 | 381 | } else { |
59597d59 NR |
382 | // we don't need the *data* in binary mode... |
383 | if (binary) | |
384 | value = getData(field).size() > 0 ? "x" : null; | |
385 | else | |
386 | value = getPreferredDataValue(field); | |
26d254a3 NR |
387 | } |
388 | ||
296a0b75 | 389 | if (value == null) { |
a3b510ab | 390 | value = ""; |
296a0b75 NR |
391 | } else { |
392 | value = StringUtils.sanitize(value, unicode); | |
393 | } | |
a3b510ab NR |
394 | |
395 | if (size > -1) { | |
296a0b75 | 396 | value = StringUtils.padString(value, size); |
a3b510ab NR |
397 | } |
398 | ||
399 | expandedFields[i] = expand; | |
400 | fixedsizeFields[i] = (size > -1); | |
401 | ||
402 | if (binary) { | |
403 | if (value != null && !value.equals("")) | |
404 | values[i] = "x"; | |
405 | else | |
406 | values[i] = " "; | |
407 | totalSize++; | |
408 | } else { | |
409 | values[i] = value; | |
410 | totalSize += value.length(); | |
411 | } | |
412 | } | |
9c8baf0c | 413 | |
a3b510ab NR |
414 | if (width > -1 && totalSize > width) { |
415 | int toDo = totalSize - width; | |
416 | for (int i = fixedsizeFields.length - 1; toDo > 0 && i >= 0; i--) { | |
417 | if (!fixedsizeFields[i]) { | |
418 | int valueLength = values[i].length(); | |
419 | if (valueLength > 0) { | |
420 | if (valueLength >= toDo) { | |
421 | values[i] = values[i].substring(0, valueLength | |
422 | - toDo); | |
423 | toDo = 0; | |
424 | } else { | |
425 | values[i] = ""; | |
426 | toDo -= valueLength; | |
427 | } | |
428 | } | |
429 | } | |
430 | } | |
431 | ||
432 | totalSize = width + toDo; | |
433 | } | |
9c8baf0c | 434 | |
a3b510ab NR |
435 | if (width > -1 && numOfFieldsToExpand > 0) { |
436 | int availablePadding = width - totalSize; | |
437 | ||
438 | if (availablePadding > 0) { | |
439 | int padPerItem = availablePadding / numOfFieldsToExpand; | |
440 | int remainder = availablePadding % numOfFieldsToExpand; | |
441 | ||
442 | for (int i = 0; i < values.length; i++) { | |
443 | if (expandedFields[i]) { | |
444 | if (remainder > 0) { | |
296a0b75 NR |
445 | values[i] = values[i] |
446 | + StringUtils.padString("", remainder); | |
a3b510ab NR |
447 | remainder = 0; |
448 | } | |
449 | if (padPerItem > 0) { | |
296a0b75 NR |
450 | values[i] = values[i] |
451 | + StringUtils.padString("", padPerItem); | |
a3b510ab NR |
452 | } |
453 | } | |
454 | } | |
455 | ||
456 | totalSize = width; | |
457 | } | |
458 | } | |
a3b510ab | 459 | |
9c8baf0c NR |
460 | int currentSize = 0; |
461 | for (int i = 0; i < values.length; i++) { | |
462 | currentSize += addToList(str, values[i], currentSize, width); | |
a3b510ab NR |
463 | } |
464 | ||
9c8baf0c | 465 | return str.toArray(new String[] {}); |
a3b510ab NR |
466 | } |
467 | ||
a3b510ab NR |
468 | /** |
469 | * Update the information from this contact with the information in the | |
470 | * given contact. Non present fields will be removed, new fields will be | |
471 | * added, BKey'ed fields will be completed with the binary information known | |
472 | * by this contact. | |
473 | * | |
474 | * @param vc | |
475 | * the contact with the newer information and optional BKeys | |
476 | */ | |
477 | public void updateFrom(Contact vc) { | |
478 | updateBKeys(false); | |
479 | ||
26d2bd05 | 480 | List<Data> newDatas = new LinkedList<Data>(vc); |
a3b510ab NR |
481 | for (int i = 0; i < newDatas.size(); i++) { |
482 | Data data = newDatas.get(i); | |
483 | int bkey = Parser.getBKey(data); | |
484 | if (bkey >= 0) { | |
485 | if (binaries.containsKey(bkey)) { | |
486 | newDatas.set(i, binaries.get(bkey)); | |
487 | } | |
488 | } | |
489 | } | |
490 | ||
26d2bd05 | 491 | replaceListContent(newDatas); |
a3b510ab | 492 | this.nextBKey = vc.nextBKey; |
78e4af97 NR |
493 | } |
494 | ||
e253bd50 NR |
495 | @Override |
496 | public String getId() { | |
497 | return "" + getPreferredDataValue("UID"); | |
498 | } | |
499 | ||
500 | @Override | |
501 | public String getState() { | |
e4444b0b | 502 | return getId(); |
e253bd50 NR |
503 | } |
504 | ||
78e4af97 NR |
505 | /** |
506 | * Return a {@link String} representation of this contact, in vCard 2.1, | |
507 | * without BKeys. | |
508 | * | |
509 | * @return the {@link String} representation | |
510 | */ | |
511 | @Override | |
512 | public String toString() { | |
59597d59 | 513 | return "[Contact: " + getPreferredDataValue("FN") + "]"; |
78e4af97 NR |
514 | } |
515 | ||
a3b510ab NR |
516 | /** |
517 | * Mark all the binary fields with a BKey number. | |
518 | * | |
519 | * @param force | |
520 | * force the marking, and reset all the numbers. | |
521 | */ | |
522 | protected void updateBKeys(boolean force) { | |
523 | if (force) { | |
524 | binaries = new HashMap<Integer, Data>(); | |
525 | nextBKey = 1; | |
526 | } | |
527 | ||
528 | if (binaries == null) { | |
529 | binaries = new HashMap<Integer, Data>(); | |
530 | } | |
531 | ||
26d2bd05 | 532 | for (Data data : this) { |
a3b510ab NR |
533 | if (data.isBinary() && (data.getB64Key() <= 0 || force)) { |
534 | binaries.put(nextBKey, data); | |
535 | data.resetB64Key(nextBKey++); | |
536 | } | |
537 | } | |
538 | } | |
539 | ||
a3b510ab | 540 | /** |
26d2bd05 NR |
541 | * Load the data from the given {@link File} under the given {@link Format}. |
542 | * | |
543 | * @param file | |
544 | * the {@link File} to load from | |
545 | * @param format | |
546 | * the {@link Format} to load as | |
547 | * | |
548 | * @return the list of elements | |
549 | * @throws IOException | |
550 | * in case of IO error | |
a3b510ab | 551 | */ |
26d2bd05 NR |
552 | static private List<Data> load(List<Data> content) { |
553 | List<Data> datas = new ArrayList<Data>(); | |
a3b510ab | 554 | |
26d2bd05 NR |
555 | boolean fn = false; |
556 | boolean n = false; | |
e253bd50 | 557 | boolean uid = false; |
26d2bd05 NR |
558 | if (content != null) { |
559 | for (Data data : content) { | |
560 | if (data.getName().equals("N")) { | |
561 | n = true; | |
562 | } else if (data.getName().equals("FN")) { | |
563 | fn = true; | |
e253bd50 NR |
564 | } else if (data.getName().equals("UID")) { |
565 | uid = true; | |
26d2bd05 NR |
566 | } |
567 | ||
568 | if (!data.getName().equals("VERSION")) { | |
569 | datas.add(data); | |
570 | } | |
571 | } | |
78e4af97 | 572 | } |
78e4af97 | 573 | |
26d2bd05 | 574 | // required fields: |
e253bd50 | 575 | if (!n) // required since vCard 3.0, supported in 2.1 |
26d2bd05 | 576 | datas.add(new Data(null, "N", "", null)); |
e253bd50 | 577 | if (!fn) // not required anymore but still supported in 4.0 |
26d2bd05 | 578 | datas.add(new Data(null, "FN", "", null)); |
e253bd50 NR |
579 | if (!uid) // supported by vCard, required by this program |
580 | datas.add(new Data(null, "UID", UUID.randomUUID().toString(), null)); | |
26d2bd05 NR |
581 | |
582 | return datas; | |
a3b510ab | 583 | } |
296a0b75 | 584 | |
bcb54330 | 585 | /** |
78e4af97 NR |
586 | * Add a {@link String} to the given {@link List}, but make sure it does not |
587 | * exceed the maximum size, and truncate it if needed to fit. | |
bcb54330 | 588 | * |
78e4af97 NR |
589 | * @param list |
590 | * @param add | |
591 | * @param currentSize | |
592 | * @param maxSize | |
593 | * @return | |
bcb54330 | 594 | */ |
78e4af97 NR |
595 | static private int addToList(List<String> list, String add, |
596 | int currentSize, int maxSize) { | |
597 | if (add == null || add.length() == 0) { | |
598 | if (add != null) | |
599 | list.add(add); | |
600 | return 0; | |
601 | } | |
602 | ||
603 | if (maxSize > -1) { | |
604 | if (currentSize < maxSize) { | |
605 | if (currentSize + add.length() >= maxSize) { | |
606 | add = add.substring(0, maxSize - currentSize); | |
bcb54330 | 607 | } |
78e4af97 NR |
608 | } else { |
609 | add = ""; | |
bcb54330 NR |
610 | } |
611 | } | |
612 | ||
78e4af97 NR |
613 | list.add(add); |
614 | return add.length(); | |
bcb54330 | 615 | } |
a3b510ab | 616 | } |