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