| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | |
|
| 23 | |
package org.webmacro.servlet; |
| 24 | |
|
| 25 | |
import java.util.ArrayList; |
| 26 | |
import java.util.Arrays; |
| 27 | |
import java.util.Enumeration; |
| 28 | |
import java.util.Iterator; |
| 29 | |
import java.util.List; |
| 30 | |
import java.util.ListIterator; |
| 31 | |
import java.util.StringTokenizer; |
| 32 | |
|
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
public final class ListUtil |
| 45 | |
{ |
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
private ListUtil () |
| 51 | 2 | { |
| 52 | 2 | } |
| 53 | |
|
| 54 | 2 | private static ListUtil _singleton = new ListUtil(); |
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 59 | |
public static ListUtil getInstance () |
| 60 | |
{ |
| 61 | 0 | return _singleton; |
| 62 | |
} |
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
public boolean isList (Object o) |
| 68 | |
{ |
| 69 | 0 | if (o == null) return false; |
| 70 | 0 | return o instanceof List; |
| 71 | |
} |
| 72 | |
|
| 73 | |
|
| 74 | |
|
| 75 | |
|
| 76 | |
public boolean isArray (Object o) |
| 77 | |
{ |
| 78 | 0 | if (o == null) return false; |
| 79 | 0 | return o.getClass().isArray(); |
| 80 | |
} |
| 81 | |
|
| 82 | |
|
| 83 | |
|
| 84 | |
|
| 85 | |
|
| 86 | |
public static boolean isEmpty (Object arg) |
| 87 | |
{ |
| 88 | 0 | if (arg == null) return true; |
| 89 | 0 | if (arg instanceof List) return ((List) arg).isEmpty(); |
| 90 | 0 | if (arg instanceof Object[]) return ((Object[]) arg).length == 0; |
| 91 | 0 | if (arg instanceof Iterator) return ((Iterator) arg).hasNext(); |
| 92 | 0 | if (arg instanceof Enumeration) |
| 93 | 0 | return ((Enumeration) arg).hasMoreElements(); |
| 94 | |
|
| 95 | 0 | if (arg.getClass().isArray()) |
| 96 | |
{ |
| 97 | 0 | return java.lang.reflect.Array.getLength(arg) == 0; |
| 98 | |
} |
| 99 | 0 | return true; |
| 100 | |
} |
| 101 | |
|
| 102 | |
|
| 103 | |
|
| 104 | |
|
| 105 | |
|
| 106 | |
|
| 107 | |
|
| 108 | |
|
| 109 | |
|
| 110 | |
|
| 111 | |
|
| 112 | |
|
| 113 | |
|
| 114 | |
|
| 115 | |
|
| 116 | |
|
| 117 | |
|
| 118 | |
|
| 119 | |
|
| 120 | |
|
| 121 | |
public static List toList (Object arg) |
| 122 | |
{ |
| 123 | 0 | List list = null; |
| 124 | 0 | if (arg instanceof List) |
| 125 | |
{ |
| 126 | 0 | list = (List) arg; |
| 127 | |
} |
| 128 | 0 | else if (arg == null) |
| 129 | |
{ |
| 130 | 0 | list = Arrays.asList(new Object[0]); |
| 131 | |
} |
| 132 | 0 | else if (arg instanceof Object[]) |
| 133 | |
{ |
| 134 | 0 | list = Arrays.asList((Object[]) arg); |
| 135 | |
} |
| 136 | 0 | else if (arg instanceof Iterator) |
| 137 | |
{ |
| 138 | 0 | list = iteratorToList((Iterator) arg); |
| 139 | |
} |
| 140 | 0 | else if (arg instanceof Enumeration) |
| 141 | |
{ |
| 142 | 0 | list = iteratorToList(new org.webmacro.util.EnumIterator((Enumeration) arg)); |
| 143 | |
} |
| 144 | 0 | else if (arg.getClass().isArray()) |
| 145 | |
{ |
| 146 | |
|
| 147 | 0 | list = iteratorToList(new org.webmacro.util.PrimitiveArrayIterator(arg)); |
| 148 | |
} |
| 149 | |
else |
| 150 | |
{ |
| 151 | |
|
| 152 | 0 | Object[] oa = {arg}; |
| 153 | 0 | list = Arrays.asList(oa); |
| 154 | |
} |
| 155 | 0 | return list; |
| 156 | |
} |
| 157 | |
|
| 158 | |
static private List iteratorToList (Iterator iter) |
| 159 | |
{ |
| 160 | 0 | List list = new ArrayList(); |
| 161 | 0 | while (iter.hasNext()) |
| 162 | |
{ |
| 163 | 0 | list.add(iter.next()); |
| 164 | |
} |
| 165 | |
|
| 166 | 0 | if (iter instanceof ListIterator) |
| 167 | |
{ |
| 168 | 0 | ListIterator li = (ListIterator) iter; |
| 169 | 0 | while (li.hasPrevious()) li.previous(); |
| 170 | |
} |
| 171 | 0 | return list; |
| 172 | |
} |
| 173 | |
|
| 174 | |
|
| 175 | |
|
| 176 | |
|
| 177 | |
public static Object getItem (Object[] oa, int pos) |
| 178 | |
{ |
| 179 | 0 | if ((pos < 0) || ((pos - 1) > oa.length)) |
| 180 | 0 | throw new IndexOutOfBoundsException( |
| 181 | |
"Index must be between 0 and " + (oa.length - 1) |
| 182 | |
+ ", user specified " + pos); |
| 183 | 0 | return oa[pos]; |
| 184 | |
} |
| 185 | |
|
| 186 | |
|
| 187 | |
|
| 188 | |
|
| 189 | |
public static Object getItem (List list, int pos) |
| 190 | |
{ |
| 191 | 0 | if (pos < 0 || (pos + 1) > list.size()) |
| 192 | 0 | throw new IndexOutOfBoundsException( |
| 193 | |
"Index must be between 0 and " + (list.size() - 1) |
| 194 | |
+ ", user specified " + pos); |
| 195 | 0 | return list.get(pos); |
| 196 | |
} |
| 197 | |
|
| 198 | |
|
| 199 | |
|
| 200 | |
|
| 201 | |
|
| 202 | |
|
| 203 | |
|
| 204 | |
|
| 205 | |
public static Object getItem (Object arr, int pos) |
| 206 | |
{ |
| 207 | 0 | if (!arr.getClass().isArray()) |
| 208 | 0 | throw new IllegalArgumentException( |
| 209 | |
"The first argument must be of List or array type."); |
| 210 | 0 | return java.lang.reflect.Array.get(arr, pos); |
| 211 | |
} |
| 212 | |
|
| 213 | |
|
| 214 | |
|
| 215 | |
|
| 216 | |
public static int size (Object[] oa) |
| 217 | |
{ |
| 218 | 0 | return oa.length; |
| 219 | |
} |
| 220 | |
|
| 221 | |
|
| 222 | |
|
| 223 | |
|
| 224 | |
public static int size (List list) |
| 225 | |
{ |
| 226 | 0 | return list.size(); |
| 227 | |
} |
| 228 | |
|
| 229 | |
public static int size (Object arr) |
| 230 | |
{ |
| 231 | 0 | if (!arr.getClass().isArray()) |
| 232 | 0 | throw new IllegalArgumentException( |
| 233 | |
"The argument must be of List or array type."); |
| 234 | 0 | return java.lang.reflect.Array.getLength(arr); |
| 235 | |
} |
| 236 | |
|
| 237 | |
|
| 238 | |
|
| 239 | |
|
| 240 | |
public static boolean contains (List list, Object o) |
| 241 | |
{ |
| 242 | 0 | return list.contains(o); |
| 243 | |
} |
| 244 | |
|
| 245 | |
|
| 246 | |
|
| 247 | |
|
| 248 | |
public static boolean contains (Object[] oa, Object o) |
| 249 | |
{ |
| 250 | 0 | return contains(Arrays.asList(oa), o); |
| 251 | |
} |
| 252 | |
|
| 253 | |
|
| 254 | |
|
| 255 | |
|
| 256 | |
public static boolean contains (Object arr, Object o) |
| 257 | |
{ |
| 258 | 0 | if (!arr.getClass().isArray()) |
| 259 | 0 | throw new IllegalArgumentException( |
| 260 | |
"The argument must be of List or array type."); |
| 261 | 0 | for (int i = 0; i < java.lang.reflect.Array.getLength(arr); i++) |
| 262 | |
{ |
| 263 | 0 | if (o.equals(java.lang.reflect.Array.get(arr, i))) return true; |
| 264 | |
} |
| 265 | 0 | return false; |
| 266 | |
} |
| 267 | |
|
| 268 | |
|
| 269 | |
|
| 270 | |
|
| 271 | |
|
| 272 | |
|
| 273 | |
|
| 274 | |
|
| 275 | |
public static List split (List arg, int colCount) |
| 276 | |
{ |
| 277 | |
|
| 278 | 0 | return split(arg, colCount, true, null); |
| 279 | |
} |
| 280 | |
|
| 281 | |
|
| 282 | |
|
| 283 | |
|
| 284 | |
|
| 285 | |
|
| 286 | |
|
| 287 | |
|
| 288 | |
|
| 289 | |
|
| 290 | |
public static List split (List arg, int colCount, boolean pad) |
| 291 | |
{ |
| 292 | |
|
| 293 | 0 | return split(arg, colCount, pad, null); |
| 294 | |
} |
| 295 | |
|
| 296 | |
|
| 297 | |
|
| 298 | |
|
| 299 | |
|
| 300 | |
|
| 301 | |
|
| 302 | |
|
| 303 | |
|
| 304 | |
public static List split (List arg, int colCount, Object padValue) |
| 305 | |
{ |
| 306 | |
|
| 307 | 0 | return split(arg, colCount, true, padValue); |
| 308 | |
} |
| 309 | |
|
| 310 | |
|
| 311 | |
|
| 312 | |
|
| 313 | |
|
| 314 | |
|
| 315 | |
|
| 316 | |
|
| 317 | |
|
| 318 | |
|
| 319 | |
|
| 320 | |
public static List split (List arg, int colCount, boolean pad, |
| 321 | |
Object padValue) |
| 322 | |
{ |
| 323 | |
|
| 324 | 0 | int size = arg.size(); |
| 325 | 0 | List rows = new ArrayList(size / colCount + 1); |
| 326 | 0 | int start = 0; |
| 327 | 0 | int end = colCount; |
| 328 | 0 | while (start < size) |
| 329 | |
{ |
| 330 | |
List row; |
| 331 | |
|
| 332 | 0 | if (end > size) |
| 333 | |
{ |
| 334 | |
|
| 335 | 0 | row = new ArrayList(arg.subList(start, size)); |
| 336 | 0 | if (pad) |
| 337 | |
{ |
| 338 | 0 | for (int i = size; i < end; ++i) |
| 339 | |
{ |
| 340 | 0 | row.add(padValue); |
| 341 | |
} |
| 342 | |
} |
| 343 | |
} |
| 344 | |
else |
| 345 | |
{ |
| 346 | 0 | row = new ArrayList(arg.subList(start, end)); |
| 347 | |
} |
| 348 | 0 | rows.add(row); |
| 349 | 0 | start = end; |
| 350 | 0 | end += colCount; |
| 351 | 0 | } |
| 352 | 0 | return rows; |
| 353 | |
} |
| 354 | |
|
| 355 | |
|
| 356 | |
|
| 357 | |
|
| 358 | |
|
| 359 | |
|
| 360 | |
|
| 361 | |
|
| 362 | |
public static Object[] split (Object[] arg, int colCount) |
| 363 | |
{ |
| 364 | |
|
| 365 | 0 | return split(arg, colCount, true, null); |
| 366 | |
} |
| 367 | |
|
| 368 | |
|
| 369 | |
|
| 370 | |
|
| 371 | |
|
| 372 | |
|
| 373 | |
|
| 374 | |
|
| 375 | |
|
| 376 | |
public static Object[] split (Object[] arg, int colCount, boolean pad) |
| 377 | |
{ |
| 378 | |
|
| 379 | 0 | return split(arg, colCount, pad, null); |
| 380 | |
} |
| 381 | |
|
| 382 | |
|
| 383 | |
|
| 384 | |
|
| 385 | |
|
| 386 | |
|
| 387 | |
|
| 388 | |
|
| 389 | |
public static Object[] split (Object[] arg, int colCount, Object padValue) |
| 390 | |
{ |
| 391 | |
|
| 392 | 0 | return split(arg, colCount, true, padValue); |
| 393 | |
} |
| 394 | |
|
| 395 | |
|
| 396 | |
|
| 397 | |
|
| 398 | |
|
| 399 | |
|
| 400 | |
|
| 401 | |
|
| 402 | |
|
| 403 | |
|
| 404 | |
public static Object[][] split (Object[] arg, int colCount, boolean pad, |
| 405 | |
Object padValue) |
| 406 | |
{ |
| 407 | |
|
| 408 | 0 | int size = arg.length; |
| 409 | 0 | int rowCount = size / colCount; |
| 410 | 0 | if ((size % colCount) != 0) |
| 411 | |
{ |
| 412 | 0 | ++rowCount; |
| 413 | |
} |
| 414 | 0 | Object[][] rows = new Object[rowCount][]; |
| 415 | 0 | int start = 0; |
| 416 | 0 | int end = colCount; |
| 417 | 0 | for (int rowNo = 0; rowNo < rowCount; ++rowNo) |
| 418 | |
{ |
| 419 | |
Object[] row; |
| 420 | |
|
| 421 | 0 | if (end > size) |
| 422 | |
{ |
| 423 | 0 | int tail = size - start; |
| 424 | 0 | if (pad) |
| 425 | |
{ |
| 426 | 0 | row = new Object[colCount]; |
| 427 | 0 | System.arraycopy(arg, start, row, 0, tail); |
| 428 | 0 | if (padValue != null) |
| 429 | |
{ |
| 430 | 0 | for (int i = tail; i < end; ++i) |
| 431 | |
{ |
| 432 | 0 | row[i] = padValue; |
| 433 | |
} |
| 434 | |
} |
| 435 | |
} |
| 436 | |
else |
| 437 | |
{ |
| 438 | 0 | row = new Object[tail]; |
| 439 | 0 | System.arraycopy(arg, start, row, 0, tail); |
| 440 | |
} |
| 441 | 0 | } |
| 442 | |
else |
| 443 | |
{ |
| 444 | 0 | row = new Object[colCount]; |
| 445 | 0 | System.arraycopy(arg, start, row, 0, colCount); |
| 446 | |
} |
| 447 | 0 | rows[rowNo] = row; |
| 448 | 0 | start = end; |
| 449 | 0 | end += colCount; |
| 450 | |
} |
| 451 | 0 | return rows; |
| 452 | |
} |
| 453 | |
|
| 454 | |
|
| 455 | |
|
| 456 | |
|
| 457 | |
|
| 458 | |
|
| 459 | |
|
| 460 | |
|
| 461 | |
public static Object[][] transposeSplit (Object[] arg, int colCount) |
| 462 | |
{ |
| 463 | |
|
| 464 | 0 | return transposeSplit(arg, colCount, true, null); |
| 465 | |
} |
| 466 | |
|
| 467 | |
|
| 468 | |
|
| 469 | |
|
| 470 | |
|
| 471 | |
|
| 472 | |
|
| 473 | |
|
| 474 | |
|
| 475 | |
public static Object[][] transposeSplit (Object[] arg, int colCount, |
| 476 | |
boolean pad) |
| 477 | |
{ |
| 478 | |
|
| 479 | 0 | return transposeSplit(arg, colCount, pad, null); |
| 480 | |
} |
| 481 | |
|
| 482 | |
|
| 483 | |
|
| 484 | |
|
| 485 | |
|
| 486 | |
|
| 487 | |
|
| 488 | |
|
| 489 | |
|
| 490 | |
public static Object[][] transposeSplit (Object[] arg, int colCount, |
| 491 | |
Object padValue) |
| 492 | |
{ |
| 493 | |
|
| 494 | 0 | return transposeSplit(arg, colCount, true, padValue); |
| 495 | |
} |
| 496 | |
|
| 497 | |
|
| 498 | |
|
| 499 | |
|
| 500 | |
|
| 501 | |
|
| 502 | |
|
| 503 | |
|
| 504 | |
|
| 505 | |
|
| 506 | |
|
| 507 | |
public static Object[][] transposeSplit (Object[] arg, int colCount, |
| 508 | |
boolean pad, Object padValue) |
| 509 | |
{ |
| 510 | 0 | int size = arg.length; |
| 511 | 0 | int rowCount = size / colCount; |
| 512 | |
Object[][] rows; |
| 513 | 0 | boolean slack = (size % colCount) != 0; |
| 514 | 0 | if (slack) |
| 515 | |
{ |
| 516 | 0 | ++rowCount; |
| 517 | |
} |
| 518 | 0 | if (slack && !pad) |
| 519 | |
{ |
| 520 | 0 | int tail = size % rowCount; |
| 521 | 0 | rows = new Object[rowCount][]; |
| 522 | 0 | for (int rowNo = 0; rowNo < rowCount; ++rowNo) |
| 523 | |
{ |
| 524 | 0 | if (rowNo < tail) |
| 525 | |
{ |
| 526 | 0 | rows[rowNo] = new Object[colCount]; |
| 527 | |
} |
| 528 | |
else |
| 529 | |
{ |
| 530 | 0 | rows[rowNo] = new Object[colCount - 1]; |
| 531 | |
} |
| 532 | |
} |
| 533 | 0 | } |
| 534 | |
else |
| 535 | |
{ |
| 536 | 0 | rows = new Object[rowCount][colCount]; |
| 537 | |
} |
| 538 | |
|
| 539 | 0 | int pos = 0; |
| 540 | 0 | for (int colNo = 0; colNo < colCount; ++colNo) |
| 541 | |
{ |
| 542 | 0 | for (int rowNo = 0; rowNo < rowCount; ++rowNo) |
| 543 | |
{ |
| 544 | 0 | if (pos < size) |
| 545 | |
{ |
| 546 | 0 | rows[rowNo][colNo] = arg[pos++]; |
| 547 | |
} |
| 548 | 0 | else if (pad) |
| 549 | |
{ |
| 550 | 0 | rows[rowNo][colNo] = padValue; |
| 551 | |
} |
| 552 | |
else |
| 553 | |
{ |
| 554 | |
break; |
| 555 | |
} |
| 556 | |
} |
| 557 | |
} |
| 558 | 0 | return rows; |
| 559 | |
} |
| 560 | |
|
| 561 | |
|
| 562 | |
|
| 563 | |
|
| 564 | |
|
| 565 | |
|
| 566 | |
|
| 567 | |
|
| 568 | |
public static List transposeSplit (List arg, int colCount) |
| 569 | |
{ |
| 570 | |
|
| 571 | 0 | return transposeSplit(arg, colCount, true, null); |
| 572 | |
} |
| 573 | |
|
| 574 | |
|
| 575 | |
|
| 576 | |
|
| 577 | |
|
| 578 | |
|
| 579 | |
|
| 580 | |
|
| 581 | |
|
| 582 | |
public static List transposeSplit (List arg, int colCount, |
| 583 | |
boolean pad) |
| 584 | |
{ |
| 585 | |
|
| 586 | 0 | return transposeSplit(arg, colCount, pad, null); |
| 587 | |
} |
| 588 | |
|
| 589 | |
|
| 590 | |
|
| 591 | |
|
| 592 | |
|
| 593 | |
|
| 594 | |
|
| 595 | |
|
| 596 | |
|
| 597 | |
public static List transposeSplit (List arg, int colCount, |
| 598 | |
Object padValue) |
| 599 | |
{ |
| 600 | |
|
| 601 | 0 | return transposeSplit(arg, colCount, true, padValue); |
| 602 | |
} |
| 603 | |
|
| 604 | |
|
| 605 | |
|
| 606 | |
|
| 607 | |
|
| 608 | |
|
| 609 | |
|
| 610 | |
|
| 611 | |
|
| 612 | |
|
| 613 | |
public static List transposeSplit (List arg, int colCount, |
| 614 | |
boolean pad, Object padValue) |
| 615 | |
{ |
| 616 | |
|
| 617 | 0 | int size = arg.size(); |
| 618 | 0 | int rowCount = size / colCount; |
| 619 | 0 | if ((size % colCount) != 0) |
| 620 | |
{ |
| 621 | 0 | ++rowCount; |
| 622 | |
} |
| 623 | 0 | List rows = new ArrayList(rowCount); |
| 624 | 0 | for (int rowNo = 0; rowNo < rowCount; ++rowNo) |
| 625 | |
{ |
| 626 | 0 | rows.add(new ArrayList(colCount)); |
| 627 | |
} |
| 628 | 0 | Iterator it = arg.iterator(); |
| 629 | 0 | for (int colNo = 0; colNo < colCount; ++colNo) |
| 630 | |
{ |
| 631 | 0 | for (int rowNo = 0; rowNo < rowCount; ++rowNo) |
| 632 | |
{ |
| 633 | 0 | List row = (List) rows.get(rowNo); |
| 634 | 0 | if (it.hasNext()) |
| 635 | |
{ |
| 636 | 0 | row.add(it.next()); |
| 637 | |
} |
| 638 | 0 | else if (pad) |
| 639 | |
{ |
| 640 | 0 | row.add(padValue); |
| 641 | |
} |
| 642 | |
else |
| 643 | |
{ |
| 644 | |
break; |
| 645 | |
} |
| 646 | |
} |
| 647 | |
} |
| 648 | 0 | return rows; |
| 649 | |
} |
| 650 | |
|
| 651 | |
public static List createRange (int rangeBegin, int rangeEnd) |
| 652 | |
{ |
| 653 | 0 | return createRange(rangeBegin, rangeEnd, 1); |
| 654 | |
} |
| 655 | |
|
| 656 | |
public static List createRange (int rangeBegin, int rangeEnd, int incr) |
| 657 | |
{ |
| 658 | 0 | if (incr > 0) |
| 659 | |
{ |
| 660 | 0 | if (rangeBegin > rangeEnd) |
| 661 | 0 | throw new IllegalArgumentException("Starting number must be less than ending number"); |
| 662 | |
} |
| 663 | 0 | else if (incr < 0) |
| 664 | |
{ |
| 665 | 0 | if (rangeBegin < rangeEnd) |
| 666 | 0 | throw new IllegalArgumentException("Starting number must be greater than ending number"); |
| 667 | |
} |
| 668 | |
else |
| 669 | |
{ |
| 670 | 0 | throw new IllegalArgumentException("Increment cannot be zero"); |
| 671 | |
} |
| 672 | |
|
| 673 | 0 | int size = ((rangeEnd - rangeBegin) / incr) + 1; |
| 674 | 0 | Integer[] ia = new Integer[size]; |
| 675 | 0 | int i = 0; |
| 676 | 0 | for (int num = rangeBegin; (incr > 0) ? num <= rangeEnd : num >= rangeEnd; num += incr) |
| 677 | |
{ |
| 678 | 0 | ia[i++] = new Integer(num); |
| 679 | |
} |
| 680 | 0 | return Arrays.asList(ia); |
| 681 | |
} |
| 682 | |
|
| 683 | |
|
| 684 | |
public static ArrayList create () |
| 685 | |
{ |
| 686 | 0 | return new ArrayList(); |
| 687 | |
} |
| 688 | |
|
| 689 | |
|
| 690 | |
public static ArrayList create (int capacity) |
| 691 | |
{ |
| 692 | 0 | return new ArrayList(capacity); |
| 693 | |
} |
| 694 | |
|
| 695 | |
|
| 696 | |
|
| 697 | |
|
| 698 | |
|
| 699 | |
|
| 700 | |
public static List append (Object o1, Object o2) |
| 701 | |
{ |
| 702 | 0 | List l1 = toList(o1); |
| 703 | 0 | List l2 = toList(o2); |
| 704 | |
try |
| 705 | |
{ |
| 706 | 0 | l1.addAll(l2); |
| 707 | 0 | return l1; |
| 708 | |
} |
| 709 | 0 | catch (Exception e) |
| 710 | |
{ |
| 711 | |
|
| 712 | 0 | List l = new ArrayList(((l1.size() + l2.size()) * 2) + 10); |
| 713 | 0 | l.addAll(l1); |
| 714 | 0 | l.addAll(l2); |
| 715 | 0 | return l; |
| 716 | |
} |
| 717 | |
} |
| 718 | |
|
| 719 | |
|
| 720 | |
public static List copy (Object o) |
| 721 | |
{ |
| 722 | 0 | List l = toList(o); |
| 723 | 0 | if (l.isEmpty()) return new ArrayList(10); |
| 724 | 0 | return new ArrayList(l); |
| 725 | |
} |
| 726 | |
|
| 727 | |
|
| 728 | |
public static void main (String[] args) |
| 729 | |
{ |
| 730 | 0 | java.io.PrintWriter out = |
| 731 | |
new java.io.PrintWriter(System.out, true); |
| 732 | 0 | ListUtil lu = ListUtil.getInstance(); |
| 733 | |
|
| 734 | 0 | out.println("createRange(2, 10, 2): " + createRange(2, 10, 2)); |
| 735 | 0 | out.println("createRange(-10, 0): " + createRange(-10, 0)); |
| 736 | 0 | out.println("createRange(21, 10, -5): " + createRange(21, 10, -5)); |
| 737 | 0 | out.println("createRange(21, 21, -5): " + createRange(21, 21, -5)); |
| 738 | |
|
| 739 | 0 | Object[] arr = { |
| 740 | |
"ant", "bird", "cat", "dog", "elephant", "ferret", "gopher" |
| 741 | |
}; |
| 742 | 0 | ArrayList l = new ArrayList(Arrays.asList(arr)); |
| 743 | |
|
| 744 | 0 | out.println("List/Array results"); |
| 745 | 0 | out.print("toList(): "); |
| 746 | 0 | out.println(ListUtil.toList(l) + "/" + ListUtil.toList(arr)); |
| 747 | 0 | out.print("size: "); |
| 748 | 0 | out.println(ListUtil.size(l) + "/" + ListUtil.size(arr)); |
| 749 | 0 | out.print("contains(\"bird\"): "); |
| 750 | 0 | out.println(ListUtil.contains(l, "bird") + "/" + ListUtil.contains(arr, "bird")); |
| 751 | 0 | out.print("contains(\"fish\"): "); |
| 752 | 0 | out.println(ListUtil.contains(l, "fish") + "/" + ListUtil.contains(arr, "fish")); |
| 753 | 0 | out.print("isArray: "); |
| 754 | 0 | out.println(lu.isArray(l) + "/" + lu.isArray(arr)); |
| 755 | 0 | out.print("isList: "); |
| 756 | 0 | out.println(lu.isList(l) + "/" + lu.isList(arr)); |
| 757 | 0 | out.print("getItem(5): "); |
| 758 | 0 | out.println(ListUtil.getItem(l, 5) + "/" + ListUtil.getItem(arr, 5)); |
| 759 | 0 | out.print("getItem(0): "); |
| 760 | |
try |
| 761 | |
{ |
| 762 | 0 | out.println(ListUtil.getItem(l, 0) + "/" + ListUtil.getItem(arr, 0)); |
| 763 | |
} |
| 764 | 0 | catch (Exception e) |
| 765 | |
{ |
| 766 | 0 | out.println(e); |
| 767 | 0 | } |
| 768 | 0 | out.println("toList(null): " + ListUtil.toList(null)); |
| 769 | 0 | out.println("toList(\"a string\"): " + ListUtil.toList("a string")); |
| 770 | |
|
| 771 | 0 | StringTokenizer st = new StringTokenizer( |
| 772 | |
"This is a bunch of words!"); |
| 773 | 0 | List l2 = ListUtil.toList(st); |
| 774 | 0 | out.println("toList(Enumeration): " + l2); |
| 775 | 0 | Iterator iter = l2.listIterator(); |
| 776 | 0 | List l3 = ListUtil.toList(iter); |
| 777 | 0 | out.println("toList(Iterator): " + l3 + ", iter.hasNext(): " + iter.hasNext()); |
| 778 | |
|
| 779 | 0 | out.println("List split with fill"); |
| 780 | 0 | List splitList1 = split(l, 3, true); |
| 781 | 0 | for (Iterator it1 = splitList1.iterator(); it1.hasNext(); ) |
| 782 | |
{ |
| 783 | 0 | out.print("-: "); |
| 784 | 0 | List part = (List) it1.next(); |
| 785 | 0 | for (Iterator it2 = part.iterator(); it2.hasNext(); ) |
| 786 | |
{ |
| 787 | 0 | out.print(it2.next() + ", "); |
| 788 | |
} |
| 789 | 0 | out.println("*"); |
| 790 | 0 | } |
| 791 | 0 | out.println("List transposeSplit"); |
| 792 | 0 | List splitList2 = transposeSplit(l, 3, false); |
| 793 | 0 | for (Iterator it1 = splitList2.iterator(); it1.hasNext(); ) |
| 794 | |
{ |
| 795 | 0 | out.print("-: "); |
| 796 | 0 | List part = (List) it1.next(); |
| 797 | 0 | for (Iterator it2 = part.iterator(); it2.hasNext(); ) |
| 798 | |
{ |
| 799 | 0 | out.print(it2.next() + ", "); |
| 800 | |
} |
| 801 | 0 | out.println("*"); |
| 802 | 0 | } |
| 803 | 0 | out.println("Array split"); |
| 804 | 0 | Object[] splitArray1 = split(new String[]{"pero"}, 2, false); |
| 805 | 0 | for (int i = 0; i < splitArray1.length; ++i) |
| 806 | |
{ |
| 807 | 0 | out.print("-: "); |
| 808 | 0 | Object[] part = (Object[]) splitArray1[i]; |
| 809 | 0 | for (int j = 0; j < part.length; ++j) |
| 810 | |
{ |
| 811 | 0 | out.print(part[j] + ", "); |
| 812 | |
} |
| 813 | 0 | out.println("*"); |
| 814 | |
} |
| 815 | 0 | out.println("Array transposeSplit"); |
| 816 | 0 | Object[][] splitArray3 = transposeSplit( |
| 817 | |
new String[]{ |
| 818 | |
"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", |
| 819 | |
"11", "12", "14", "15", "16", "17", "18", "19" |
| 820 | |
}, 3, false); |
| 821 | 0 | for (int i = 0; i < splitArray3.length; ++i) |
| 822 | |
{ |
| 823 | 0 | out.print("-: "); |
| 824 | 0 | for (int j = 0; j < splitArray3[i].length; ++j) |
| 825 | |
{ |
| 826 | 0 | out.print(splitArray3[i][j] + ", "); |
| 827 | |
} |
| 828 | 0 | out.println("*"); |
| 829 | |
} |
| 830 | |
|
| 831 | 0 | int[] emptyInts = new int[0]; |
| 832 | 0 | out.println("Empty array of int: isEmpty=" + isEmpty(emptyInts)); |
| 833 | 0 | char[] chars = {'A', 'B', 'C'}; |
| 834 | 0 | out.println("Array of char: isEmpty=" + isEmpty(chars) + ", size=" + size(chars)); |
| 835 | 0 | out.println("contains 'C'=" + contains(chars, new Character('C'))); |
| 836 | 0 | out.println("contains 'Z'=" + contains(chars, new Character('Z'))); |
| 837 | 0 | out.println("toList=" + toList(chars)); |
| 838 | 0 | float[] f = new float[]{1.1f, 2.2f, 3.3f}; |
| 839 | 0 | out.println("getItem(floats, 0)=" + getItem(f, 0)); |
| 840 | 0 | List appendList = append(f, chars); |
| 841 | 0 | out.println("append(f, chars)=" + appendList); |
| 842 | 0 | append(appendList, "another thing"); |
| 843 | 0 | out.println("append(appendList, \"another thing\")=" + appendList); |
| 844 | 0 | } |
| 845 | |
} |