001 package fj.data; 002 003 import fj.Effect; 004 import fj.F; 005 import fj.P1; 006 import static fj.data.List.asString; 007 import static fj.data.List.fromString; 008 009 /** 010 * Functions that convert between data structure types. 011 * 012 * @version %build.number%<br> 013 * <ul> 014 * <li>$LastChangedRevision: 122 $</li> 015 * <li>$LastChangedDate: 2009-04-24 17:24:38 -0500 (Fri, 24 Apr 2009) $</li> 016 * </ul> 017 */ 018 public final class Conversions { 019 private Conversions() { 020 throw new UnsupportedOperationException(); 021 } 022 023 // BEGIN List -> 024 025 /** 026 * A function that converts lists to arrays. 027 * 028 * @return A function that converts lists to arrays. 029 */ 030 public static <A> F<List<A>, Array<A>> List_Array() { 031 return new F<List<A>, Array<A>>() { 032 public Array<A> f(final List<A> as) { 033 return as.toArray(); 034 } 035 }; 036 } 037 038 /** 039 * A function that converts lists to streams. 040 * 041 * @return A function that converts lists to streams. 042 */ 043 public static <A> F<List<A>, Stream<A>> List_Stream() { 044 return new F<List<A>, Stream<A>>() { 045 public Stream<A> f(final List<A> as) { 046 return as.toStream(); 047 } 048 }; 049 } 050 051 /** 052 * A function that converts lists to options. 053 * 054 * @return A function that converts lists to options. 055 */ 056 public static <A> F<List<A>, Option<A>> List_Option() { 057 return new F<List<A>, Option<A>>() { 058 public Option<A> f(final List<A> as) { 059 return as.toOption(); 060 } 061 }; 062 } 063 064 /** 065 * A function that converts lists to eithers. 066 * 067 * @return A function that converts lists to eithers. 068 */ 069 public static <A, B> F<P1<A>, F<List<B>, Either<A, B>>> List_Either() { 070 return new F<P1<A>, F<List<B>, Either<A, B>>>() { 071 public F<List<B>, Either<A, B>> f(final P1<A> a) { 072 return new F<List<B>, Either<A, B>>() { 073 public Either<A, B> f(final List<B> bs) { 074 return bs.toEither(a); 075 } 076 }; 077 } 078 }; 079 } 080 081 /** 082 * A function that converts lists to strings. 083 */ 084 public static final F<List<Character>, String> List_String = new F<List<Character>, String>() { 085 public String f(final List<Character> cs) { 086 return asString(cs); 087 } 088 }; 089 090 /** 091 * A function that converts lists to string buffers. 092 */ 093 public static final F<List<Character>, StringBuffer> List_StringBuffer = new F<List<Character>, StringBuffer>() { 094 public StringBuffer f(final List<Character> cs) { 095 return new StringBuffer(asString(cs)); 096 } 097 }; 098 099 /** 100 * A function that converts lists to string builders. 101 */ 102 public static final F<List<Character>, StringBuilder> List_StringBuilder = new F<List<Character>, StringBuilder>() { 103 public StringBuilder f(final List<Character> cs) { 104 return new StringBuilder(asString(cs)); 105 } 106 }; 107 108 // END List -> 109 110 // BEGIN Array -> 111 112 /** 113 * A function that converts arrays to lists. 114 * 115 * @return A function that converts arrays to lists. 116 */ 117 public static <A> F<Array<A>, List<A>> Array_List() { 118 return new F<Array<A>, List<A>>() { 119 public List<A> f(final Array<A> as) { 120 return as.toList(); 121 } 122 }; 123 } 124 125 /** 126 * A function that converts arrays to streams. 127 * 128 * @return A function that converts arrays to streams. 129 */ 130 public static <A> F<Array<A>, Stream<A>> Array_Stream() { 131 return new F<Array<A>, Stream<A>>() { 132 public Stream<A> f(final Array<A> as) { 133 return as.toStream(); 134 } 135 }; 136 } 137 138 /** 139 * A function that converts arrays to options. 140 * 141 * @return A function that converts arrays to options. 142 */ 143 public static <A> F<Array<A>, Option<A>> Array_Option() { 144 return new F<Array<A>, Option<A>>() { 145 public Option<A> f(final Array<A> as) { 146 return as.toOption(); 147 } 148 }; 149 } 150 151 /** 152 * A function that converts arrays to eithers. 153 * 154 * @return A function that converts arrays to eithers. 155 */ 156 public static <A, B> F<P1<A>, F<Array<B>, Either<A, B>>> Array_Either() { 157 return new F<P1<A>, F<Array<B>, Either<A, B>>>() { 158 public F<Array<B>, Either<A, B>> f(final P1<A> a) { 159 return new F<Array<B>, Either<A, B>>() { 160 public Either<A, B> f(final Array<B> bs) { 161 return bs.toEither(a); 162 } 163 }; 164 } 165 }; 166 } 167 168 /** 169 * A function that converts arrays to strings. 170 */ 171 public static final F<Array<Character>, String> Array_String = new F<Array<Character>, String>() { 172 public String f(final Array<Character> cs) { 173 final StringBuilder sb = new StringBuilder(); 174 cs.foreach(new Effect<Character>() { 175 public void e(final Character c) { 176 sb.append(c); 177 } 178 }); 179 return sb.toString(); 180 } 181 }; 182 183 /** 184 * A function that converts arrays to string buffers. 185 */ 186 public static final F<Array<Character>, StringBuffer> Array_StringBuffer = new F<Array<Character>, StringBuffer>() { 187 public StringBuffer f(final Array<Character> cs) { 188 final StringBuffer sb = new StringBuffer(); 189 cs.foreach(new Effect<Character>() { 190 public void e(final Character c) { 191 sb.append(c); 192 } 193 }); 194 return sb; 195 } 196 }; 197 198 /** 199 * A function that converts arrays to string builders. 200 */ 201 public static final F<Array<Character>, StringBuilder> Array_StringBuilder = 202 new F<Array<Character>, StringBuilder>() { 203 public StringBuilder f(final Array<Character> cs) { 204 final StringBuilder sb = new StringBuilder(); 205 cs.foreach(new Effect<Character>() { 206 public void e(final Character c) { 207 sb.append(c); 208 } 209 }); 210 return sb; 211 } 212 }; 213 214 // END Array -> 215 216 // BEGIN Stream -> 217 218 /** 219 * A function that converts streams to lists. 220 * 221 * @return A function that converts streams to lists. 222 */ 223 public static <A> F<Stream<A>, List<A>> Stream_List() { 224 return new F<Stream<A>, List<A>>() { 225 public List<A> f(final Stream<A> as) { 226 return as.toList(); 227 } 228 }; 229 } 230 231 /** 232 * A function that converts streams to arrays. 233 * 234 * @return A function that converts streams to arrays. 235 */ 236 public static <A> F<Stream<A>, Array<A>> Stream_Array() { 237 return new F<Stream<A>, Array<A>>() { 238 public Array<A> f(final Stream<A> as) { 239 return as.toArray(); 240 } 241 }; 242 } 243 244 /** 245 * A function that converts streams to options. 246 * 247 * @return A function that converts streams to options. 248 */ 249 public static <A> F<Stream<A>, Option<A>> Stream_Option() { 250 return new F<Stream<A>, Option<A>>() { 251 public Option<A> f(final Stream<A> as) { 252 return as.toOption(); 253 } 254 }; 255 } 256 257 /** 258 * A function that converts streams to eithers. 259 * 260 * @return A function that converts streams to eithers. 261 */ 262 public static <A, B> F<P1<A>, F<Stream<B>, Either<A, B>>> Stream_Either() { 263 return new F<P1<A>, F<Stream<B>, Either<A, B>>>() { 264 public F<Stream<B>, Either<A, B>> f(final P1<A> a) { 265 return new F<Stream<B>, Either<A, B>>() { 266 public Either<A, B> f(final Stream<B> bs) { 267 return bs.toEither(a); 268 } 269 }; 270 } 271 }; 272 } 273 274 /** 275 * A function that converts streams to strings. 276 */ 277 public static final F<Stream<Character>, String> Stream_String = new F<Stream<Character>, String>() { 278 public String f(final Stream<Character> cs) { 279 final StringBuilder sb = new StringBuilder(); 280 cs.foreach(new Effect<Character>() { 281 public void e(final Character c) { 282 sb.append(c); 283 } 284 }); 285 return sb.toString(); 286 } 287 }; 288 289 /** 290 * A function that converts streams to string buffers. 291 */ 292 public static final F<Stream<Character>, StringBuffer> Stream_StringBuffer = 293 new F<Stream<Character>, StringBuffer>() { 294 public StringBuffer f(final Stream<Character> cs) { 295 final StringBuffer sb = new StringBuffer(); 296 cs.foreach(new Effect<Character>() { 297 public void e(final Character c) { 298 sb.append(c); 299 } 300 }); 301 return sb; 302 } 303 }; 304 305 /** 306 * A function that converts streams to string builders. 307 */ 308 public static final F<Stream<Character>, StringBuilder> Stream_StringBuilder = 309 new F<Stream<Character>, StringBuilder>() { 310 public StringBuilder f(final Stream<Character> cs) { 311 final StringBuilder sb = new StringBuilder(); 312 cs.foreach(new Effect<Character>() { 313 public void e(final Character c) { 314 sb.append(c); 315 } 316 }); 317 return sb; 318 } 319 }; 320 321 // END Stream -> 322 323 // BEGIN Option -> 324 325 /** 326 * A function that converts options to lists. 327 * 328 * @return A function that converts options to lists. 329 */ 330 public static <A> F<Option<A>, List<A>> Option_List() { 331 return new F<Option<A>, List<A>>() { 332 public List<A> f(final Option<A> o) { 333 return o.toList(); 334 } 335 }; 336 } 337 338 /** 339 * A function that converts options to arrays. 340 * 341 * @return A function that converts options to arrays. 342 */ 343 public static <A> F<Option<A>, Array<A>> Option_Array() { 344 return new F<Option<A>, Array<A>>() { 345 public Array<A> f(final Option<A> o) { 346 return o.toArray(); 347 } 348 }; 349 } 350 351 /** 352 * A function that converts options to streams. 353 * 354 * @return A function that converts options to streams. 355 */ 356 public static <A> F<Option<A>, Stream<A>> Option_Stream() { 357 return new F<Option<A>, Stream<A>>() { 358 public Stream<A> f(final Option<A> o) { 359 return o.toStream(); 360 } 361 }; 362 } 363 364 /** 365 * A function that converts options to eithers. 366 * 367 * @return A function that converts options to eithers. 368 */ 369 public static <A, B> F<P1<A>, F<Option<B>, Either<A, B>>> Option_Either() { 370 return new F<P1<A>, F<Option<B>, Either<A, B>>>() { 371 public F<Option<B>, Either<A, B>> f(final P1<A> a) { 372 return new F<Option<B>, Either<A, B>>() { 373 public Either<A, B> f(final Option<B> o) { 374 return o.toEither(a); 375 } 376 }; 377 } 378 }; 379 } 380 381 /** 382 * A function that converts options to strings. 383 */ 384 public static final F<Option<Character>, String> Option_String = new F<Option<Character>, String>() { 385 public String f(final Option<Character> o) { 386 return asString(o.toList()); 387 } 388 }; 389 390 /** 391 * A function that converts options to string buffers. 392 */ 393 public static final F<Option<Character>, StringBuffer> Option_StringBuffer = 394 new F<Option<Character>, StringBuffer>() { 395 public StringBuffer f(final Option<Character> o) { 396 return new StringBuffer(asString(o.toList())); 397 } 398 }; 399 400 /** 401 * A function that converts options to string builders. 402 */ 403 public static final F<Option<Character>, StringBuilder> Option_StringBuilder = 404 new F<Option<Character>, StringBuilder>() { 405 public StringBuilder f(final Option<Character> o) { 406 return new StringBuilder(asString(o.toList())); 407 } 408 }; 409 410 // END Option -> 411 412 // BEGIN Either -> 413 414 /** 415 * A function that converts eithers to lists. 416 * 417 * @return A function that converts eithers to lists. 418 */ 419 public static <A, B> F<Either<A, B>, List<A>> Either_ListA() { 420 return new F<Either<A, B>, List<A>>() { 421 public List<A> f(final Either<A, B> e) { 422 return e.left().toList(); 423 } 424 }; 425 } 426 427 /** 428 * A function that converts eithers to lists. 429 * 430 * @return A function that converts eithers to lists. 431 */ 432 public static <A, B> F<Either<A, B>, List<B>> Either_ListB() { 433 return new F<Either<A, B>, List<B>>() { 434 public List<B> f(final Either<A, B> e) { 435 return e.right().toList(); 436 } 437 }; 438 } 439 440 /** 441 * A function that converts eithers to arrays. 442 * 443 * @return A function that converts eithers to arrays. 444 */ 445 public static <A, B> F<Either<A, B>, Array<A>> Either_ArrayA() { 446 return new F<Either<A, B>, Array<A>>() { 447 public Array<A> f(final Either<A, B> e) { 448 return e.left().toArray(); 449 } 450 }; 451 } 452 453 /** 454 * A function that converts eithers to arrays. 455 * 456 * @return A function that converts eithers to arrays. 457 */ 458 public static <A, B> F<Either<A, B>, Array<B>> Either_ArrayB() { 459 return new F<Either<A, B>, Array<B>>() { 460 public Array<B> f(final Either<A, B> e) { 461 return e.right().toArray(); 462 } 463 }; 464 } 465 466 /** 467 * A function that converts eithers to streams. 468 * 469 * @return A function that converts eithers to streams. 470 */ 471 public static <A, B> F<Either<A, B>, Stream<A>> Either_StreamA() { 472 return new F<Either<A, B>, Stream<A>>() { 473 public Stream<A> f(final Either<A, B> e) { 474 return e.left().toStream(); 475 } 476 }; 477 } 478 479 /** 480 * A function that converts eithers to streams. 481 * 482 * @return A function that converts eithers to streams. 483 */ 484 public static <A, B> F<Either<A, B>, Stream<B>> Either_StreamB() { 485 return new F<Either<A, B>, Stream<B>>() { 486 public Stream<B> f(final Either<A, B> e) { 487 return e.right().toStream(); 488 } 489 }; 490 } 491 492 /** 493 * A function that converts eithers to options. 494 * 495 * @return A function that converts eithers to options. 496 */ 497 public static <A, B> F<Either<A, B>, Option<A>> Either_OptionA() { 498 return new F<Either<A, B>, Option<A>>() { 499 public Option<A> f(final Either<A, B> e) { 500 return e.left().toOption(); 501 } 502 }; 503 } 504 505 /** 506 * A function that converts eithers to options. 507 * 508 * @return A function that converts eithers to options. 509 */ 510 public static <A, B> F<Either<A, B>, Option<B>> Either_OptionB() { 511 return new F<Either<A, B>, Option<B>>() { 512 public Option<B> f(final Either<A, B> e) { 513 return e.right().toOption(); 514 } 515 }; 516 } 517 518 /** 519 * A function that converts eithers to strings. 520 * 521 * @return A function that converts eithers to strings. 522 */ 523 public static <B> F<Either<Character, B>, String> Either_StringA() { 524 return new F<Either<Character, B>, String>() { 525 public String f(final Either<Character, B> e) { 526 return asString(e.left().toList()); 527 } 528 }; 529 } 530 531 /** 532 * A function that converts eithers to strings. 533 * 534 * @return A function that converts eithers to strings. 535 */ 536 public static <A> F<Either<A, Character>, String> Either_StringB() { 537 return new F<Either<A, Character>, String>() { 538 public String f(final Either<A, Character> e) { 539 return asString(e.right().toList()); 540 } 541 }; 542 } 543 544 /** 545 * A function that converts eithers to string buffers. 546 * 547 * @return A function that converts eithers to string buffers. 548 */ 549 public static <B> F<Either<Character, B>, StringBuffer> Either_StringBufferA() { 550 return new F<Either<Character, B>, StringBuffer>() { 551 public StringBuffer f(final Either<Character, B> e) { 552 return new StringBuffer(asString(e.left().toList())); 553 } 554 }; 555 } 556 557 /** 558 * A function that converts eithers to string buffers. 559 * 560 * @return A function that converts eithers to string buffers. 561 */ 562 public static <A> F<Either<A, Character>, StringBuffer> Either_StringBufferB() { 563 return new F<Either<A, Character>, StringBuffer>() { 564 public StringBuffer f(final Either<A, Character> e) { 565 return new StringBuffer(asString(e.right().toList())); 566 } 567 }; 568 } 569 570 /** 571 * A function that converts eithers to string builders. 572 * 573 * @return A function that converts eithers to string builders. 574 */ 575 public static <B> F<Either<Character, B>, StringBuilder> Either_StringBuilderA() { 576 return new F<Either<Character, B>, StringBuilder>() { 577 public StringBuilder f(final Either<Character, B> e) { 578 return new StringBuilder(asString(e.left().toList())); 579 } 580 }; 581 } 582 583 /** 584 * A function that converts eithers to string builders. 585 * 586 * @return A function that converts eithers to string builders. 587 */ 588 public static <A> F<Either<A, Character>, StringBuilder> Either_StringBuilderB() { 589 return new F<Either<A, Character>, StringBuilder>() { 590 public StringBuilder f(final Either<A, Character> e) { 591 return new StringBuilder(asString(e.right().toList())); 592 } 593 }; 594 } 595 596 // END Either -> 597 598 // BEGIN String -> 599 600 /** 601 * A function that converts strings to lists. 602 */ 603 public static final F<String, List<Character>> String_List = new F<String, List<Character>>() { 604 public List<Character> f(final String s) { 605 return fromString(s); 606 } 607 }; 608 609 /** 610 * A function that converts strings to arrays. 611 */ 612 public static final F<String, Array<Character>> String_Array = new F<String, Array<Character>>() { 613 public Array<Character> f(final String s) { 614 return fromString(s).toArray(); 615 } 616 }; 617 618 /** 619 * A function that converts strings to options. 620 */ 621 public static final F<String, Option<Character>> String_Option = new F<String, Option<Character>>() { 622 public Option<Character> f(final String s) { 623 return fromString(s).toOption(); 624 } 625 }; 626 627 /** 628 * A function that converts string to eithers. 629 * 630 * @return A function that converts string to eithers. 631 */ 632 public static <A> F<P1<A>, F<String, Either<A, Character>>> String_Either() { 633 return new F<P1<A>, F<String, Either<A, Character>>>() { 634 public F<String, Either<A, Character>> f(final P1<A> a) { 635 return new F<String, Either<A, Character>>() { 636 public Either<A, Character> f(final String s) { 637 return fromString(s).toEither(a); 638 } 639 }; 640 } 641 }; 642 } 643 644 /** 645 * A function that converts strings to streams. 646 */ 647 public static final F<String, Stream<Character>> String_Stream = new F<String, Stream<Character>>() { 648 public Stream<Character> f(final String s) { 649 return fromString(s).toStream(); 650 } 651 }; 652 653 /** 654 * A function that converts strings to string buffers. 655 */ 656 public static final F<String, StringBuffer> String_StringBuffer = new F<String, StringBuffer>() { 657 public StringBuffer f(final String s) { 658 return new StringBuffer(s); 659 } 660 }; 661 662 /** 663 * A function that converts strings to string builders. 664 */ 665 public static final F<String, StringBuilder> String_StringBuilder = new F<String, StringBuilder>() { 666 public StringBuilder f(final String s) { 667 return new StringBuilder(s); 668 } 669 }; 670 671 // END String -> 672 673 // BEGIN StringBuffer -> 674 675 /** 676 * A function that converts string buffers to lists. 677 */ 678 public static final F<StringBuffer, List<Character>> StringBuffer_List = new F<StringBuffer, List<Character>>() { 679 public List<Character> f(final StringBuffer s) { 680 return fromString(s.toString()); 681 } 682 }; 683 684 /** 685 * A function that converts string buffers to arrays. 686 */ 687 public static final F<StringBuffer, Array<Character>> StringBuffer_Array = new F<StringBuffer, Array<Character>>() { 688 public Array<Character> f(final StringBuffer s) { 689 return fromString(s.toString()).toArray(); 690 } 691 }; 692 693 /** 694 * A function that converts string buffers to streams. 695 */ 696 public static final F<StringBuffer, Stream<Character>> StringBuffer_Stream = 697 new F<StringBuffer, Stream<Character>>() { 698 public Stream<Character> f(final StringBuffer s) { 699 return fromString(s.toString()).toStream(); 700 } 701 }; 702 703 /** 704 * A function that converts string buffers to options. 705 */ 706 public static final F<StringBuffer, Option<Character>> StringBuffer_Option = 707 new F<StringBuffer, Option<Character>>() { 708 public Option<Character> f(final StringBuffer s) { 709 return fromString(s.toString()).toOption(); 710 } 711 }; 712 713 /** 714 * A function that converts string buffers to eithers. 715 * 716 * @return A function that converts string buffers to eithers. 717 */ 718 public static <A> F<P1<A>, F<StringBuffer, Either<A, Character>>> StringBuffer_Either() { 719 return new F<P1<A>, F<StringBuffer, Either<A, Character>>>() { 720 public F<StringBuffer, Either<A, Character>> f(final P1<A> a) { 721 return new F<StringBuffer, Either<A, Character>>() { 722 public Either<A, Character> f(final StringBuffer s) { 723 return fromString(s.toString()).toEither(a); 724 } 725 }; 726 } 727 }; 728 } 729 730 /** 731 * A function that converts string buffers to strings. 732 */ 733 public static final F<StringBuffer, String> StringBuffer_String = new F<StringBuffer, String>() { 734 public String f(final StringBuffer s) { 735 return s.toString(); 736 } 737 }; 738 739 /** 740 * A function that converts string buffers to string builders. 741 */ 742 public static final F<StringBuffer, StringBuilder> StringBuffer_StringBuilder = new F<StringBuffer, StringBuilder>() { 743 public StringBuilder f(final StringBuffer s) { 744 return new StringBuilder(s); 745 } 746 }; 747 748 // END StringBuffer -> 749 750 // BEGIN StringBuilder -> 751 752 /** 753 * A function that converts string builders to lists. 754 */ 755 public static final F<StringBuilder, List<Character>> StringBuilder_List = new F<StringBuilder, List<Character>>() { 756 public List<Character> f(final StringBuilder s) { 757 return fromString(s.toString()); 758 } 759 }; 760 761 /** 762 * A function that converts string builders to arrays. 763 */ 764 public static final F<StringBuilder, Array<Character>> StringBuilder_Array = 765 new F<StringBuilder, Array<Character>>() { 766 public Array<Character> f(final StringBuilder s) { 767 return fromString(s.toString()).toArray(); 768 } 769 }; 770 771 /** 772 * A function that converts string builders to streams. 773 */ 774 public static final F<StringBuilder, Stream<Character>> StringBuilder_Stream = 775 new F<StringBuilder, Stream<Character>>() { 776 public Stream<Character> f(final StringBuilder s) { 777 return fromString(s.toString()).toStream(); 778 } 779 }; 780 781 /** 782 * A function that converts string builders to options. 783 */ 784 public static final F<StringBuilder, Option<Character>> StringBuilder_Option = 785 new F<StringBuilder, Option<Character>>() { 786 public Option<Character> f(final StringBuilder s) { 787 return fromString(s.toString()).toOption(); 788 } 789 }; 790 791 /** 792 * A function that converts string builders to eithers. 793 * 794 * @return A function that converts string builders to eithers. 795 */ 796 public static <A> F<P1<A>, F<StringBuilder, Either<A, Character>>> StringBuilder_Either() { 797 return new F<P1<A>, F<StringBuilder, Either<A, Character>>>() { 798 public F<StringBuilder, Either<A, Character>> f(final P1<A> a) { 799 return new F<StringBuilder, Either<A, Character>>() { 800 public Either<A, Character> f(final StringBuilder s) { 801 return fromString(s.toString()).toEither(a); 802 } 803 }; 804 } 805 }; 806 } 807 808 /** 809 * A function that converts string builders to strings. 810 */ 811 public static final F<StringBuilder, String> StringBuilder_String = new F<StringBuilder, String>() { 812 public String f(final StringBuilder s) { 813 return s.toString(); 814 } 815 }; 816 817 /** 818 * A function that converts string builders to string buffers. 819 */ 820 public static final F<StringBuilder, StringBuffer> StringBuilder_StringBuffer = new F<StringBuilder, StringBuffer>() { 821 public StringBuffer f(final StringBuilder s) { 822 return new StringBuffer(s); 823 } 824 }; 825 826 // END StringBuilder -> 827 }