File tree 2 files changed +19
-6
lines changed
libs/core/src/main/java/org/elasticsearch/core 2 files changed +19
-6
lines changed Original file line number Diff line number Diff line change 8
8
9
9
package org .elasticsearch .core ;
10
10
11
+ import java .util .ArrayList ;
11
12
import java .util .Arrays ;
12
13
import java .util .Collection ;
13
14
import java .util .Collections ;
@@ -75,8 +76,14 @@ public static <T> java.util.List<T> of(T... entries) {
75
76
* @param coll a {@code Collection} from which elements are drawn, must be non-null
76
77
* @return a {@code List} containing the elements of the given {@code Collection}
77
78
*/
78
- @ SuppressWarnings ("unchecked" )
79
79
public static <T > java .util .List <T > copyOf (Collection <? extends T > coll ) {
80
- return (java .util .List <T >) List .of (coll .toArray ());
80
+ switch (coll .size ()) {
81
+ case 0 :
82
+ return Collections .emptyList ();
83
+ case 1 :
84
+ return Collections .singletonList (coll .iterator ().next ());
85
+ default :
86
+ return Collections .unmodifiableList (new ArrayList <>(coll ));
87
+ }
81
88
}
82
89
}
Original file line number Diff line number Diff line change @@ -61,9 +61,9 @@ public static <T> java.util.Set<T> of(T e1, T e2) {
61
61
public static <T > java .util .Set <T > of (T ... entries ) {
62
62
switch (entries .length ) {
63
63
case 0 :
64
- return Set . of ();
64
+ return of ();
65
65
case 1 :
66
- return Set . of (entries [0 ]);
66
+ return of (entries [0 ]);
67
67
default :
68
68
return Collections .unmodifiableSet (new HashSet <>(Arrays .asList (entries )));
69
69
}
@@ -78,8 +78,14 @@ public static <T> java.util.Set<T> of(T... entries) {
78
78
* @throws NullPointerException if coll is null, or if it contains any nulls
79
79
* @since 10
80
80
*/
81
- @ SuppressWarnings ("unchecked" )
82
81
public static <T > java .util .Set <T > copyOf (Collection <? extends T > coll ) {
83
- return (java .util .Set <T >) Set .of (new HashSet <>(coll ).toArray ());
82
+ switch (coll .size ()) {
83
+ case 0 :
84
+ return Collections .emptySet ();
85
+ case 1 :
86
+ return Collections .singleton (coll .iterator ().next ());
87
+ default :
88
+ return Collections .unmodifiableSet (new HashSet <>(coll ));
89
+ }
84
90
}
85
91
}
You can’t perform that action at this time.
0 commit comments