How to use ImmutableCollections method of org.assertj.core.test.jdk11.ImmutableCollections class

Best Assertj code snippet using org.assertj.core.test.jdk11.ImmutableCollections.ImmutableCollections

Source:Jdk11.java Github

copy

Full Screen

...26 *27 * @since 928 */29 static <E> java.util.Set<E> of() {30 return ImmutableCollections.emptySet();31 }32 /**33 * Returns an unmodifiable set containing one element.34 * See <a href="#unmodifiable">Unmodifiable Sets</a> for details.35 *36 * @param <E> the {@code Set}'s element type37 * @param e1 the single element38 * @return a {@code Set} containing the specified element39 * @throws NullPointerException if the element is {@code null}40 *41 * @since 942 */43 static <E> java.util.Set<E> of(E e1) {44 return new ImmutableCollections.Set12<>(e1);45 }46 /**47 * Returns an unmodifiable set containing two elements.48 * See <a href="#unmodifiable">Unmodifiable Sets</a> for details.49 *50 * @param <E> the {@code Set}'s element type51 * @param e1 the first element52 * @param e2 the second element53 * @return a {@code Set} containing the specified elements54 * @throws IllegalArgumentException if the elements are duplicates55 * @throws NullPointerException if an element is {@code null}56 *57 * @since 958 */59 static <E> java.util.Set<E> of(E e1, E e2) {60 return new ImmutableCollections.Set12<>(e1, e2);61 }62 /**63 * Returns an unmodifiable set containing three elements.64 * See <a href="#unmodifiable">Unmodifiable Sets</a> for details.65 *66 * @param <E> the {@code Set}'s element type67 * @param e1 the first element68 * @param e2 the second element69 * @param e3 the third element70 * @return a {@code Set} containing the specified elements71 * @throws IllegalArgumentException if there are any duplicate elements72 * @throws NullPointerException if an element is {@code null}73 *74 * @since 975 */76 static <E> java.util.Set<E> of(E e1, E e2, E e3) {77 return new ImmutableCollections.SetN<>(e1, e2, e3);78 }79 /**80 * Returns an unmodifiable set containing four elements.81 * See <a href="#unmodifiable">Unmodifiable Sets</a> for details.82 *83 * @param <E> the {@code Set}'s element type84 * @param e1 the first element85 * @param e2 the second element86 * @param e3 the third element87 * @param e4 the fourth element88 * @return a {@code Set} containing the specified elements89 * @throws IllegalArgumentException if there are any duplicate elements90 * @throws NullPointerException if an element is {@code null}91 *92 * @since 993 */94 static <E> java.util.Set<E> of(E e1, E e2, E e3, E e4) {95 return new ImmutableCollections.SetN<>(e1, e2, e3, e4);96 }97 /**98 * Returns an unmodifiable set containing five elements.99 * See <a href="#unmodifiable">Unmodifiable Sets</a> for details.100 *101 * @param <E> the {@code Set}'s element type102 * @param e1 the first element103 * @param e2 the second element104 * @param e3 the third element105 * @param e4 the fourth element106 * @param e5 the fifth element107 * @return a {@code Set} containing the specified elements108 * @throws IllegalArgumentException if there are any duplicate elements109 * @throws NullPointerException if an element is {@code null}110 *111 * @since 9112 */113 static <E> java.util.Set<E> of(E e1, E e2, E e3, E e4, E e5) {114 return new ImmutableCollections.SetN<>(e1, e2, e3, e4, e5);115 }116 /**117 * Returns an unmodifiable set containing six elements.118 * See <a href="#unmodifiable">Unmodifiable Sets</a> for details.119 *120 * @param <E> the {@code Set}'s element type121 * @param e1 the first element122 * @param e2 the second element123 * @param e3 the third element124 * @param e4 the fourth element125 * @param e5 the fifth element126 * @param e6 the sixth element127 * @return a {@code Set} containing the specified elements128 * @throws IllegalArgumentException if there are any duplicate elements129 * @throws NullPointerException if an element is {@code null}130 *131 * @since 9132 */133 static <E> java.util.Set<E> of(E e1, E e2, E e3, E e4, E e5, E e6) {134 return new ImmutableCollections.SetN<>(e1, e2, e3, e4, e5,135 e6);136 }137 /**138 * Returns an unmodifiable set containing seven elements.139 * See <a href="#unmodifiable">Unmodifiable Sets</a> for details.140 *141 * @param <E> the {@code Set}'s element type142 * @param e1 the first element143 * @param e2 the second element144 * @param e3 the third element145 * @param e4 the fourth element146 * @param e5 the fifth element147 * @param e6 the sixth element148 * @param e7 the seventh element149 * @return a {@code Set} containing the specified elements150 * @throws IllegalArgumentException if there are any duplicate elements151 * @throws NullPointerException if an element is {@code null}152 *153 * @since 9154 */155 static <E> java.util.Set<E> of(E e1, E e2, E e3, E e4, E e5, E e6, E e7) {156 return new ImmutableCollections.SetN<>(e1, e2, e3, e4, e5,157 e6, e7);158 }159 /**160 * Returns an unmodifiable set containing eight elements.161 * See <a href="#unmodifiable">Unmodifiable Sets</a> for details.162 *163 * @param <E> the {@code Set}'s element type164 * @param e1 the first element165 * @param e2 the second element166 * @param e3 the third element167 * @param e4 the fourth element168 * @param e5 the fifth element169 * @param e6 the sixth element170 * @param e7 the seventh element171 * @param e8 the eighth element172 * @return a {@code Set} containing the specified elements173 * @throws IllegalArgumentException if there are any duplicate elements174 * @throws NullPointerException if an element is {@code null}175 *176 * @since 9177 */178 static <E> java.util.Set<E> of(E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8) {179 return new ImmutableCollections.SetN<>(e1, e2, e3, e4, e5,180 e6, e7, e8);181 }182 /**183 * Returns an unmodifiable set containing nine elements.184 * See <a href="#unmodifiable">Unmodifiable Sets</a> for details.185 *186 * @param <E> the {@code Set}'s element type187 * @param e1 the first element188 * @param e2 the second element189 * @param e3 the third element190 * @param e4 the fourth element191 * @param e5 the fifth element192 * @param e6 the sixth element193 * @param e7 the seventh element194 * @param e8 the eighth element195 * @param e9 the ninth element196 * @return a {@code Set} containing the specified elements197 * @throws IllegalArgumentException if there are any duplicate elements198 * @throws NullPointerException if an element is {@code null}199 *200 * @since 9201 */202 static <E> java.util.Set<E> of(E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8, E e9) {203 return new ImmutableCollections.SetN<>(e1, e2, e3, e4, e5,204 e6, e7, e8, e9);205 }206 /**207 * Returns an unmodifiable set containing ten elements.208 * See <a href="#unmodifiable">Unmodifiable Sets</a> for details.209 *210 * @param <E> the {@code Set}'s element type211 * @param e1 the first element212 * @param e2 the second element213 * @param e3 the third element214 * @param e4 the fourth element215 * @param e5 the fifth element216 * @param e6 the sixth element217 * @param e7 the seventh element218 * @param e8 the eighth element219 * @param e9 the ninth element220 * @param e10 the tenth element221 * @return a {@code Set} containing the specified elements222 * @throws IllegalArgumentException if there are any duplicate elements223 * @throws NullPointerException if an element is {@code null}224 *225 * @since 9226 */227 static <E> java.util.Set<E> of(E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8, E e9, E e10) {228 return new ImmutableCollections.SetN<>(e1, e2, e3, e4, e5,229 e6, e7, e8, e9, e10);230 }231 /**232 * Returns an unmodifiable set containing an arbitrary number of elements.233 * See <a href="#unmodifiable">Unmodifiable Sets</a> for details.234 *235 * @apiNote236 * This method also accepts a single array as an argument. The element type of237 * the resulting set will be the component type of the array, and the size of238 * the set will be equal to the length of the array. To create a set with239 * a single element that is an array, do the following:240 *241 * <pre>{@code242 * String[] array = ... ;243 * Set<String[]> list = Set.<String[]>of(array);244 * }</pre>245 *246 * This will cause the {@link java.util.Set#of(Object) Set.of(E)} method247 * to be invoked instead.248 *249 * @param <E> the {@code Set}'s element type250 * @param elements the elements to be contained in the set251 * @return a {@code Set} containing the specified elements252 * @throws IllegalArgumentException if there are any duplicate elements253 * @throws NullPointerException if an element is {@code null} or if the array is {@code null}254 *255 * @since 9256 */257 @SafeVarargs258 @SuppressWarnings("varargs")259 static <E> java.util.Set<E> of(E... elements) {260 switch (elements.length) { // implicit null check of elements261 case 0:262 return ImmutableCollections.emptySet();263 case 1:264 return new ImmutableCollections.Set12<>(elements[0]);265 case 2:266 return new ImmutableCollections.Set12<>(elements[0], elements[1]);267 default:268 return new ImmutableCollections.SetN<>(elements);269 }270 }271 /**272 * Returns an <a href="#unmodifiable">unmodifiable Set</a> containing the elements273 * of the given Collection. The given Collection must not be null, and it must not274 * contain any null elements. If the given Collection contains duplicate elements,275 * an arbitrary element of the duplicates is preserved. If the given Collection is276 * subsequently modified, the returned Set will not reflect such modifications.277 *278 * @implNote279 * If the given Collection is an <a href="#unmodifiable">unmodifiable Set</a>,280 * calling copyOf will generally not create a copy.281 *282 * @param <E> the {@code Set}'s element type283 * @param coll a {@code Collection} from which elements are drawn, must be non-null284 * @return a {@code Set} containing the elements of the given {@code Collection}285 * @throws NullPointerException if coll is null, or if it contains any nulls286 * @since 10287 */288 @SuppressWarnings("unchecked")289 static <E> java.util.Set<E> copyOf(Collection<? extends E> coll) {290 if (coll instanceof ImmutableCollections.AbstractImmutableSet) {291 return (java.util.Set<E>) coll;292 } else {293 return (java.util.Set<E>) Set.of(new HashSet<>(coll).toArray());294 }295 }296 }297 public interface List {298 /**299 * Returns an unmodifiable list containing zero elements.300 *301 * See <a href="#unmodifiable">Unmodifiable Lists</a> for details.302 *303 * @param <E> the {@code List}'s element type304 * @return an empty {@code List}305 *306 * @since 9307 */308 static <E> java.util.List<E> of() {309 return ImmutableCollections.emptyList();310 }311 /**312 * Returns an unmodifiable list containing one element.313 *314 * See <a href="#unmodifiable">Unmodifiable Lists</a> for details.315 *316 * @param <E> the {@code List}'s element type317 * @param e1 the single element318 * @return a {@code List} containing the specified element319 * @throws NullPointerException if the element is {@code null}320 *321 * @since 9322 */323 static <E> java.util.List<E> of(E e1) {324 return new ImmutableCollections.List12<>(e1);325 }326 /**327 * Returns an unmodifiable list containing two elements.328 *329 * See <a href="#unmodifiable">Unmodifiable Lists</a> for details.330 *331 * @param <E> the {@code List}'s element type332 * @param e1 the first element333 * @param e2 the second element334 * @return a {@code List} containing the specified elements335 * @throws NullPointerException if an element is {@code null}336 *337 * @since 9338 */339 static <E> java.util.List<E> of(E e1, E e2) {340 return new ImmutableCollections.List12<>(e1, e2);341 }342 /**343 * Returns an unmodifiable list containing three elements.344 *345 * See <a href="#unmodifiable">Unmodifiable Lists</a> for details.346 *347 * @param <E> the {@code List}'s element type348 * @param e1 the first element349 * @param e2 the second element350 * @param e3 the third element351 * @return a {@code List} containing the specified elements352 * @throws NullPointerException if an element is {@code null}353 *354 * @since 9355 */356 static <E> java.util.List<E> of(E e1, E e2, E e3) {357 return new ImmutableCollections.ListN<>(e1, e2, e3);358 }359 /**360 * Returns an unmodifiable list containing four elements.361 *362 * See <a href="#unmodifiable">Unmodifiable Lists</a> for details.363 *364 * @param <E> the {@code List}'s element type365 * @param e1 the first element366 * @param e2 the second element367 * @param e3 the third element368 * @param e4 the fourth element369 * @return a {@code List} containing the specified elements370 * @throws NullPointerException if an element is {@code null}371 *372 * @since 9373 */374 static <E> java.util.List<E> of(E e1, E e2, E e3, E e4) {375 return new ImmutableCollections.ListN<>(e1, e2, e3, e4);376 }377 /**378 * Returns an unmodifiable list containing five elements.379 *380 * See <a href="#unmodifiable">Unmodifiable Lists</a> for details.381 *382 * @param <E> the {@code List}'s element type383 * @param e1 the first element384 * @param e2 the second element385 * @param e3 the third element386 * @param e4 the fourth element387 * @param e5 the fifth element388 * @return a {@code List} containing the specified elements389 * @throws NullPointerException if an element is {@code null}390 *391 * @since 9392 */393 static <E> java.util.List<E> of(E e1, E e2, E e3, E e4, E e5) {394 return new ImmutableCollections.ListN<>(e1, e2, e3, e4, e5);395 }396 /**397 * Returns an unmodifiable list containing six elements.398 *399 * See <a href="#unmodifiable">Unmodifiable Lists</a> for details.400 *401 * @param <E> the {@code List}'s element type402 * @param e1 the first element403 * @param e2 the second element404 * @param e3 the third element405 * @param e4 the fourth element406 * @param e5 the fifth element407 * @param e6 the sixth element408 * @return a {@code List} containing the specified elements409 * @throws NullPointerException if an element is {@code null}410 *411 * @since 9412 */413 static <E> java.util.List<E> of(E e1, E e2, E e3, E e4, E e5, E e6) {414 return new ImmutableCollections.ListN<>(e1, e2, e3, e4, e5,415 e6);416 }417 /**418 * Returns an unmodifiable list containing seven elements.419 *420 * See <a href="#unmodifiable">Unmodifiable Lists</a> for details.421 *422 * @param <E> the {@code List}'s element type423 * @param e1 the first element424 * @param e2 the second element425 * @param e3 the third element426 * @param e4 the fourth element427 * @param e5 the fifth element428 * @param e6 the sixth element429 * @param e7 the seventh element430 * @return a {@code List} containing the specified elements431 * @throws NullPointerException if an element is {@code null}432 *433 * @since 9434 */435 static <E> java.util.List<E> of(E e1, E e2, E e3, E e4, E e5, E e6, E e7) {436 return new ImmutableCollections.ListN<>(e1, e2, e3, e4, e5,437 e6, e7);438 }439 /**440 * Returns an unmodifiable list containing eight elements.441 *442 * See <a href="#unmodifiable">Unmodifiable Lists</a> for details.443 *444 * @param <E> the {@code List}'s element type445 * @param e1 the first element446 * @param e2 the second element447 * @param e3 the third element448 * @param e4 the fourth element449 * @param e5 the fifth element450 * @param e6 the sixth element451 * @param e7 the seventh element452 * @param e8 the eighth element453 * @return a {@code List} containing the specified elements454 * @throws NullPointerException if an element is {@code null}455 *456 * @since 9457 */458 static <E> java.util.List<E> of(E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8) {459 return new ImmutableCollections.ListN<>(e1, e2, e3, e4, e5,460 e6, e7, e8);461 }462 /**463 * Returns an unmodifiable list containing nine elements.464 *465 * See <a href="#unmodifiable">Unmodifiable Lists</a> for details.466 *467 * @param <E> the {@code List}'s element type468 * @param e1 the first element469 * @param e2 the second element470 * @param e3 the third element471 * @param e4 the fourth element472 * @param e5 the fifth element473 * @param e6 the sixth element474 * @param e7 the seventh element475 * @param e8 the eighth element476 * @param e9 the ninth element477 * @return a {@code List} containing the specified elements478 * @throws NullPointerException if an element is {@code null}479 *480 * @since 9481 */482 static <E> java.util.List<E> of(E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8, E e9) {483 return new ImmutableCollections.ListN<>(e1, e2, e3, e4, e5,484 e6, e7, e8, e9);485 }486 /**487 * Returns an unmodifiable list containing ten elements.488 *489 * See <a href="#unmodifiable">Unmodifiable Lists</a> for details.490 *491 * @param <E> the {@code List}'s element type492 * @param e1 the first element493 * @param e2 the second element494 * @param e3 the third element495 * @param e4 the fourth element496 * @param e5 the fifth element497 * @param e6 the sixth element498 * @param e7 the seventh element499 * @param e8 the eighth element500 * @param e9 the ninth element501 * @param e10 the tenth element502 * @return a {@code List} containing the specified elements503 * @throws NullPointerException if an element is {@code null}504 *505 * @since 9506 */507 static <E> java.util.List<E> of(E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8, E e9, E e10) {508 return new ImmutableCollections.ListN<>(e1, e2, e3, e4, e5,509 e6, e7, e8, e9, e10);510 }511 /**512 * Returns an unmodifiable list containing an arbitrary number of elements.513 * See <a href="#unmodifiable">Unmodifiable Lists</a> for details.514 *515 * @apiNote516 * This method also accepts a single array as an argument. The element type of517 * the resulting list will be the component type of the array, and the size of518 * the list will be equal to the length of the array. To create a list with519 * a single element that is an array, do the following:520 *521 * <pre>{@code522 * String[] array = ... ;523 * List<String[]> list = List.<String[]>of(array);524 * }</pre>525 *526 * This will cause the {@link java.util.List#of(Object) List.of(E)} method527 * to be invoked instead.528 *529 * @param <E> the {@code List}'s element type530 * @param elements the elements to be contained in the list531 * @return a {@code List} containing the specified elements532 * @throws NullPointerException if an element is {@code null} or if the array is {@code null}533 *534 * @since 9535 */536 @SafeVarargs537 @SuppressWarnings("varargs")538 static <E> java.util.List<E> of(E... elements) {539 switch (elements.length) { // implicit null check of elements540 case 0:541 return ImmutableCollections.emptyList();542 case 1:543 return new ImmutableCollections.List12<>(elements[0]);544 case 2:545 return new ImmutableCollections.List12<>(elements[0], elements[1]);546 default:547 return new ImmutableCollections.ListN<>(elements);548 }549 }550 /**551 * Returns an <a href="#unmodifiable">unmodifiable List</a> containing the elements of552 * the given Collection, in its iteration order. The given Collection must not be null,553 * and it must not contain any null elements. If the given Collection is subsequently554 * modified, the returned List will not reflect such modifications.555 *556 * @implNote557 * If the given Collection is an <a href="#unmodifiable">unmodifiable List</a>,558 * calling copyOf will generally not create a copy.559 *560 * @param <E> the {@code List}'s element type561 * @param coll a {@code Collection} from which elements are drawn, must be non-null562 * @return a {@code List} containing the elements of the given {@code Collection}563 * @throws NullPointerException if coll is null, or if it contains any nulls564 * @since 10565 */566 static <E> java.util.List<E> copyOf(Collection<? extends E> coll) {567 return ImmutableCollections.listCopy(coll);568 }569 }570 public interface Map {571 /**572 * Returns an unmodifiable map containing zero mappings.573 * See <a href="#unmodifiable">Unmodifiable Maps</a> for details.574 *575 * @param <K> the {@code Map}'s key type576 * @param <V> the {@code Map}'s value type577 * @return an empty {@code Map}578 *579 * @since 9580 */581 static <K, V> java.util.Map<K, V> of() {582 return ImmutableCollections.emptyMap();583 }584 /**585 * Returns an unmodifiable map containing a single mapping.586 * See <a href="#unmodifiable">Unmodifiable Maps</a> for details.587 *588 * @param <K> the {@code Map}'s key type589 * @param <V> the {@code Map}'s value type590 * @param k1 the mapping's key591 * @param v1 the mapping's value592 * @return a {@code Map} containing the specified mapping593 * @throws NullPointerException if the key or the value is {@code null}594 *595 * @since 9596 */597 static <K, V> java.util.Map<K, V> of(K k1, V v1) {598 return new ImmutableCollections.Map1<>(k1, v1);599 }600 /**601 * Returns an unmodifiable map containing two mappings.602 * See <a href="#unmodifiable">Unmodifiable Maps</a> for details.603 *604 * @param <K> the {@code Map}'s key type605 * @param <V> the {@code Map}'s value type606 * @param k1 the first mapping's key607 * @param v1 the first mapping's value608 * @param k2 the second mapping's key609 * @param v2 the second mapping's value610 * @return a {@code Map} containing the specified mappings611 * @throws IllegalArgumentException if the keys are duplicates612 * @throws NullPointerException if any key or value is {@code null}613 *614 * @since 9615 */616 static <K, V> java.util.Map<K, V> of(K k1, V v1, K k2, V v2) {617 return new ImmutableCollections.MapN<>(k1, v1, k2, v2);618 }619 /**620 * Returns an unmodifiable map containing three mappings.621 * See <a href="#unmodifiable">Unmodifiable Maps</a> for details.622 *623 * @param <K> the {@code Map}'s key type624 * @param <V> the {@code Map}'s value type625 * @param k1 the first mapping's key626 * @param v1 the first mapping's value627 * @param k2 the second mapping's key628 * @param v2 the second mapping's value629 * @param k3 the third mapping's key630 * @param v3 the third mapping's value631 * @return a {@code Map} containing the specified mappings632 * @throws IllegalArgumentException if there are any duplicate keys633 * @throws NullPointerException if any key or value is {@code null}634 *635 * @since 9636 */637 static <K, V> java.util.Map<K, V> of(K k1, V v1, K k2, V v2, K k3, V v3) {638 return new ImmutableCollections.MapN<>(k1, v1, k2, v2, k3, v3);639 }640 /**641 * Returns an unmodifiable map containing four mappings.642 * See <a href="#unmodifiable">Unmodifiable Maps</a> for details.643 *644 * @param <K> the {@code Map}'s key type645 * @param <V> the {@code Map}'s value type646 * @param k1 the first mapping's key647 * @param v1 the first mapping's value648 * @param k2 the second mapping's key649 * @param v2 the second mapping's value650 * @param k3 the third mapping's key651 * @param v3 the third mapping's value652 * @param k4 the fourth mapping's key653 * @param v4 the fourth mapping's value654 * @return a {@code Map} containing the specified mappings655 * @throws IllegalArgumentException if there are any duplicate keys656 * @throws NullPointerException if any key or value is {@code null}657 *658 * @since 9659 */660 static <K, V> java.util.Map<K, V> of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4) {661 return new ImmutableCollections.MapN<>(k1, v1, k2, v2, k3, v3, k4, v4);662 }663 /**664 * Returns an unmodifiable map containing five mappings.665 * See <a href="#unmodifiable">Unmodifiable Maps</a> for details.666 *667 * @param <K> the {@code Map}'s key type668 * @param <V> the {@code Map}'s value type669 * @param k1 the first mapping's key670 * @param v1 the first mapping's value671 * @param k2 the second mapping's key672 * @param v2 the second mapping's value673 * @param k3 the third mapping's key674 * @param v3 the third mapping's value675 * @param k4 the fourth mapping's key676 * @param v4 the fourth mapping's value677 * @param k5 the fifth mapping's key678 * @param v5 the fifth mapping's value679 * @return a {@code Map} containing the specified mappings680 * @throws IllegalArgumentException if there are any duplicate keys681 * @throws NullPointerException if any key or value is {@code null}682 *683 * @since 9684 */685 static <K, V> java.util.Map<K, V> of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5) {686 return new ImmutableCollections.MapN<>(k1, v1, k2, v2, k3, v3, k4, v4, k5, v5);687 }688 /**689 * Returns an unmodifiable map containing six mappings.690 * See <a href="#unmodifiable">Unmodifiable Maps</a> for details.691 *692 * @param <K> the {@code Map}'s key type693 * @param <V> the {@code Map}'s value type694 * @param k1 the first mapping's key695 * @param v1 the first mapping's value696 * @param k2 the second mapping's key697 * @param v2 the second mapping's value698 * @param k3 the third mapping's key699 * @param v3 the third mapping's value700 * @param k4 the fourth mapping's key701 * @param v4 the fourth mapping's value702 * @param k5 the fifth mapping's key703 * @param v5 the fifth mapping's value704 * @param k6 the sixth mapping's key705 * @param v6 the sixth mapping's value706 * @return a {@code Map} containing the specified mappings707 * @throws IllegalArgumentException if there are any duplicate keys708 * @throws NullPointerException if any key or value is {@code null}709 *710 * @since 9711 */712 static <K, V> java.util.Map<K, V> of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5,713 K k6, V v6) {714 return new ImmutableCollections.MapN<>(k1, v1, k2, v2, k3, v3, k4, v4, k5, v5,715 k6, v6);716 }717 /**718 * Returns an unmodifiable map containing seven mappings.719 * See <a href="#unmodifiable">Unmodifiable Maps</a> for details.720 *721 * @param <K> the {@code Map}'s key type722 * @param <V> the {@code Map}'s value type723 * @param k1 the first mapping's key724 * @param v1 the first mapping's value725 * @param k2 the second mapping's key726 * @param v2 the second mapping's value727 * @param k3 the third mapping's key728 * @param v3 the third mapping's value729 * @param k4 the fourth mapping's key730 * @param v4 the fourth mapping's value731 * @param k5 the fifth mapping's key732 * @param v5 the fifth mapping's value733 * @param k6 the sixth mapping's key734 * @param v6 the sixth mapping's value735 * @param k7 the seventh mapping's key736 * @param v7 the seventh mapping's value737 * @return a {@code Map} containing the specified mappings738 * @throws IllegalArgumentException if there are any duplicate keys739 * @throws NullPointerException if any key or value is {@code null}740 *741 * @since 9742 */743 static <K, V> java.util.Map<K, V> of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5,744 K k6, V v6, K k7, V v7) {745 return new ImmutableCollections.MapN<>(k1, v1, k2, v2, k3, v3, k4, v4, k5, v5,746 k6, v6, k7, v7);747 }748 /**749 * Returns an unmodifiable map containing eight mappings.750 * See <a href="#unmodifiable">Unmodifiable Maps</a> for details.751 *752 * @param <K> the {@code Map}'s key type753 * @param <V> the {@code Map}'s value type754 * @param k1 the first mapping's key755 * @param v1 the first mapping's value756 * @param k2 the second mapping's key757 * @param v2 the second mapping's value758 * @param k3 the third mapping's key759 * @param v3 the third mapping's value760 * @param k4 the fourth mapping's key761 * @param v4 the fourth mapping's value762 * @param k5 the fifth mapping's key763 * @param v5 the fifth mapping's value764 * @param k6 the sixth mapping's key765 * @param v6 the sixth mapping's value766 * @param k7 the seventh mapping's key767 * @param v7 the seventh mapping's value768 * @param k8 the eighth mapping's key769 * @param v8 the eighth mapping's value770 * @return a {@code Map} containing the specified mappings771 * @throws IllegalArgumentException if there are any duplicate keys772 * @throws NullPointerException if any key or value is {@code null}773 *774 * @since 9775 */776 static <K, V> java.util.Map<K, V> of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5,777 K k6, V v6, K k7, V v7, K k8, V v8) {778 return new ImmutableCollections.MapN<>(k1, v1, k2, v2, k3, v3, k4, v4, k5, v5,779 k6, v6, k7, v7, k8, v8);780 }781 /**782 * Returns an unmodifiable map containing nine mappings.783 * See <a href="#unmodifiable">Unmodifiable Maps</a> for details.784 *785 * @param <K> the {@code Map}'s key type786 * @param <V> the {@code Map}'s value type787 * @param k1 the first mapping's key788 * @param v1 the first mapping's value789 * @param k2 the second mapping's key790 * @param v2 the second mapping's value791 * @param k3 the third mapping's key792 * @param v3 the third mapping's value793 * @param k4 the fourth mapping's key794 * @param v4 the fourth mapping's value795 * @param k5 the fifth mapping's key796 * @param v5 the fifth mapping's value797 * @param k6 the sixth mapping's key798 * @param v6 the sixth mapping's value799 * @param k7 the seventh mapping's key800 * @param v7 the seventh mapping's value801 * @param k8 the eighth mapping's key802 * @param v8 the eighth mapping's value803 * @param k9 the ninth mapping's key804 * @param v9 the ninth mapping's value805 * @return a {@code Map} containing the specified mappings806 * @throws IllegalArgumentException if there are any duplicate keys807 * @throws NullPointerException if any key or value is {@code null}808 *809 * @since 9810 */811 static <K, V> java.util.Map<K, V> of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5,812 K k6, V v6, K k7, V v7, K k8, V v8, K k9, V v9) {813 return new ImmutableCollections.MapN<>(k1, v1, k2, v2, k3, v3, k4, v4, k5, v5,814 k6, v6, k7, v7, k8, v8, k9, v9);815 }816 /**817 * Returns an unmodifiable map containing ten mappings.818 * See <a href="#unmodifiable">Unmodifiable Maps</a> for details.819 *820 * @param <K> the {@code Map}'s key type821 * @param <V> the {@code Map}'s value type822 * @param k1 the first mapping's key823 * @param v1 the first mapping's value824 * @param k2 the second mapping's key825 * @param v2 the second mapping's value826 * @param k3 the third mapping's key827 * @param v3 the third mapping's value828 * @param k4 the fourth mapping's key829 * @param v4 the fourth mapping's value830 * @param k5 the fifth mapping's key831 * @param v5 the fifth mapping's value832 * @param k6 the sixth mapping's key833 * @param v6 the sixth mapping's value834 * @param k7 the seventh mapping's key835 * @param v7 the seventh mapping's value836 * @param k8 the eighth mapping's key837 * @param v8 the eighth mapping's value838 * @param k9 the ninth mapping's key839 * @param v9 the ninth mapping's value840 * @param k10 the tenth mapping's key841 * @param v10 the tenth mapping's value842 * @return a {@code Map} containing the specified mappings843 * @throws IllegalArgumentException if there are any duplicate keys844 * @throws NullPointerException if any key or value is {@code null}845 *846 * @since 9847 */848 static <K, V> java.util.Map<K, V> of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5,849 K k6, V v6, K k7, V v7, K k8, V v8, K k9, V v9, K k10, V v10) {850 return new ImmutableCollections.MapN<>(k1, v1, k2, v2, k3, v3, k4, v4, k5, v5,851 k6, v6, k7, v7, k8, v8, k9, v9, k10, v10);852 }853 /**854 * Returns an unmodifiable map containing keys and values extracted from the given entries.855 * The entries themselves are not stored in the map.856 * See <a href="#unmodifiable">Unmodifiable Maps</a> for details.857 *858 * @apiNote859 * It is convenient to create the map entries using the {@link java.util.Map#entry Map.entry()} method.860 * For example,861 *862 * <pre>{@code863 * import static java.util.Map.entry;864 *865 * Map<Integer,String> map = Map.ofEntries(866 * entry(1, "a"),867 * entry(2, "b"),868 * entry(3, "c"),869 * ...870 * entry(26, "z"));871 * }</pre>872 *873 * @param <K> the {@code Map}'s key type874 * @param <V> the {@code Map}'s value type875 * @param entries {@code Map.Entry}s containing the keys and values from which the map is populated876 * @return a {@code Map} containing the specified mappings877 * @throws IllegalArgumentException if there are any duplicate keys878 * @throws NullPointerException if any entry, key, or value is {@code null}, or if879 * the {@code entries} array is {@code null}880 *881 * @see java.util.Map#entry Map.entry()882 * @since 9883 */884 @SafeVarargs885 @SuppressWarnings("varargs")886 static <K, V> java.util.Map<K, V> ofEntries(java.util.Map.Entry<? extends K, ? extends V>... entries) {887 if (entries.length == 0) { // implicit null check of entries array888 return ImmutableCollections.emptyMap();889 } else if (entries.length == 1) {890 // implicit null check of the array slot891 return new ImmutableCollections.Map1<>(entries[0].getKey(),892 entries[0].getValue());893 } else {894 Object[] kva = new Object[entries.length << 1];895 int a = 0;896 for (java.util.Map.Entry<? extends K, ? extends V> entry : entries) {897 // implicit null checks of each array slot898 kva[a++] = entry.getKey();899 kva[a++] = entry.getValue();900 }901 return new ImmutableCollections.MapN<>(kva);902 }903 }904 /**905 * Returns an unmodifiable {@link java.util.Map.Entry} containing the given key and value.906 * These entries are suitable for populating {@code Map} instances using the907 * {@link java.util.Map#ofEntries Map.ofEntries()} method.908 * The {@code Entry} instances created by this method have the following characteristics:909 *910 * <ul>911 * <li>They disallow {@code null} keys and values. Attempts to create them using a {@code null}912 * key or value result in {@code NullPointerException}.913 * <li>They are unmodifiable. Calls to {@link java.util.Map.Entry#setValue Entry.setValue()}914 * on a returned {@code Entry} result in {@code UnsupportedOperationException}.915 * <li>They are not serializable.916 * <li>They are <a href="../lang/doc-files/ValueBased.html">value-based</a>.917 * Callers should make no assumptions about the identity of the returned instances.918 * This method is free to create new instances or reuse existing ones. Therefore,919 * identity-sensitive operations on these instances (reference equality ({@code ==}),920 * identity hash code, and synchronization) are unreliable and should be avoided.921 * </ul>922 *923 * @apiNote924 * For a serializable {@code Entry}, see {@link AbstractMap.SimpleEntry} or925 * {@link AbstractMap.SimpleImmutableEntry}.926 *927 * @param <K> the key's type928 * @param <V> the value's type929 * @param k the key930 * @param v the value931 * @return an {@code Entry} containing the specified key and value932 * @throws NullPointerException if the key or value is {@code null}933 *934 * @see java.util.Map#ofEntries Map.ofEntries()935 * @since 9936 */937 static <K, V> java.util.Map.Entry<K, V> entry(K k, V v) {938 // KeyValueHolder checks for nulls939 return new KeyValueHolder<>(k, v);940 }941 /**942 * Returns an <a href="#unmodifiable">unmodifiable Map</a> containing the entries943 * of the given Map. The given Map must not be null, and it must not contain any944 * null keys or values. If the given Map is subsequently modified, the returned945 * Map will not reflect such modifications.946 *947 * @implNote948 * If the given Map is an <a href="#unmodifiable">unmodifiable Map</a>,949 * calling copyOf will generally not create a copy.950 *951 * @param <K> the {@code Map}'s key type952 * @param <V> the {@code Map}'s value type953 * @param map a {@code Map} from which entries are drawn, must be non-null954 * @return a {@code Map} containing the entries of the given {@code Map}955 * @throws NullPointerException if map is null, or if it contains any null keys or values956 * @since 10957 */958 @SuppressWarnings({ "rawtypes", "unchecked" })959 static <K, V> java.util.Map<K, V> copyOf(java.util.Map<? extends K, ? extends V> map) {960 if (map instanceof ImmutableCollections.AbstractImmutableMap) {961 return (java.util.Map<K, V>) map;962 } else {963 return Map.ofEntries(map.entrySet().toArray(new java.util.Map.Entry[0]));964 }965 }966 }967}...

Full Screen

Full Screen

Source:ImmutableCollections.java Github

copy

Full Screen

...34import java.util.function.Function;35import java.util.function.Predicate;36import java.util.function.UnaryOperator;37/**38 * Copied from {@code java.util.ImmutableCollections}.39 * 40 * Container class for immutable collections. Not part of the public API.41 * Mainly for namespace management and shared infrastructure.42 *43 * Serial warnings are suppressed throughout because all implementation44 * classes use a serial proxy and thus have no need to declare serialVersionUID.45 */46class ImmutableCollections {47 /**48 * A "salt" value used for randomizing iteration order. This is initialized once49 * and stays constant for the lifetime of the JVM. It need not be truly random, but50 * it needs to vary sufficiently from one run to the next so that iteration order51 * will vary between JVM runs.52 */53 static final int SALT;54 static {55 long nt = System.nanoTime();56 SALT = (int) ((nt >>> 32) ^ nt);57 }58 /** No instances. */59 private ImmutableCollections() {}60 /**61 * The reciprocal of load factor. Given a number of elements62 * to store, multiply by this factor to get the table size.63 */64 static final int EXPAND_FACTOR = 2;65 static UnsupportedOperationException uoe() {66 return new UnsupportedOperationException();67 }68 static abstract class AbstractImmutableCollection<E> extends AbstractCollection<E> {69 // all mutating methods throw UnsupportedOperationException70 @Override71 public boolean add(E e) {72 throw uoe();73 }74 @Override75 public boolean addAll(Collection<? extends E> c) {76 throw uoe();77 }78 @Override79 public void clear() {80 throw uoe();81 }82 @Override83 public boolean remove(Object o) {84 throw uoe();85 }86 @Override87 public boolean removeAll(Collection<?> c) {88 throw uoe();89 }90 @Override91 public boolean removeIf(Predicate<? super E> filter) {92 throw uoe();93 }94 @Override95 public boolean retainAll(Collection<?> c) {96 throw uoe();97 }98 }99 // ---------- List Implementations ----------100 // make a copy, short-circuiting based on implementation class101 @SuppressWarnings("unchecked")102 static <E> List<E> listCopy(Collection<? extends E> coll) {103 if (coll instanceof AbstractImmutableList && coll.getClass() != SubList.class) {104 return (List<E>) coll;105 } else {106 return (List<E>) Jdk11.List.of(coll.toArray());107 }108 }109 @SuppressWarnings("unchecked")110 static <E> List<E> emptyList() {111 return (List<E>) ListN.EMPTY_LIST;112 }113 static abstract class AbstractImmutableList<E> extends AbstractImmutableCollection<E>114 implements List<E>, RandomAccess {115 // all mutating methods throw UnsupportedOperationException116 @Override117 public void add(int index, E element) {118 throw uoe();119 }120 @Override121 public boolean addAll(int index, Collection<? extends E> c) {122 throw uoe();123 }124 @Override125 public E remove(int index) {126 throw uoe();127 }128 @Override129 public void replaceAll(UnaryOperator<E> operator) {130 throw uoe();131 }132 @Override133 public E set(int index, E element) {134 throw uoe();135 }136 @Override137 public void sort(Comparator<? super E> c) {138 throw uoe();139 }140 @Override141 public List<E> subList(int fromIndex, int toIndex) {142 int size = size();143 subListRangeCheck(fromIndex, toIndex, size);144 return SubList.fromList(this, fromIndex, toIndex);145 }146 static void subListRangeCheck(int fromIndex, int toIndex, int size) {147 if (fromIndex < 0)148 throw new IndexOutOfBoundsException("fromIndex = " + fromIndex);149 if (toIndex > size)150 throw new IndexOutOfBoundsException("toIndex = " + toIndex);151 if (fromIndex > toIndex)152 throw new IllegalArgumentException("fromIndex(" + fromIndex +153 ") > toIndex(" + toIndex + ")");154 }155 @Override156 public Iterator<E> iterator() {157 return new ListItr<E>(this, size());158 }159 @Override160 public ListIterator<E> listIterator() {161 return listIterator(0);162 }163 @Override164 public ListIterator<E> listIterator(final int index) {165 int size = size();166 if (index < 0 || index > size) {167 throw outOfBounds(index);168 }169 return new ListItr<E>(this, size, index);170 }171 @Override172 public boolean equals(Object o) {173 if (o == this) {174 return true;175 }176 if (!(o instanceof List)) {177 return false;178 }179 Iterator<?> oit = ((List<?>) o).iterator();180 for (int i = 0, s = size(); i < s; i++) {181 if (!oit.hasNext() || !get(i).equals(oit.next())) {182 return false;183 }184 }185 return !oit.hasNext();186 }187 @Override188 public int indexOf(Object o) {189 Objects.requireNonNull(o);190 for (int i = 0, s = size(); i < s; i++) {191 if (o.equals(get(i))) {192 return i;193 }194 }195 return -1;196 }197 @Override198 public int lastIndexOf(Object o) {199 Objects.requireNonNull(o);200 for (int i = size() - 1; i >= 0; i--) {201 if (o.equals(get(i))) {202 return i;203 }204 }205 return -1;206 }207 @Override208 public int hashCode() {209 int hash = 1;210 for (int i = 0, s = size(); i < s; i++) {211 hash = 31 * hash + get(i).hashCode();212 }213 return hash;214 }215 @Override216 public boolean contains(Object o) {217 return indexOf(o) >= 0;218 }219 IndexOutOfBoundsException outOfBounds(int index) {220 return new IndexOutOfBoundsException("Index: " + index + " Size: " + size());221 }222 }223 static final class ListItr<E> implements ListIterator<E> {224 private final List<E> list;225 private final int size;226 private final boolean isListIterator;227 private int cursor;228 ListItr(List<E> list, int size) {229 this.list = list;230 this.size = size;231 this.cursor = 0;232 isListIterator = false;233 }234 ListItr(List<E> list, int size, int index) {235 this.list = list;236 this.size = size;237 this.cursor = index;238 isListIterator = true;239 }240 public boolean hasNext() {241 return cursor != size;242 }243 public E next() {244 try {245 int i = cursor;246 E next = list.get(i);247 cursor = i + 1;248 return next;249 } catch (IndexOutOfBoundsException e) {250 throw new NoSuchElementException();251 }252 }253 public void remove() {254 throw uoe();255 }256 public boolean hasPrevious() {257 if (!isListIterator) {258 throw uoe();259 }260 return cursor != 0;261 }262 public E previous() {263 if (!isListIterator) {264 throw uoe();265 }266 try {267 int i = cursor - 1;268 E previous = list.get(i);269 cursor = i;270 return previous;271 } catch (IndexOutOfBoundsException e) {272 throw new NoSuchElementException();273 }274 }275 public int nextIndex() {276 if (!isListIterator) {277 throw uoe();278 }279 return cursor;280 }281 public int previousIndex() {282 if (!isListIterator) {283 throw uoe();284 }285 return cursor - 1;286 }287 public void set(E e) {288 throw uoe();289 }290 public void add(E e) {291 throw uoe();292 }293 }294 static final class SubList<E> extends AbstractImmutableList<E>295 implements RandomAccess {296 private final List<E> root;297 private final int offset;298 private final int size;299 private SubList(List<E> root, int offset, int size) {300 this.root = root;301 this.offset = offset;302 this.size = size;303 }304 /**305 * Constructs a sublist of another SubList.306 */307 static <E> SubList<E> fromSubList(SubList<E> parent, int fromIndex, int toIndex) {308 return new SubList<>(parent.root, parent.offset + fromIndex, toIndex - fromIndex);309 }310 /**311 * Constructs a sublist of an arbitrary AbstractImmutableList, which is312 * not a SubList itself.313 */314 static <E> SubList<E> fromList(List<E> list, int fromIndex, int toIndex) {315 return new SubList<>(list, fromIndex, toIndex - fromIndex);316 }317 public E get(int index) {318 // Replacement for Objects.checkIndex(index, size);319 if (index < 0 || index >= size) {320 throw new IndexOutOfBoundsException(String.format("Index %s out of bounds for length %s", index, size));321 }322 return root.get(offset + index);323 }324 public int size() {325 return size;326 }327 public Iterator<E> iterator() {328 return new ListItr<>(this, size());329 }330 public ListIterator<E> listIterator(int index) {331 rangeCheck(index);332 return new ListItr<>(this, size(), index);333 }334 public List<E> subList(int fromIndex, int toIndex) {335 subListRangeCheck(fromIndex, toIndex, size);336 return SubList.fromSubList(this, fromIndex, toIndex);337 }338 private void rangeCheck(int index) {339 if (index < 0 || index > size) {340 throw outOfBounds(index);341 }342 }343 }344 static final class List12<E> extends AbstractImmutableList<E>345 implements Serializable {346 private final E e0;347 private final E e1;348 List12(E e0) {349 this.e0 = Objects.requireNonNull(e0);350 this.e1 = null;351 }352 List12(E e0, E e1) {353 this.e0 = Objects.requireNonNull(e0);354 this.e1 = Objects.requireNonNull(e1);355 }356 @Override357 public int size() {358 return e1 != null ? 2 : 1;359 }360 @Override361 public E get(int index) {362 if (index == 0) {363 return e0;364 } else if (index == 1 && e1 != null) {365 return e1;366 }367 throw outOfBounds(index);368 }369 private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {370 throw new InvalidObjectException("not serial proxy");371 }372 private Object writeReplace() {373 if (e1 == null) {374 return new CollSer(CollSer.IMM_LIST, e0);375 } else {376 return new CollSer(CollSer.IMM_LIST, e0, e1);377 }378 }379 }380 static final class ListN<E> extends AbstractImmutableList<E>381 implements Serializable {382 // EMPTY_LIST may be initialized from the CDS archive.383 static List<?> EMPTY_LIST;384 static {385 // VM.initializeFromArchive(ListN.class);386 if (EMPTY_LIST == null) {387 EMPTY_LIST = new ListN<>();388 }389 }390 private final E[] elements;391 @SafeVarargs392 ListN(E... input) {393 // copy and check manually to avoid TOCTOU394 @SuppressWarnings("unchecked")395 E[] tmp = (E[]) new Object[input.length]; // implicit nullcheck of input396 for (int i = 0; i < input.length; i++) {397 tmp[i] = Objects.requireNonNull(input[i]);398 }399 elements = tmp;400 }401 @Override402 public boolean isEmpty() {403 return size() == 0;404 }405 @Override406 public int size() {407 return elements.length;408 }409 @Override410 public E get(int index) {411 return elements[index];412 }413 private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {414 throw new InvalidObjectException("not serial proxy");415 }416 private Object writeReplace() {417 return new CollSer(CollSer.IMM_LIST, elements);418 }419 }420 // ---------- Set Implementations ----------421 static abstract class AbstractImmutableSet<E> extends AbstractImmutableCollection<E>422 implements Set<E> {423 @Override424 public boolean equals(Object o) {425 if (o == this) {426 return true;427 } else if (!(o instanceof Set)) {428 return false;429 }430 Collection<?> c = (Collection<?>) o;431 if (c.size() != size()) {432 return false;433 }434 for (Object e : c) {435 if (e == null || !contains(e)) {436 return false;437 }438 }439 return true;440 }441 @Override442 public abstract int hashCode();443 }444 @SuppressWarnings("unchecked")445 static <E> Set<E> emptySet() {446 return (Set<E>) SetN.EMPTY_SET;447 }448 static final class Set12<E> extends AbstractImmutableSet<E>449 implements Serializable {450 final E e0;451 final E e1;452 Set12(E e0) {453 this.e0 = Objects.requireNonNull(e0);454 this.e1 = null;455 }456 Set12(E e0, E e1) {457 if (e0.equals(Objects.requireNonNull(e1))) { // implicit nullcheck of e0458 throw new IllegalArgumentException("duplicate element: " + e0);459 }460 this.e0 = e0;461 this.e1 = e1;462 }463 @Override464 public int size() {465 return (e1 == null) ? 1 : 2;466 }467 @Override468 public boolean contains(Object o) {469 return o.equals(e0) || o.equals(e1); // implicit nullcheck of o470 }471 @Override472 public int hashCode() {473 return e0.hashCode() + (e1 == null ? 0 : e1.hashCode());474 }475 @Override476 public Iterator<E> iterator() {477 return new Iterator<E>() {478 private int idx = size();479 @Override480 public boolean hasNext() {481 return idx > 0;482 }483 @Override484 public E next() {485 if (idx == 1) {486 idx = 0;487 return SALT >= 0 || e1 == null ? e0 : e1;488 } else if (idx == 2) {489 idx = 1;490 return SALT >= 0 ? e1 : e0;491 } else {492 throw new NoSuchElementException();493 }494 }495 };496 }497 private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {498 throw new InvalidObjectException("not serial proxy");499 }500 private Object writeReplace() {501 if (e1 == null) {502 return new CollSer(CollSer.IMM_SET, e0);503 } else {504 return new CollSer(CollSer.IMM_SET, e0, e1);505 }506 }507 }508 /**509 * An array-based Set implementation. The element array must be strictly510 * larger than the size (the number of contained elements) so that at511 * least one null is always present.512 * @param <E> the element type513 */514 static final class SetN<E> extends AbstractImmutableSet<E>515 implements Serializable {516 // EMPTY_SET may be initialized from the CDS archive.517 static Set<?> EMPTY_SET;518 static {519 // VM.initializeFromArchive(SetN.class);520 if (EMPTY_SET == null) {521 EMPTY_SET = new SetN<>();522 }523 }524 final E[] elements;525 final int size;526 @SafeVarargs527 @SuppressWarnings("unchecked")528 SetN(E... input) {529 size = input.length; // implicit nullcheck of input530 elements = (E[]) new Object[EXPAND_FACTOR * input.length];531 for (int i = 0; i < input.length; i++) {532 E e = input[i];533 int idx = probe(e); // implicit nullcheck of e534 if (idx >= 0) {535 throw new IllegalArgumentException("duplicate element: " + e);536 } else {537 elements[-(idx + 1)] = e;538 }539 }540 }541 @Override542 public int size() {543 return size;544 }545 @Override546 public boolean contains(Object o) {547 Objects.requireNonNull(o);548 return size > 0 && probe(o) >= 0;549 }550 private final class SetNIterator implements Iterator<E> {551 private int remaining;552 private int idx;553 SetNIterator() {554 remaining = size();555 if (remaining > 0) {556 idx = Math.floorMod(SALT, elements.length);557 }558 }559 @Override560 public boolean hasNext() {561 return remaining > 0;562 }563 private int nextIndex() {564 int idx = this.idx;565 if (SALT >= 0) {566 if (++idx >= elements.length) {567 idx = 0;568 }569 } else {570 if (--idx < 0) {571 idx = elements.length - 1;572 }573 }574 return this.idx = idx;575 }576 @Override577 public E next() {578 if (hasNext()) {579 E element;580 // skip null elements581 while ((element = elements[nextIndex()]) == null) {}582 remaining--;583 return element;584 } else {585 throw new NoSuchElementException();586 }587 }588 }589 @Override590 public Iterator<E> iterator() {591 return new SetN<E>.SetNIterator();592 }593 @Override594 public int hashCode() {595 int h = 0;596 for (E e : elements) {597 if (e != null) {598 h += e.hashCode();599 }600 }601 return h;602 }603 // returns index at which element is present; or if absent,604 // (-i - 1) where i is location where element should be inserted.605 // Callers are relying on this method to perform an implicit nullcheck606 // of pe607 private int probe(Object pe) {608 int idx = Math.floorMod(pe.hashCode(), elements.length);609 while (true) {610 E ee = elements[idx];611 if (ee == null) {612 return -idx - 1;613 } else if (pe.equals(ee)) {614 return idx;615 } else if (++idx == elements.length) {616 idx = 0;617 }618 }619 }620 private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {621 throw new InvalidObjectException("not serial proxy");622 }623 private Object writeReplace() {624 Object[] array = new Object[size];625 int dest = 0;626 for (Object o : elements) {627 if (o != null) {628 array[dest++] = o;629 }630 }631 return new CollSer(CollSer.IMM_SET, array);632 }633 }634 // ---------- Map Implementations ----------635 @SuppressWarnings("unchecked")636 static <K, V> Map<K, V> emptyMap() {637 return (Map<K, V>) MapN.EMPTY_MAP;638 }639 abstract static class AbstractImmutableMap<K, V> extends AbstractMap<K, V> implements Serializable {640 @Override641 public void clear() {642 throw uoe();643 }644 @Override645 public V compute(K key, BiFunction<? super K, ? super V, ? extends V> rf) {646 throw uoe();647 }648 @Override649 public V computeIfAbsent(K key, Function<? super K, ? extends V> mf) {650 throw uoe();651 }652 @Override653 public V computeIfPresent(K key, BiFunction<? super K, ? super V, ? extends V> rf) {654 throw uoe();655 }656 @Override657 public V merge(K key, V value, BiFunction<? super V, ? super V, ? extends V> rf) {658 throw uoe();659 }660 @Override661 public V put(K key, V value) {662 throw uoe();663 }664 @Override665 public void putAll(Map<? extends K, ? extends V> m) {666 throw uoe();667 }668 @Override669 public V putIfAbsent(K key, V value) {670 throw uoe();671 }672 @Override673 public V remove(Object key) {674 throw uoe();675 }676 @Override677 public boolean remove(Object key, Object value) {678 throw uoe();679 }680 @Override681 public V replace(K key, V value) {682 throw uoe();683 }684 @Override685 public boolean replace(K key, V oldValue, V newValue) {686 throw uoe();687 }688 @Override689 public void replaceAll(BiFunction<? super K, ? super V, ? extends V> f) {690 throw uoe();691 }692 }693 static final class Map1<K, V> extends AbstractImmutableMap<K, V> {694 private final K k0;695 private final V v0;696 Map1(K k0, V v0) {697 this.k0 = Objects.requireNonNull(k0);698 this.v0 = Objects.requireNonNull(v0);699 }700 @Override701 public Set<Map.Entry<K, V>> entrySet() {702 return Jdk11.Set.of(new KeyValueHolder<>(k0, v0));703 }704 @Override705 public V get(Object o) {706 return o.equals(k0) ? v0 : null; // implicit nullcheck of o707 }708 @Override709 public boolean containsKey(Object o) {710 return o.equals(k0); // implicit nullcheck of o711 }712 @Override713 public boolean containsValue(Object o) {714 return o.equals(v0); // implicit nullcheck of o715 }716 private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {717 throw new InvalidObjectException("not serial proxy");718 }719 private Object writeReplace() {720 return new CollSer(CollSer.IMM_MAP, k0, v0);721 }722 @Override723 public int hashCode() {724 return k0.hashCode() ^ v0.hashCode();725 }726 }727 /**728 * An array-based Map implementation. There is a single array "table" that729 * contains keys and values interleaved: table[0] is kA, table[1] is vA,730 * table[2] is kB, table[3] is vB, etc. The table size must be even. It must731 * also be strictly larger than the size (the number of key-value pairs contained732 * in the map) so that at least one null key is always present.733 * @param <K> the key type734 * @param <V> the value type735 */736 static final class MapN<K, V> extends AbstractImmutableMap<K, V> {737 // EMPTY_MAP may be initialized from the CDS archive.738 static Map<?, ?> EMPTY_MAP;739 static {740 // VM.initializeFromArchive(MapN.class);741 EMPTY_MAP = new MapN<>();742 }743 final Object[] table; // pairs of key, value744 final int size; // number of pairs745 MapN(Object... input) {746 if ((input.length & 1) != 0) { // implicit nullcheck of input747 throw new InternalError("length is odd");748 }749 size = input.length >> 1;750 int len = EXPAND_FACTOR * input.length;751 len = (len + 1) & ~1; // ensure table is even length752 table = new Object[len];753 for (int i = 0; i < input.length; i += 2) {754 @SuppressWarnings("unchecked")755 K k = Objects.requireNonNull((K) input[i]);756 @SuppressWarnings("unchecked")757 V v = Objects.requireNonNull((V) input[i + 1]);758 int idx = probe(k);759 if (idx >= 0) {760 throw new IllegalArgumentException("duplicate key: " + k);761 } else {762 int dest = -(idx + 1);763 table[dest] = k;764 table[dest + 1] = v;765 }766 }767 }768 @Override769 public boolean containsKey(Object o) {770 Objects.requireNonNull(o);771 return size > 0 && probe(o) >= 0;772 }773 @Override774 public boolean containsValue(Object o) {775 Objects.requireNonNull(o);776 for (int i = 1; i < table.length; i += 2) {777 Object v = table[i];778 if (v != null && o.equals(v)) {779 return true;780 }781 }782 return false;783 }784 @Override785 public int hashCode() {786 int hash = 0;787 for (int i = 0; i < table.length; i += 2) {788 Object k = table[i];789 if (k != null) {790 hash += k.hashCode() ^ table[i + 1].hashCode();791 }792 }793 return hash;794 }795 @Override796 @SuppressWarnings("unchecked")797 public V get(Object o) {798 if (size == 0) {799 Objects.requireNonNull(o);800 return null;801 }802 int i = probe(o);803 if (i >= 0) {804 return (V) table[i + 1];805 } else {806 return null;807 }808 }809 @Override810 public int size() {811 return size;812 }813 class MapNIterator implements Iterator<Map.Entry<K, V>> {814 private int remaining;815 private int idx;816 MapNIterator() {817 remaining = size();818 if (remaining > 0) {819 idx = Math.floorMod(SALT, table.length >> 1) << 1;820 }821 }822 @Override823 public boolean hasNext() {824 return remaining > 0;825 }826 private int nextIndex() {827 int idx = this.idx;828 if (SALT >= 0) {829 if ((idx += 2) >= table.length) {830 idx = 0;831 }832 } else {833 if ((idx -= 2) < 0) {834 idx = table.length - 2;835 }836 }837 return this.idx = idx;838 }839 @Override840 public Map.Entry<K, V> next() {841 if (hasNext()) {842 while (table[nextIndex()] == null) {}843 @SuppressWarnings("unchecked")844 Map.Entry<K, V> e = new KeyValueHolder<>((K) table[idx], (V) table[idx + 1]);845 remaining--;846 return e;847 } else {848 throw new NoSuchElementException();849 }850 }851 }852 @Override853 public Set<Map.Entry<K, V>> entrySet() {854 return new AbstractSet<Map.Entry<K, V>>() {855 @Override856 public int size() {857 return MapN.this.size;858 }859 @Override860 public Iterator<Map.Entry<K, V>> iterator() {861 return new MapNIterator();862 }863 };864 }865 // returns index at which the probe key is present; or if absent,866 // (-i - 1) where i is location where element should be inserted.867 // Callers are relying on this method to perform an implicit nullcheck868 // of pk.869 private int probe(Object pk) {870 int idx = Math.floorMod(pk.hashCode(), table.length >> 1) << 1;871 while (true) {872 @SuppressWarnings("unchecked")873 K ek = (K) table[idx];874 if (ek == null) {875 return -idx - 1;876 } else if (pk.equals(ek)) {877 return idx;878 } else if ((idx += 2) == table.length) {879 idx = 0;880 }881 }882 }883 private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {884 throw new InvalidObjectException("not serial proxy");885 }886 private Object writeReplace() {887 Object[] array = new Object[2 * size];888 int len = table.length;889 int dest = 0;890 for (int i = 0; i < len; i += 2) {891 if (table[i] != null) {892 array[dest++] = table[i];893 array[dest++] = table[i + 1];894 }895 }896 return new CollSer(CollSer.IMM_MAP, array);897 }898 }899}900// ---------- Serialization Proxy ----------901/**902 * Copied from {@code java.util.CollSer}.903 * 904 * A unified serialization proxy class for the immutable collections.905 *906 * @serial907 * @since 9908 */909final class CollSer implements Serializable {910 private static final long serialVersionUID = 6309168927139932177L;911 static final int IMM_LIST = 1;912 static final int IMM_SET = 2;913 static final int IMM_MAP = 3;914 /**915 * Indicates the type of collection that is serialized.916 * The low order 8 bits have the value 1 for an immutable917 * {@code List}, 2 for an immutable {@code Set}, and 3 for918 * an immutable {@code Map}. Any other value causes an919 * {@link InvalidObjectException} to be thrown. The high920 * order 24 bits are zero when an instance is serialized,921 * and they are ignored when an instance is deserialized.922 * They can thus be used by future implementations without923 * causing compatibility issues.924 *925 * <p>The tag value also determines the interpretation of the926 * transient {@code Object[] array} field.927 * For {@code List} and {@code Set}, the array's length is the size928 * of the collection, and the array contains the elements of the collection.929 * Null elements are not allowed. For {@code Set}, duplicate elements930 * are not allowed.931 *932 * <p>For {@code Map}, the array's length is twice the number of mappings933 * present in the map. The array length is necessarily even.934 * The array contains a succession of key and value pairs:935 * {@code k1, v1, k2, v2, ..., kN, vN.} Nulls are not allowed,936 * and duplicate keys are not allowed.937 *938 * @serial939 * @since 9940 */941 private final int tag;942 /**943 * @serial944 * @since 9945 */946 private transient Object[] array;947 CollSer(int t, Object... a) {948 tag = t;949 array = a;950 }951 /**952 * Reads objects from the stream and stores them953 * in the transient {@code Object[] array} field.954 *955 * @serialData956 * A nonnegative int, indicating the count of objects,957 * followed by that many objects.958 *959 * @param ois the ObjectInputStream from which data is read960 * @throws IOException if an I/O error occurs961 * @throws ClassNotFoundException if a serialized class cannot be loaded962 * @throws InvalidObjectException if the count is negative963 * @since 9964 */965 private void readObject(ObjectInputStream ois) throws IOException, ClassNotFoundException {966 ois.defaultReadObject();967 int len = ois.readInt();968 if (len < 0) {969 throw new InvalidObjectException("negative length " + len);970 }971 // SharedSecrets.getJavaObjectInputStreamAccess().checkArray(ois, Object[].class, len);972 Object[] a = new Object[len];973 for (int i = 0; i < len; i++) {974 a[i] = ois.readObject();975 }976 array = a;977 }978 /**979 * Writes objects to the stream from980 * the transient {@code Object[] array} field.981 *982 * @serialData983 * A nonnegative int, indicating the count of objects,984 * followed by that many objects.985 *986 * @param oos the ObjectOutputStream to which data is written987 * @throws IOException if an I/O error occurs988 * @since 9989 */990 private void writeObject(ObjectOutputStream oos) throws IOException {991 oos.defaultWriteObject();992 oos.writeInt(array.length);993 for (int i = 0; i < array.length; i++) {994 oos.writeObject(array[i]);995 }996 }997 /**998 * Creates and returns an immutable collection from this proxy class.999 * The instance returned is created as if by calling one of the1000 * static factory methods for1001 * <a href="List.html#immutable">List</a>,1002 * <a href="Map.html#immutable">Map</a>, or1003 * <a href="Set.html#immutable">Set</a>.1004 * This proxy class is the serial form for all immutable collection instances,1005 * regardless of implementation type. This is necessary to ensure that the1006 * existence of any particular implementation type is kept out of the1007 * serialized form.1008 *1009 * @return a collection created from this proxy object1010 * @throws InvalidObjectException if the tag value is illegal or if an exception1011 * is thrown during creation of the collection1012 * @throws ObjectStreamException if another serialization error has occurred1013 * @since 91014 */1015 private Object readResolve() throws ObjectStreamException {1016 try {1017 if (array == null) {1018 throw new InvalidObjectException("null array");1019 }1020 // use low order 8 bits to indicate "kind"1021 // ignore high order 24 bits1022 switch (tag & 0xff) {1023 case IMM_LIST:1024 return Jdk11.List.of(array);1025 case IMM_SET:1026 return Jdk11.Set.of(array);1027 case IMM_MAP:1028 if (array.length == 0) {1029 return ImmutableCollections.emptyMap();1030 } else if (array.length == 2) {1031 return new ImmutableCollections.Map1<>(array[0], array[1]);1032 } else {1033 return new ImmutableCollections.MapN<>(array);1034 }1035 default:1036 throw new InvalidObjectException(String.format("invalid flags 0x%x", tag));1037 }1038 } catch (NullPointerException | IllegalArgumentException ex) {1039 InvalidObjectException ioe = new InvalidObjectException("invalid object");1040 ioe.initCause(ex);1041 throw ioe;1042 }1043 }1044}...

Full Screen

Full Screen

ImmutableCollections

Using AI Code Generation

copy

Full Screen

1import org.assertj.core.test.jdk11.ImmutableCollections;2import java.util.List;3public class 1 {4 public static void main(String[] args) {5 List<Integer> list = ImmutableCollections.listOf(1, 2, 3);6 list.forEach(System.out::println);7 }8}9import org.assertj.core.test.jdk11.ImmutableCollections;10import java.util.List;11public class 2 {12 public static void main(String[] args) {13 List<Integer> list = ImmutableCollections.listOf(1, 2, 3);14 list.forEach(System.out::println);15 }16}17import org.assertj.core.test.jdk11.ImmutableCollections;18import java.util.List;19public class 3 {20 public static void main(String[] args) {21 List<Integer> list = ImmutableCollections.listOf(1, 2, 3);22 list.forEach(System.out::println);23 }24}25import org.assertj.core.test.jdk11.ImmutableCollections;26import java.util.List;27public class 4 {28 public static void main(String[] args) {29 List<Integer> list = ImmutableCollections.listOf(1, 2, 3);30 list.forEach(System.out::println);31 }32}33 at 3.main(3.java:6)34 at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:581)35 at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)36 at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)

Full Screen

Full Screen

ImmutableCollections

Using AI Code Generation

copy

Full Screen

1import org.assertj.core.test.jdk11.ImmutableCollections;2import java.util.List;3public class ImmutableCollectionsTest {4public static void main(String[] args) {5List<Integer> list = ImmutableCollections.of(1, 2, 3);6System.out.println(list);7}8}

Full Screen

Full Screen

ImmutableCollections

Using AI Code Generation

copy

Full Screen

1import org.assertj.core.test.jdk11.ImmutableCollections;2import java.util.List;3public class Test {4 public static void main(String[] args) {5 List<Integer> list = ImmutableCollections.of(1, 2, 3);6 System.out.println(list);7 }8}9 List<Integer> list = ImmutableCollections.of(1, 2, 3);10 symbol: method of(int,int,int)11 List<Integer> list = ImmutableCollections.of(1, 2, 3);12 symbol: method of(int,int,int)13 List<Integer> list = ImmutableCollections.of(1, 2, 3);14 symbol: method of(int,int,int)

Full Screen

Full Screen

ImmutableCollections

Using AI Code Generation

copy

Full Screen

1import org.assertj.core.test.jdk11.ImmutableCollections;2import java.util.List;3import java.util.Set;4import java.util.Map;5import java.util.stream.Collectors;6import java.util.stream.Stream;7import java.util.stream.IntStream;8import java.util.stream.DoubleStream;9import java.util.stream.LongStream;10import java.util.stream.StreamSupport;11import java.util.stream.Stream.Builder;12import java.util.stream.DoubleStream.Builder;13import java.util.stream.IntStream.Builder;14import java.util.stream.LongStream.Builder;15import java.util.stream.Stream.Builder;16import java.util.stream.Collectors;

Full Screen

Full Screen

ImmutableCollections

Using AI Code Generation

copy

Full Screen

1import org.assertj.core.test.jdk11.ImmutableCollections;2import java.util.List;3public class 1 {4 public static void main(String[] args) {5 List<String> list = ImmutableCollections.of("a", "b", "c");6 list.forEach(System.out::println);7 }8}

Full Screen

Full Screen

ImmutableCollections

Using AI Code Generation

copy

Full Screen

1import org.assertj.core.test.jdk11.ImmutableCollections;2import java.util.List;3import java.util.Set;4public class ImmutableCollectionsTest {5 public static void main(String[] args) {6 List<String> list = ImmutableCollections.listOf("one", "two", "three");7 Set<String> set = ImmutableCollections.setOf("one", "two", "three");8 System.out.println(list);9 System.out.println(set);10 }11}12ImmutableList.copyOf()13ImmutableList.of()14ImmutableList.builder()15Example 1: Using ImmutableList.copyOf()16import com.google.common.collect.ImmutableList;17import java.util.List;18public class ImmutableListTest {19 public static void main(String[] args) {20 List<String> list = List.of("one", "two", "three");21 ImmutableList<String> immutableList = ImmutableList.copyOf(list);22 System.out.println(immutableList);23 }24}25Example 2: Using ImmutableList.of()26import com.google.common.collect.ImmutableList;27public class ImmutableListTest {28 public static void main(String[] args) {29 ImmutableList<String> immutableList = ImmutableList.of("one", "two", "three");30 System.out.println(immutableList);31 }32}33Example 3: Using ImmutableList.builder()34import com.google.common.collect.ImmutableList;35public class ImmutableListTest {36 public static void main(String[] args) {37 ImmutableList<String> immutableList = ImmutableList.<String>builder()38 .add("one")39 .add("two")40 .add("three")41 .build();42 System.out.println(immutableList);43 }44}

Full Screen

Full Screen

ImmutableCollections

Using AI Code Generation

copy

Full Screen

1import static org.assertj.core.test.jdk11.ImmutableCollections.*;2import java.util.*;3class Test {4 public static void main(String[] args) {5 List<String> list = of("a", "b", "c");6 System.out.println(list);7 }8}9import static org.assertj.core.test.jdk11.ImmutableCollections.*;10import java.util.*;11class Test {12 public static void main(String[] args) {13 List<String> list = of("a", "b", "c");14 System.out.println(list);15 }16}17import static org.assertj.core.test.jdk11.ImmutableCollections.*;18import java.util.*;19class Test {20 public static void main(String[] args) {21 List<String> list = of("a", "b", "c");22 System.out.println(list);23 }24}25import static org.assertj.core.test.jdk11.ImmutableCollections.*;26import java.util.*;27class Test {28 public static void main(String[] args) {29 List<String> list = of("a", "b", "c");30 System.out.println(list);31 }32}33import static org.assertj.core.test.jdk11.ImmutableCollections.*;34import java.util.*;35class Test {36 public static void main(String[] args) {37 List<String> list = of("a", "b", "c");38 System.out.println(list);39 }40}41import static org.assertj.core.test.jdk11.ImmutableCollections.*;42import java.util.*;43class Test {44 public static void main(String[] args) {45 List<String> list = of("a", "b", "c");46 System.out.println(list);47 }48}49import static org.assertj.core.test.jdk11.ImmutableCollections.*;50import java.util.*;

Full Screen

Full Screen

ImmutableCollections

Using AI Code Generation

copy

Full Screen

1package com.guru99.demo;2import java.util.List;3import org.assertj.core.test.jdk11.ImmutableCollections;4public class ImmutableCollectionsTest {5 public static void main(String[] args) {6 List<String> list = ImmutableCollections.listOf("A", "B", "C");7 System.out.println(list);8 }9}

Full Screen

Full Screen

ImmutableCollections

Using AI Code Generation

copy

Full Screen

1import org.assertj.core.test.jdk11.ImmutableCollections;2class Test {3 public static void main(String[] args) {4 ImmutableCollections immutableCollections = new ImmutableCollections();5 immutableCollections.listOf();6 }7}8import org.assertj.core.test.jdk8.ImmutableCollections;9class Test {10 public static void main(String[] args) {11 ImmutableCollections immutableCollections = new ImmutableCollections();12 immutableCollections.listOf();13 }14}15Workaround (if any):

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful