001// Generated by delombok at Sat Feb 25 17:58:34 UTC 2023 002package io.cui.tools.collect; 003 004import static io.cui.tools.collect.MoreCollections.isEmpty; 005import java.util.ArrayList; 006import java.util.Arrays; 007import java.util.Collection; 008import java.util.Collections; 009import java.util.HashMap; 010import java.util.HashSet; 011import java.util.Iterator; 012import java.util.List; 013import java.util.Map; 014import java.util.Set; 015import java.util.SortedSet; 016import java.util.TreeSet; 017import java.util.stream.Stream; 018 019/** 020 * <h2>Overview</h2> 021 * Provides a number of methods simplifying the task of creating populated 022 * {@link java.util.Collection}s. In 023 * essence its doing the same compared to the corresponding com.google.common.collect types 024 * but with different semantics (like naming, types) and is designed as a one-stop utility class. 025 * It differentiates between the subtypes and mutability / immutability. This class is 026 * complementary to the corresponding guava types. 027 * <h3>Lists</h3> 028 * 029 * <pre> 030 * assertMutable(CollectionLiterals#mutableList("1")); 031 * assertMutable(CollectionLiterals#mutableList("1", "2")); 032 * assertMutable(CollectionLiterals#mutableList(Arrays.asList("1", "2").stream())); 033 * </pre> 034 * 035 * <pre> 036 * assertImmutable(CollectionLiterals#immutableList("1")); 037 * assertImmutable(CollectionLiterals#immutableList("1", "2")); 038 * assertImmutable(CollectionLiterals#immutableList(Arrays.asList("1", "2").stream())); 039 * </pre> 040 * 041 * <h3>Sets</h3> 042 * 043 * <pre> 044 * assertMutable(CollectionLiterals#mutableSet("1")); 045 * assertMutable(CollectionLiterals#mutableSet("1", "2")); 046 * assertMutable(CollectionLiterals#mutableSet(Arrays.asList("1", "2").stream())); 047 * </pre> 048 * 049 * <pre> 050 * assertImmutable(CollectionLiterals#immutableSet("1")); 051 * assertImmutable(CollectionLiterals#immutableSet("1", "2")); 052 * assertImmutable(CollectionLiterals#immutableSet(Arrays.asList("1", "2").stream())); 053 * </pre> 054 * 055 * <h3>Maps</h3> 056 * 057 * <pre> 058 * assertMutable(CollectionLiterals#mutableMap()); 059 * assertMutable(CollectionLiterals#mutableMap("1", "1-1")); 060 * assertMutable(CollectionLiterals#mutableMap("1", "1-1", "2", "2-2", "3", "3-3", "4", "4-4")); 061 * </pre> 062 * 063 * <pre> 064 * assertImmutable(CollectionLiterals#immutableMap()); 065 * assertImmutable(CollectionLiterals#immutableMap("1", "1-1")); 066 * assertImmutable(CollectionLiterals#immutableMap("1", "1-1", "2", "2-2", "3", "3-3", "4", "4-4")); 067 * </pre> 068 * 069 * @author Oliver Wolff 070 */ 071public final class CollectionLiterals { 072 /** 073 * <p> 074 * mutableList. 075 * </p> 076 * 077 * @return a newly created empty {@link java.util.ArrayList} 078 * @param <E> a E class 079 */ 080 public static <E> List<E> mutableList() { 081 return new ArrayList<>(); 082 } 083 084 /** 085 * Creates a <i>mutable</i> {@code List} instance containing the given elements. 086 * 087 * @param elements to be added 088 * @return the <i>mutable</i> {@link java.util.List} with the given elements 089 * @param <E> a E class 090 */ 091 @SafeVarargs 092 public static <E> List<E> mutableList(E... elements) { 093 if (isEmpty(elements)) { 094 return new ArrayList<>(); 095 } 096 return new ArrayList<>(Arrays.asList(elements)); 097 } 098 099 /** 100 * Creates a <i>mutable</i> {@code List} instance containing the given element 101 * 102 * @param element to be added 103 * @return the <i>mutable</i> {@link java.util.List} with the given element 104 * @param <E> a E class 105 */ 106 public static <E> List<E> mutableList(E element) { 107 List<E> list = new ArrayList<>(); 108 list.add(element); 109 return list; 110 } 111 112 /** 113 * Creates a <i>mutable</i> {@code List} instance containing the given elements. 114 * 115 * @param elements to be added. If it is null and empty <i>mutable</i> list will be returned 116 * @return the <i>mutable</i> {@link java.util.List} with the given elements 117 * @param <E> a E class 118 */ 119 public static <E> List<E> mutableList(Iterable<? extends E> elements) { 120 var list = new ArrayList<E>(); 121 if (isEmpty(elements)) { 122 return list; 123 } 124 elements.forEach(list::add); 125 return list; 126 } 127 128 /** 129 * Creates a <i>mutable</i> {@code List} instance containing the given elements. 130 * 131 * @param elements to be added. If it is null and empty <i>mutable</i> list will be returned 132 * @return the <i>mutable</i> {@link java.util.List} with the given elements 133 * @param <E> a E class 134 */ 135 public static <E> List<E> mutableList(Collection<? extends E> elements) { 136 if (isEmpty(elements)) { 137 return new ArrayList<>(); 138 } 139 return new ArrayList<>(elements); 140 } 141 142 /** 143 * Creates a <i>mutable</i> {@code List} instance containing the given elements. 144 * <em>Caution:</em> The stream will be consumed by this operation 145 * 146 * @param elements to be added. If it is null an empty <i>mutable</i> {@code List} will be 147 * returned 148 * @return the <i>mutable</i> {@code List} with the given elements 149 * @param <E> a E class 150 */ 151 public static <E> List<E> mutableList(Stream<? extends E> elements) { 152 List<E> list = new ArrayList<>(); 153 if (isEmpty(elements)) { 154 return list; 155 } 156 elements.forEach(list::add); 157 return list; 158 } 159 160 /** 161 * Creates a <i>mutable</i> {@code List} instance containing the given elements. 162 * 163 * @param elements to be added. If it is null and empty <i>mutable</i> list will be returned 164 * @return the <i>mutable</i> {@link java.util.List} with the given elements 165 * @param <E> a E class 166 */ 167 public static <E> List<E> mutableList(Iterator<? extends E> elements) { 168 List<E> list = new ArrayList<>(); 169 if (isEmpty(elements)) { 170 return list; 171 } 172 while (elements.hasNext()) { 173 list.add(elements.next()); 174 } 175 return list; 176 } 177 178 /** 179 * Creates an <i>immutable</i> {@code List} instance. Convenience method for 180 * {@link java.util.Collections#emptyList()} 181 * 182 * @return the <i>immutable</i> {@link java.util.List} without any element 183 * @param <E> a E class 184 */ 185 public static <E> List<E> immutableList() { 186 return Collections.emptyList(); 187 } 188 189 /** 190 * Creates an <i>immutable</i> {@code List} instance containing the given elements. 191 * 192 * @param elements to be wrapped, must not be null 193 * @return the <i>immutable</i> {@link java.util.List} with the given elements 194 * @param <E> a E class 195 */ 196 @SafeVarargs 197 public static <E> List<E> immutableList(E... elements) { 198 if (isEmpty(elements)) { 199 return Collections.emptyList(); 200 } 201 return Collections.unmodifiableList(mutableList(elements)); 202 } 203 204 /** 205 * Creates an <i>immutable</i> {@code List} instance containing the given element. 206 * 207 * @param element to be wrapped, must not be null 208 * @return the <i>immutable</i> {@link java.util.List} with the given elements 209 * @param <E> a E class 210 */ 211 public static <E> List<E> immutableList(E element) { 212 if (null == element) { 213 return Collections.emptyList(); 214 } 215 return Collections.singletonList(element); 216 } 217 218 /** 219 * Creates an <i>immutable</i> {@code List} instance containing the given elements. 220 * 221 * @param elements to be wrapped, must not be null 222 * @return the <i>immutable</i> {@link java.util.List} with the given elements 223 * @param <E> a E class 224 */ 225 public static <E> List<E> immutableList(Iterable<? extends E> elements) { 226 if (isEmpty(elements)) { 227 return Collections.emptyList(); 228 } 229 return Collections.unmodifiableList(mutableList(elements)); 230 } 231 232 /** 233 * Creates an <i>immutable</i> {@code List} instance containing the given elements. 234 * 235 * @param elements to be wrapped, must not be null 236 * @return the <i>immutable</i> {@link java.util.List} with the given elements. It implicitly 237 * creates a 238 * copy 239 * @param <E> a E class 240 */ 241 public static <E> List<E> immutableList(Collection<? extends E> elements) { 242 if (isEmpty(elements)) { 243 return Collections.emptyList(); 244 } 245 return Collections.unmodifiableList(mutableList(elements)); 246 } 247 248 /** 249 * Creates an <i>immutable</i> {@code List} instance containing the given elements. 250 * <em>Caution:</em> The stream will be consumed by this operation 251 * 252 * @param elements to be wrapped, must not be null 253 * @return the <i>immutable</i> {@link java.util.List} with the given elements 254 * @param <E> a E class 255 */ 256 public static <E> List<E> immutableList(Stream<? extends E> elements) { 257 if (isEmpty(elements)) { 258 return Collections.emptyList(); 259 } 260 return Collections.unmodifiableList(mutableList(elements)); 261 } 262 263 /** 264 * Creates an <i>immutable</i> {@code List} instance containing the given elements. 265 * 266 * @param elements to be wrapped, must not be null 267 * @return the <i>immutable</i> {@link java.util.List} with the given elements 268 * @param <E> a E class 269 */ 270 public static <E> List<E> immutableList(Iterator<? extends E> elements) { 271 if (isEmpty(elements)) { 272 return Collections.emptyList(); 273 } 274 return Collections.unmodifiableList(mutableList(elements)); 275 } 276 277 /** 278 * <p> 279 * mutableSet. 280 * </p> 281 * 282 * @return a newly created empty {@link java.util.HashSet} 283 * @param <E> a E class 284 */ 285 public static <E> Set<E> mutableSet() { 286 return new HashSet<>(); 287 } 288 289 /** 290 * <p> 291 * mutableSet. 292 * </p> 293 * 294 * @param element to be added. If it is {@code null} it will not be added 295 * @return a newly created empty {@link java.util.HashSet} with the given elements 296 * @param <E> a E class 297 */ 298 public static <E> Set<E> mutableSet(E element) { 299 Set<E> set = new HashSet<>(); 300 if (null == element) { 301 return set; 302 } 303 set.add(element); 304 return set; 305 } 306 307 /** 308 * <p> 309 * mutableSet. 310 * </p> 311 * 312 * @param elements to be added 313 * @return a newly created empty {@link java.util.HashSet} with the given elements 314 * @param <E> a E class 315 */ 316 @SafeVarargs 317 public static <E> Set<E> mutableSet(E... elements) { 318 if (isEmpty(elements)) { 319 return new HashSet<>(); 320 } 321 return new HashSet<>(Arrays.asList(elements)); 322 } 323 324 /** 325 * Creates a <i>mutable</i> {@code Set} instance containing the given elements. 326 * 327 * @param elements to be added. If it is null and empty <i>mutable</i> list will be returned 328 * @return the <i>mutable</i> {@link java.util.Set} with the given elements 329 * @param <E> a E class 330 */ 331 public static <E> Set<E> mutableSet(Iterable<? extends E> elements) { 332 Set<E> set = new HashSet<>(); 333 if (isEmpty(elements)) { 334 return set; 335 } 336 elements.forEach(set::add); 337 return set; 338 } 339 340 /** 341 * Creates a <i>mutable</i> {@code Set} instance containing the given elements. 342 * 343 * @param elements to be added. If it is null and empty <i>mutable</i> list will be returned 344 * @return the <i>mutable</i> {@link java.util.Set} with the given elements 345 * @param <E> a E class 346 */ 347 public static <E> Set<E> mutableSet(Collection<? extends E> elements) { 348 if (isEmpty(elements)) { 349 return new HashSet<>(); 350 } 351 return new HashSet<>(elements); 352 } 353 354 /** 355 * Creates a <i>mutable</i> {@code Set} instance containing the given elements. 356 * 357 * @param elements to be added. If it is null and empty <i>mutable</i> list will be returned 358 * @return the <i>mutable</i> {@link java.util.Set} with the given elements 359 * @param <E> a E class 360 */ 361 public static <E> Set<E> mutableSet(Iterator<? extends E> elements) { 362 Set<E> set = new HashSet<>(); 363 if (isEmpty(elements)) { 364 return set; 365 } 366 while (elements.hasNext()) { 367 set.add(elements.next()); 368 } 369 return set; 370 } 371 372 /** 373 * Creates a <i>mutable</i> {@code Set} instance containing the given elements. 374 * <em>Caution:</em> The stream will be consumed by this operation 375 * 376 * @param elements to be added. If it is null and empty <i>mutable</i> {@link java.util.Set} 377 * will be 378 * returned 379 * @return the <i>mutable</i> {@link java.util.Set} with the given elements 380 * @param <E> a E class 381 */ 382 public static <E> Set<E> mutableSet(Stream<? extends E> elements) { 383 Set<E> set = new HashSet<>(); 384 if (isEmpty(elements)) { 385 return set; 386 } 387 elements.forEach(set::add); 388 return set; 389 } 390 391 /** 392 * <p> 393 * immutableSet. 394 * </p> 395 * 396 * @return a newly created empty {@link java.util.HashSet} Convenience method for 397 * {@link java.util.Collections#emptySet()} 398 * @param <E> a E class 399 */ 400 public static <E> Set<E> immutableSet() { 401 return Collections.emptySet(); 402 } 403 404 /** 405 * Creates an <i>immutable</i> {@code Set} instance containing the given elements. 406 * 407 * @param element to be wrapped, must not be null 408 * @return the <i>immutable</i> {@link java.util.Set} with the given elements 409 * @param <E> a E class 410 */ 411 public static <E> Set<E> immutableSet(E element) { 412 if (null == element) { 413 return Collections.emptySet(); 414 } 415 return Collections.singleton(element); 416 } 417 418 /** 419 * Creates an <i>immutable</i> {@code Set} instance containing the given elements. 420 * 421 * @param elements to be wrapped, must not be null 422 * @return the <i>immutable</i> {@link java.util.Set} with the given elements 423 * @param <E> a E class 424 */ 425 @SafeVarargs 426 public static <E> Set<E> immutableSet(E... elements) { 427 if (isEmpty(elements)) { 428 return Collections.emptySet(); 429 } 430 return Collections.unmodifiableSet(mutableSet(elements)); 431 } 432 433 /** 434 * Creates an <i>immutable</i> {@code Set} instance containing the given elements. 435 * 436 * @param elements to be wrapped 437 * @return the <i>immutable</i> {@link java.util.Set} with the given elements 438 * @param <E> a E class 439 */ 440 public static <E> Set<E> immutableSet(Iterable<? extends E> elements) { 441 if (isEmpty(elements)) { 442 return Collections.emptySet(); 443 } 444 return Collections.unmodifiableSet(mutableSet(elements)); 445 } 446 447 /** 448 * Creates an <i>immutable</i> {@code Set} instance containing the given elements. 449 * 450 * @param elements to be wrapped, must not be null 451 * @return the <i>immutable</i> {@link java.util.Set} with the given elements 452 * @param <E> a E class 453 */ 454 public static <E> Set<E> immutableSet(Iterator<? extends E> elements) { 455 if (isEmpty(elements)) { 456 return Collections.emptySet(); 457 } 458 return Collections.unmodifiableSet(mutableSet(elements)); 459 } 460 461 /** 462 * Creates an <i>immutable</i> {@code Set} instance containing the given elements. 463 * <em>Caution:</em> The stream will be consumed by this operation 464 * 465 * @param elements to be wrapped, must not be null 466 * @return the <i>immutable</i> {@link java.util.Set} with the given elements 467 * @param <E> a E class 468 */ 469 public static <E> Set<E> immutableSet(Stream<? extends E> elements) { 470 if (isEmpty(elements)) { 471 return Collections.emptySet(); 472 } 473 return Collections.unmodifiableSet(mutableSet(elements)); 474 } 475 476 /** 477 * <p> 478 * mutableSortedSet. 479 * </p> 480 * 481 * @return a newly created empty {@link java.util.TreeSet} 482 * @param <E> a E class 483 */ 484 public static <E> SortedSet<E> mutableSortedSet() { 485 return new TreeSet<>(); 486 } 487 488 /** 489 * <p> 490 * mutableSortedSet. 491 * </p> 492 * 493 * @param element to be added. If it is {@code null} an empty {@link java.util.SortedSet} will 494 * be returned 495 * @return a newly created empty {@link java.util.TreeSet} with the given element 496 * @param <E> a E class 497 */ 498 public static <E> SortedSet<E> mutableSortedSet(E element) { 499 SortedSet<E> set = new TreeSet<>(); 500 if (null == element) { 501 return set; 502 } 503 set.add(element); 504 return set; 505 } 506 507 /** 508 * <p> 509 * mutableSortedSet. 510 * </p> 511 * 512 * @param elements to be added 513 * @return a newly created empty {@link java.util.TreeSet} with the given elements 514 * @param <E> a E class 515 */ 516 @SafeVarargs 517 public static <E> SortedSet<E> mutableSortedSet(E... elements) { 518 SortedSet<E> set = new TreeSet<>(); 519 if (isEmpty(elements)) { 520 return set; 521 } 522 set.addAll(Arrays.asList(elements)); 523 return set; 524 } 525 526 /** 527 * Creates a <i>mutable</i> {@code SortedSet} instance containing the given elements. 528 * 529 * @param elements to be added. If it is null and empty <i>mutable</i> list will be returned 530 * @return the <i>mutable</i> {@link java.util.TreeSet} with the given elements 531 * @param <E> a E class 532 */ 533 public static <E> SortedSet<E> mutableSortedSet(Iterable<? extends E> elements) { 534 SortedSet<E> set = new TreeSet<>(); 535 if (null == elements) { 536 return set; 537 } 538 elements.forEach(set::add); 539 return set; 540 } 541 542 /** 543 * Creates a <i>mutable</i> {@code SortedSet} instance containing the given elements. 544 * 545 * @param elements to be added. If it is null and empty <i>mutable</i> list will be returned 546 * @return the <i>mutable</i> {@link java.util.Set} with the given elements 547 * @param <E> a E class 548 */ 549 public static <E> SortedSet<E> mutableSortedSet(Iterator<? extends E> elements) { 550 SortedSet<E> set = new TreeSet<>(); 551 if (null == elements) { 552 return set; 553 } 554 while (elements.hasNext()) { 555 set.add(elements.next()); 556 } 557 return set; 558 } 559 560 /** 561 * Creates a <i>mutable</i> {@code SortedSet} instance containing the given elements. 562 * <em>Caution:</em> The stream will be consumed by this operation 563 * 564 * @param elements to be added. If it is null and empty <i>mutable</i> list will be returned 565 * @return the <i>mutable</i> {@link java.util.Set} with the given elements 566 * @param <E> a E class 567 */ 568 public static <E> SortedSet<E> mutableSortedSet(Stream<? extends E> elements) { 569 SortedSet<E> set = new TreeSet<>(); 570 if (null == elements) { 571 return set; 572 } 573 elements.forEach(set::add); 574 return set; 575 } 576 577 /** 578 * <p> 579 * immutableSortedSet. 580 * </p> 581 * 582 * @return a newly created empty {@link java.util.SortedSet} Convenience method for 583 * {@link java.util.Collections#emptySortedSet()} 584 * @param <E> a E class 585 */ 586 public static <E> SortedSet<E> immutableSortedSet() { 587 return Collections.emptySortedSet(); 588 } 589 590 /** 591 * Creates an <i>immutable</i> {@code SortedSet} instance containing the given elements. 592 * 593 * @param element to be wrapped, must not be null 594 * @return the <i>immutable</i> {@link java.util.Set} with the given elements 595 * @param <E> a E class 596 */ 597 public static <E> SortedSet<E> immutableSortedSet(E element) { 598 if (null == element) { 599 return Collections.emptySortedSet(); 600 } 601 return Collections.unmodifiableSortedSet(mutableSortedSet(element)); 602 } 603 604 /** 605 * Creates an <i>immutable</i> {@code SortedSet} instance containing the given elements. 606 * 607 * @param elements to be wrapped, must not be null 608 * @return the <i>immutable</i> {@link java.util.Set} with the given elements 609 * @param <E> a E class 610 */ 611 @SafeVarargs 612 public static <E> SortedSet<E> immutableSortedSet(E... elements) { 613 return Collections.unmodifiableSortedSet(mutableSortedSet(elements)); 614 } 615 616 /** 617 * Creates an <i>immutable</i> {@code SortedSet} instance containing the given elements. 618 * 619 * @param elements to be wrapped, must not be null 620 * @return the <i>immutable</i> {@link java.util.Set} with the given elements 621 * @param <E> a E class 622 */ 623 public static <E> SortedSet<E> immutableSortedSet(Iterable<? extends E> elements) { 624 return Collections.unmodifiableSortedSet(mutableSortedSet(elements)); 625 } 626 627 /** 628 * Creates an <i>immutable</i> {@code SortedSet} instance containing the given elements. 629 * 630 * @param elements to be wrapped, must not be null 631 * @return the <i>immutable</i> {@link java.util.Set} with the given elements 632 * @param <E> a E class 633 */ 634 public static <E> SortedSet<E> immutableSortedSet(Iterator<? extends E> elements) { 635 return Collections.unmodifiableSortedSet(mutableSortedSet(elements)); 636 } 637 638 /** 639 * Creates an <i>immutable</i> {@code SortedSet} instance containing the given elements. 640 * <em>Caution:</em> The stream will be consumed by this operation 641 * 642 * @param elements to be wrapped, must not be null 643 * @return the <i>immutable</i> {@link java.util.Set} with the given elements 644 * @param <E> a E class 645 */ 646 public static <E> SortedSet<E> immutableSortedSet(Stream<? extends E> elements) { 647 return Collections.unmodifiableSortedSet(mutableSortedSet(elements)); 648 } 649 650 /** 651 * <p> 652 * mutableMap. 653 * </p> 654 * 655 * @return an empty <i>mutable</i> Map 656 * @param <K> a K class 657 * @param <V> a V class 658 */ 659 public static <K, V> Map<K, V> mutableMap() { 660 return new HashMap<>(); 661 } 662 663 /** 664 * Convenience method for the inline creation of a map with values 665 * 666 * @param k key to be added 667 * @param v value to be added 668 * @return a <i>mutable</i> Map with the given elements 669 * @param <K> a K class 670 * @param <V> a V class 671 */ 672 public static <K, V> Map<K, V> mutableMap(K k, V v) { 673 Map<K, V> map = new HashMap<>(); 674 map.put(k, v); 675 return map; 676 } 677 678 /** 679 * Convenience method for the inline creation of a map with values 680 * 681 * @param k1 key to be added 682 * @param v1 value to be added 683 * @param k2 key to be added 684 * @param v2 value to be added 685 * @return a <i>mutable</i> Map with the given elements 686 * @param <K> a K class 687 * @param <V> a V class 688 */ 689 public static <K, V> Map<K, V> mutableMap(K k1, V v1, K k2, V v2) { 690 Map<K, V> map = new HashMap<>(); 691 map.put(k1, v1); 692 map.put(k2, v2); 693 return map; 694 } 695 696 /** 697 * Convenience method for the inline creation of a map with values 698 * 699 * @param k1 key to be added 700 * @param v1 value to be added 701 * @param k2 key to be added 702 * @param v2 value to be added 703 * @param k3 key to be added 704 * @param v3 value to be added 705 * @return a <i>mutable</i> Map with the given elements 706 * @param <K> a K class 707 * @param <V> a V class 708 */ 709 public static <K, V> Map<K, V> mutableMap(K k1, V v1, K k2, V v2, K k3, V v3) { 710 Map<K, V> map = new HashMap<>(); 711 map.put(k1, v1); 712 map.put(k2, v2); 713 map.put(k3, v3); 714 return map; 715 } 716 717 // owolff: Number of parameters match to the use-case 718 /** 719 * Convenience method for the inline creation of a map with values 720 * 721 * @param k1 key to be added 722 * @param v1 value to be added 723 * @param k2 key to be added 724 * @param v2 value to be added 725 * @param k3 key to be added 726 * @param v3 value to be added 727 * @param k4 key to be added 728 * @param v4 value to be added 729 * @return a <i>mutable</i> Map with the given elements 730 * @param <K> a K class 731 * @param <V> a V class 732 */ 733 @SuppressWarnings("squid:S00107") 734 public static <K, V> Map<K, V> mutableMap(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4) { 735 Map<K, V> map = new HashMap<>(); 736 map.put(k1, v1); 737 map.put(k2, v2); 738 map.put(k3, v3); 739 map.put(k4, v4); 740 return map; 741 } 742 743 /** 744 * <p> 745 * immutableMap. 746 * </p> 747 * 748 * @return an empty <i>mutable</i> Map 749 * @param <K> a K class 750 * @param <V> a V class 751 */ 752 public static <K, V> Map<K, V> immutableMap() { 753 return Collections.emptyMap(); 754 } 755 756 /** 757 * Shorthand to {@link java.util.Collections#unmodifiableMap(Map)} 758 * 759 * @param source a {@link java.util.Map} object 760 * @return an <i>immutable</i> Map with the given elements 761 * @param <K> a K class 762 * @param <V> a V class 763 */ 764 public static <K, V> Map<K, V> immutableMap(Map<K, V> source) { 765 return Collections.unmodifiableMap(source); 766 } 767 768 /** 769 * Copies the entries of the given map stream into a new map which is then returned as an 770 * unmodifiable map. 771 * 772 * @param elements to be copied 773 * @param <K> key type 774 * @param <V> value type 775 * @return unmodifiable map with entries form the given stream 776 */ 777 public static <K, V> Map<K, V> immutableMap(Stream<Map.Entry<K, V>> elements) { 778 final Map<K, V> map = mutableMap(); 779 elements.forEach(entry -> map.put(entry.getKey(), entry.getValue())); 780 return Collections.unmodifiableMap(map); 781 } 782 783 /** 784 * Convenience method for the inline creation of a map with values 785 * 786 * @param k key to be added 787 * @param v value to be added 788 * @return an <i>immutable</i> Map with the given elements 789 * @param <K> a K class 790 * @param <V> a V class 791 */ 792 public static <K, V> Map<K, V> immutableMap(K k, V v) { 793 Map<K, V> map = mutableMap(); 794 map.put(k, v); 795 return Collections.unmodifiableMap(map); 796 } 797 798 /** 799 * Convenience method for the inline creation of a map with values 800 * 801 * @param k1 key to be added 802 * @param v1 value to be added 803 * @param k2 key to be added 804 * @param v2 value to be added 805 * @return an <i>immutable</i> Map with the given elements 806 * @param <K> a K class 807 * @param <V> a V class 808 */ 809 public static <K, V> Map<K, V> immutableMap(K k1, V v1, K k2, V v2) { 810 Map<K, V> map = mutableMap(); 811 map.put(k1, v1); 812 map.put(k2, v2); 813 return Collections.unmodifiableMap(map); 814 } 815 816 /** 817 * Convenience method for the inline creation of a map with values 818 * 819 * @param k1 key to be added 820 * @param v1 value to be added 821 * @param k2 key to be added 822 * @param v2 value to be added 823 * @param k3 key to be added 824 * @param v3 value to be added 825 * @return an <i>immutable</i> Map with the given elements 826 * @param <K> a K class 827 * @param <V> a V class 828 */ 829 public static <K, V> Map<K, V> immutableMap(K k1, V v1, K k2, V v2, K k3, V v3) { 830 Map<K, V> map = mutableMap(); 831 map.put(k1, v1); 832 map.put(k2, v2); 833 map.put(k3, v3); 834 return Collections.unmodifiableMap(map); 835 } 836 837 // owolff: Number of parameters match to the use-case 838 /** 839 * Convenience method for the inline creation of a map with values 840 * 841 * @param k1 key to be added 842 * @param v1 value to be added 843 * @param k2 key to be added 844 * @param v2 value to be added 845 * @param k3 key to be added 846 * @param v3 value to be added 847 * @param k4 key to be added 848 * @param v4 value to be added 849 * @return an <i>immutable</i> Map with the given elements 850 * @param <K> a K class 851 * @param <V> a V class 852 */ 853 @SuppressWarnings("squid:S00107") 854 public static <K, V> Map<K, V> immutableMap(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4) { 855 Map<K, V> map = mutableMap(); 856 map.put(k1, v1); 857 map.put(k2, v2); 858 map.put(k3, v3); 859 map.put(k4, v4); 860 return Collections.unmodifiableMap(map); 861 } 862 863 @java.lang.SuppressWarnings("all") 864 @lombok.Generated 865 private CollectionLiterals() { 866 throw new java.lang.UnsupportedOperationException("This is a utility class and cannot be instantiated"); 867 } 868}