From 1d0a9a1b2700b99a1cfc3e5f822ba7f9d737a4a3 Mon Sep 17 00:00:00 2001 From: Dan Hermann Date: Wed, 1 Apr 2020 10:27:18 -0500 Subject: [PATCH] Java8 implementations of collection initializers (#54183) --- .../elasticsearch/common/collect/List.java | 93 ++++++++++ .../org/elasticsearch/common/collect/Map.java | 163 ++++++++++++++++++ .../org/elasticsearch/common/collect/Set.java | 96 +++++++++++ .../elasticsearch/common/collect/List.java | 81 +++++++++ .../org/elasticsearch/common/collect/Map.java | 122 +++++++++++++ .../org/elasticsearch/common/collect/Set.java | 82 +++++++++ .../common/collect/ListTests.java | 70 ++++++++ .../common/collect/MapTests.java | 120 +++++++++++++ .../common/collect/SetTests.java | 70 ++++++++ 9 files changed, 897 insertions(+) create mode 100644 libs/core/src/main/java/org/elasticsearch/common/collect/List.java create mode 100644 libs/core/src/main/java/org/elasticsearch/common/collect/Map.java create mode 100644 libs/core/src/main/java/org/elasticsearch/common/collect/Set.java create mode 100644 libs/core/src/main/java11/org/elasticsearch/common/collect/List.java create mode 100644 libs/core/src/main/java11/org/elasticsearch/common/collect/Map.java create mode 100644 libs/core/src/main/java11/org/elasticsearch/common/collect/Set.java create mode 100644 libs/core/src/test/java/org/elasticsearch/common/collect/ListTests.java create mode 100644 libs/core/src/test/java/org/elasticsearch/common/collect/MapTests.java create mode 100644 libs/core/src/test/java/org/elasticsearch/common/collect/SetTests.java diff --git a/libs/core/src/main/java/org/elasticsearch/common/collect/List.java b/libs/core/src/main/java/org/elasticsearch/common/collect/List.java new file mode 100644 index 00000000000..e9a2a20bb1a --- /dev/null +++ b/libs/core/src/main/java/org/elasticsearch/common/collect/List.java @@ -0,0 +1,93 @@ +/* + * Licensed to Elasticsearch under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.elasticsearch.common.collect; + +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; + +public class List { + + /** + * Returns an unmodifiable list containing zero elements. + * + * @param the {@code List}'s element type + * @return an empty {@code List} + */ + public static java.util.List of() { + return Collections.emptyList(); + } + + /** + * Returns an unmodifiable list containing one element. + * + * @param the {@code List}'s element type + * @param e1 the single element + * @return a {@code List} containing the specified element + */ + public static java.util.List of(T e1) { + return Collections.singletonList(e1); + } + + /** + * Returns an unmodifiable list containing two elements. + * + * @param the {@code List}'s element type + * @param e1 the first element + * @param e2 the second element + * @return a {@code List} containing the specified element + */ + @SuppressWarnings("unchecked") + public static java.util.List of(T e1, T e2) { + return List.of((T[]) new Object[]{e1, e2}); + } + + /** + * Returns an unmodifiable list containing an arbitrary number of elements. + * + * @param entries the elements to be contained in the list + * @param the {@code List}'s element type + * @return an unmodifiable list containing the specified elements. + */ + @SafeVarargs + @SuppressWarnings("varargs") + public static java.util.List of(T... entries) { + switch (entries.length) { + case 0: + return List.of(); + case 1: + return List.of(entries[0]); + default: + return Collections.unmodifiableList(Arrays.asList(entries)); + } + } + + /** + * Returns an unmodifiable {@code List} containing the elements of the given {@code Collection} in iteration order. + * + * @param the {@code List}'s element type + * @param coll a {@code Collection} from which elements are drawn, must be non-null + * @return a {@code List} containing the elements of the given {@code Collection} + */ + @SuppressWarnings("unchecked") + public static java.util.List copyOf(Collection coll) { + return (java.util.List) List.of(coll.toArray()); + } +} diff --git a/libs/core/src/main/java/org/elasticsearch/common/collect/Map.java b/libs/core/src/main/java/org/elasticsearch/common/collect/Map.java new file mode 100644 index 00000000000..caa3aacd97f --- /dev/null +++ b/libs/core/src/main/java/org/elasticsearch/common/collect/Map.java @@ -0,0 +1,163 @@ +/* + * Licensed to Elasticsearch under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.elasticsearch.common.collect; + +import java.util.Collections; +import java.util.HashMap; + +public class Map { + + /** + * Returns an unmodifiable map containing one mapping. + */ + public static java.util.Map of() { + return Collections.emptyMap(); + } + + /** + * Returns an unmodifiable map containing one mapping. + */ + public static java.util.Map of(K k1, V v1) { + return Collections.singletonMap(k1, v1); + } + + /** + * Returns an unmodifiable map containing two mappings. + */ + public static java.util.Map of(K k1, V v1, K k2, V v2) { + return mapN(k1, v1, k2, v2); + } + + /** + * Returns an unmodifiable map containing three mappings. + */ + public static java.util.Map of(K k1, V v1, K k2, V v2, K k3, V v3) { + return mapN(k1, v1, k2, v2, k3, v3); + } + + /** + * Returns an unmodifiable map containing four mappings. + */ + public static java.util.Map of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4) { + return mapN(k1, v1, k2, v2, k3, v3, k4, v4); + } + + /** + * Returns an unmodifiable map containing five mappings. + */ + public static java.util.Map of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5) { + return mapN(k1, v1, k2, v2, k3, v3, k4, v4, k5, v5); + } + + /** + * Returns an unmodifiable map containing six mappings. + */ + public static java.util.Map of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, + K k6, V v6) { + return mapN(k1, v1, k2, v2, k3, v3, k4, v4, k5, v5, k6, v6); + } + + /** + * Returns an unmodifiable map containing seven mappings. + */ + public static java.util.Map of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, + K k6, V v6, K k7, V v7) { + return mapN(k1, v1, k2, v2, k3, v3, k4, v4, k5, v5, k6, v6, k7, v7); + } + + /** + * Returns an unmodifiable map containing eight mappings. + */ + public static java.util.Map of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, + K k6, V v6, K k7, V v7, K k8, V v8) { + return mapN(k1, v1, k2, v2, k3, v3, k4, v4, k5, v5, k6, v6, k7, v7, k8, v8); + } + + /** + * Returns an unmodifiable map containing nine mappings. + */ + public static java.util.Map of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, + K k6, V v6, K k7, V v7, K k8, V v8, K k9, V v9) { + return mapN(k1, v1, k2, v2, k3, v3, k4, v4, k5, v5, k6, v6, k7, v7, k8, v8, k9, v9); + } + + /** + * Returns an unmodifiable map containing ten mappings. + */ + public static java.util.Map of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, + K k6, V v6, K k7, V v7, K k8, V v8, K k9, V v9, K k10, V v10) { + return mapN(k1, v1, k2, v2, k3, v3, k4, v4, k5, v5, k6, v6, k7, v7, k8, v8, k9, v9, k10, v10); + } + + @SuppressWarnings("unchecked") + private static java.util.Map mapN(Object... objects) { + if (objects.length % 2 != 0) { + throw new IllegalStateException("Must provide an even number of arguments to Map::of method"); + } + switch (objects.length) { + case 0: + return Map.of(); + case 2: + return Map.of((K) objects[0], (V) objects[1]); + default: + HashMap map = new HashMap<>(); + for (int k = 0; k < objects.length / 2; k++) { + map.put((K) objects[k * 2], (V) objects[k * 2 + 1]); + } + return Collections.unmodifiableMap(map); + } + } + + /** + * Returns an unmodifiable map containing keys and values extracted from the given entries. + * + * @param the {@code Map}'s key type + * @param the {@code Map}'s value type + * @param entries {@code Map.Entry}s containing the keys and values from which the map is populated + * @return a {@code Map} containing the specified mappings + */ + @SafeVarargs + public static java.util.Map ofEntries(java.util.Map.Entry... entries) { + if (entries.length == 0) { + return Collections.emptyMap(); + } else if (entries.length == 1) { + return Collections.singletonMap(entries[0].getKey(), entries[0].getValue()); + } else { + HashMap map = new HashMap<>(); + for (java.util.Map.Entry entry : entries) { + map.put(entry.getKey(), entry.getValue()); + } + return Collections.unmodifiableMap(map); + } + } + + /** + * Returns an unmodifiable {@code Map} containing the entries of the given {@code Map}. + * + * @param the {@code Map}'s key type + * @param the {@code Map}'s value type + * @param map a {@code Map} from which entries are drawn, must be non-null + * @return a {@code Map} containing the entries of the given {@code Map} + */ + @SuppressWarnings({"unchecked", "rawtypes"}) + public static java.util.Map copyOf(java.util.Map map) { + return (java.util.Map) Map.ofEntries(map.entrySet().toArray(new java.util.Map.Entry[0])); + } +} diff --git a/libs/core/src/main/java/org/elasticsearch/common/collect/Set.java b/libs/core/src/main/java/org/elasticsearch/common/collect/Set.java new file mode 100644 index 00000000000..b6d9df65423 --- /dev/null +++ b/libs/core/src/main/java/org/elasticsearch/common/collect/Set.java @@ -0,0 +1,96 @@ +/* + * Licensed to Elasticsearch under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.elasticsearch.common.collect; + +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.HashSet; + +public class Set { + + /** + * Returns an unmodifiable set containing zero elements. + * + * @param the {@code Set}'s element type + * @return an empty {@code Set} + */ + public static java.util.Set of() { + return Collections.emptySet(); + } + + /** + * Returns an unmodifiable set containing one element. + * + * @param the {@code Set}'s element type + * @param e1 the single element + * @return a {@code Set} containing the specified element + */ + public static java.util.Set of(T e1) { + return Collections.singleton(e1); + } + + /** + * Returns an unmodifiable set containing two elements. + * + * @param the {@code Set}'s element type + * @param e1 the first element + * @param e2 the second element + * @return a {@code Set} containing the specified element + */ + @SuppressWarnings("unchecked") + public static java.util.Set of(T e1, T e2) { + return Set.of((T[]) new Object[]{e1, e2}); + } + + /** + * Returns an unmodifiable set containing an arbitrary number of elements. + * + * @param entries the elements to be contained in the set + * @param the {@code Set}'s element type + * @return an unmodifiable set containing the specified elements. + */ + @SafeVarargs + @SuppressWarnings("varargs") + public static java.util.Set of(T... entries) { + switch (entries.length) { + case 0: + return Set.of(); + case 1: + return Set.of(entries[0]); + default: + return Collections.unmodifiableSet(new HashSet<>(Arrays.asList(entries))); + } + } + + /** + * Returns an unmodifiable {@code Set} containing the elements of the given Collection. + * + * @param the {@code Set}'s element type + * @param coll a {@code Collection} from which elements are drawn, must be non-null + * @return a {@code Set} containing the elements of the given {@code Collection} + * @throws NullPointerException if coll is null, or if it contains any nulls + * @since 10 + */ + @SuppressWarnings("unchecked") + public static java.util.Set copyOf(Collection coll) { + return (java.util.Set) Set.of(new HashSet<>(coll).toArray()); + } +} diff --git a/libs/core/src/main/java11/org/elasticsearch/common/collect/List.java b/libs/core/src/main/java11/org/elasticsearch/common/collect/List.java new file mode 100644 index 00000000000..40534f11c05 --- /dev/null +++ b/libs/core/src/main/java11/org/elasticsearch/common/collect/List.java @@ -0,0 +1,81 @@ +/* + * Licensed to Elasticsearch under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.elasticsearch.common.collect; + +import java.util.Collection; + +public class List { + + /** + * Delegates to the Java9 {@code List.of()} method. + * + * @param the {@code List}'s element type + * @return an empty {@code List} + */ + public static java.util.List of() { + return java.util.List.of(); + } + + /** + * Delegates to the Java9 {@code List.of()} method. + * + * @param the {@code List}'s element type + * @param e1 the single element + * @return a {@code List} containing the specified element + */ + public static java.util.List of(T e1) { + return java.util.List.of(e1); + } + + /** + * Delegates to the Java9 {@code List.of()} method. + * + * @param the {@code List}'s element type + * @param e1 the single element + * @return a {@code List} containing the specified element + */ + public static java.util.List of(T e1, T e2) { + return java.util.List.of(e1, e2); + } + + /** + * Delegates to the Java9 {@code List.of()} method. + * + * @param entries the elements to be contained in the list + * @param the {@code List}'s element type + * @return an unmodifiable list containing the specified elements. + */ + @SafeVarargs + @SuppressWarnings("varargs") + public static java.util.List of(T... entries) { + return java.util.List.of(entries); + } + + /** + * Delegates to the Java9 {@code List.copyOf()} method. + * + * @param the {@code List}'s element type + * @param coll a {@code Collection} from which elements are drawn, must be non-null + * @return a {@code List} containing the elements of the given {@code Collection} + */ + public static java.util.List copyOf(Collection coll) { + return java.util.List.copyOf(coll); + } +} diff --git a/libs/core/src/main/java11/org/elasticsearch/common/collect/Map.java b/libs/core/src/main/java11/org/elasticsearch/common/collect/Map.java new file mode 100644 index 00000000000..f9badb14c2e --- /dev/null +++ b/libs/core/src/main/java11/org/elasticsearch/common/collect/Map.java @@ -0,0 +1,122 @@ +/* + * Licensed to Elasticsearch under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.elasticsearch.common.collect; + +public class Map { + + /** + * Delegates to the Java9 {@code Map.of()} method. + */ + public static java.util.Map of() { + return java.util.Map.of(); + } + + /** + * Delegates to the Java9 {@code Map.of()} method. + */ + public static java.util.Map of(K k1, V v1) { + return java.util.Map.of(k1, v1); + } + + /** + * Delegates to the Java9 {@code Map.of()} method. + */ + public static java.util.Map of(K k1, V v1, K k2, V v2) { + return java.util.Map.of(k1, v1, k2, v2); + } + + /** + * Delegates to the Java9 {@code Map.of()} method. + */ + public static java.util.Map of(K k1, V v1, K k2, V v2, K k3, V v3) { + return java.util.Map.of(k1, v1, k2, v2, k3, v3); + } + + /** + * Delegates to the Java9 {@code Map.of()} method. + */ + public static java.util.Map of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4) { + return java.util.Map.of(k1, v1, k2, v2, k3, v3, k4, v4); + } + + /** + * Delegates to the Java9 {@code Map.of()} method. + */ + public static java.util.Map of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5) { + return java.util.Map.of(k1, v1, k2, v2, k3, v3, k4, v4, k5, v5); + } + + /** + * Delegates to the Java9 {@code Map.of()} method. + */ + public static java.util.Map of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, + K k6, V v6) { + return java.util.Map.of(k1, v1, k2, v2, k3, v3, k4, v4, k5, v5, k6, v6); + } + + /** + * Delegates to the Java9 {@code Map.of()} method. + */ + public static java.util.Map of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, + K k6, V v6, K k7, V v7) { + return java.util.Map.of(k1, v1, k2, v2, k3, v3, k4, v4, k5, v5, k6, v6, k7, v7); + } + + /** + * Delegates to the Java9 {@code Map.of()} method. + */ + public static java.util.Map of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, + K k6, V v6, K k7, V v7, K k8, V v8) { + return java.util.Map.of(k1, v1, k2, v2, k3, v3, k4, v4, k5, v5, k6, v6, k7, v7, k8, v8); + } + + /** + * Delegates to the Java9 {@code Map.of()} method. + */ + public static java.util.Map of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, + K k6, V v6, K k7, V v7, K k8, V v8, K k9, V v9) { + return java.util.Map.of(k1, v1, k2, v2, k3, v3, k4, v4, k5, v5, k6, v6, k7, v7, k8, v8, k9, v9); + } + + /** + * Delegates to the Java9 {@code Map.of()} method. + */ + public static java.util.Map of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, + K k6, V v6, K k7, V v7, K k8, V v8, K k9, V v9, K k10, V v10) { + return java.util.Map.of(k1, v1, k2, v2, k3, v3, k4, v4, k5, v5, k6, v6, k7, v7, k8, v8, k9, v9, k10, v10); + } + + /** + * Delegates to the Java9 {@code Map.ofEntries()} method. + */ + @SafeVarargs + @SuppressWarnings("varargs") + public static java.util.Map ofEntries(java.util.Map.Entry... entries) { + return java.util.Map.ofEntries(entries); + } + + /** + * Delegates to the Java10 {@code Map.copyOf()} method. + */ + public static java.util.Map copyOf(java.util.Map map) { + return java.util.Map.copyOf(map); + } + +} diff --git a/libs/core/src/main/java11/org/elasticsearch/common/collect/Set.java b/libs/core/src/main/java11/org/elasticsearch/common/collect/Set.java new file mode 100644 index 00000000000..9594f22fb39 --- /dev/null +++ b/libs/core/src/main/java11/org/elasticsearch/common/collect/Set.java @@ -0,0 +1,82 @@ +/* + * Licensed to Elasticsearch under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.elasticsearch.common.collect; + +import java.util.Collection; + +public class Set { + + /** + * Delegates to the Java9 {@code Set.of()} method. + * + * @param the {@code Set}'s element type + * @return an empty {@code Set} + */ + public static java.util.Set of() { + return java.util.Set.of(); + } + + /** + * Delegates to the Java9 {@code Set.of()} method. + * + * @param the {@code Set}'s element type + * @param e1 the single element + * @return a {@code Set} containing the specified element + */ + public static java.util.Set of(T e1) { + return java.util.Set.of(e1); + } + + /** + * Delegates to the Java9 {@code Set.of()} method. + * + * @param the {@code Set}'s element type + * @param e1 the first element + * @param e2 the second element + * @return a {@code Set} containing the specified element + */ + public static java.util.Set of(T e1, T e2) { + return java.util.Set.of(e1, e2); + } + + /** + * Delegates to the Java9 {@code Set.of()} method. + * + * @param entries the elements to be contained in the set + * @param the {@code Set}'s element type + * @return an unmodifiable set containing the specified elements. + */ + @SafeVarargs + @SuppressWarnings("varargs") + public static java.util.Set of(T... entries) { + return java.util.Set.of(entries); + } + + /** + * Delegates to the Java10 {@code Set.copyOf} method. + * + * @param the {@code Set}'s element type + * @param coll a {@code Collection} from which elements are drawn, must be non-null + * @return a {@code Set} containing the elements of the given {@code Collection} + */ + public static java.util.Set copyOf(Collection coll) { + return java.util.Set.copyOf(coll); + } +} diff --git a/libs/core/src/test/java/org/elasticsearch/common/collect/ListTests.java b/libs/core/src/test/java/org/elasticsearch/common/collect/ListTests.java new file mode 100644 index 00000000000..719aa73c250 --- /dev/null +++ b/libs/core/src/test/java/org/elasticsearch/common/collect/ListTests.java @@ -0,0 +1,70 @@ +/* + * Licensed to Elasticsearch under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.elasticsearch.common.collect; + +import org.elasticsearch.test.ESTestCase; + +import java.util.Arrays; +import java.util.Collection; + +import static org.hamcrest.CoreMatchers.equalTo; + +public class ListTests extends ESTestCase { + + public void testStringListOfZero() { + final String[] strings = {}; + final java.util.List stringsList = List.of(strings); + assertThat(stringsList.size(), equalTo(strings.length)); + assertTrue(stringsList.containsAll(Arrays.asList(strings))); + expectThrows(UnsupportedOperationException.class, () -> stringsList.add("foo")); + } + + public void testStringListOfOne() { + final String[] strings = {"foo"}; + final java.util.List stringsList = List.of(strings); + assertThat(stringsList.size(), equalTo(strings.length)); + assertTrue(stringsList.containsAll(Arrays.asList(strings))); + expectThrows(UnsupportedOperationException.class, () -> stringsList.add("foo")); + } + + public void testStringListOfTwo() { + final String[] strings = {"foo", "bar"}; + final java.util.List stringsList = List.of(strings); + assertThat(stringsList.size(), equalTo(strings.length)); + assertTrue(stringsList.containsAll(Arrays.asList(strings))); + expectThrows(UnsupportedOperationException.class, () -> stringsList.add("foo")); + } + + public void testStringListOfN() { + final String[] strings = {"foo", "bar", "baz"}; + final java.util.List stringsList = List.of(strings); + assertThat(stringsList.size(), equalTo(strings.length)); + assertTrue(stringsList.containsAll(Arrays.asList(strings))); + expectThrows(UnsupportedOperationException.class, () -> stringsList.add("foo")); + } + + public void testCopyOf() { + final Collection coll = Arrays.asList("foo", "bar", "baz"); + final java.util.List copy = List.copyOf(coll); + assertThat(coll.size(), equalTo(copy.size())); + assertTrue(copy.containsAll(coll)); + expectThrows(UnsupportedOperationException.class, () -> copy.add("foo")); + } +} diff --git a/libs/core/src/test/java/org/elasticsearch/common/collect/MapTests.java b/libs/core/src/test/java/org/elasticsearch/common/collect/MapTests.java new file mode 100644 index 00000000000..84034fcbec4 --- /dev/null +++ b/libs/core/src/test/java/org/elasticsearch/common/collect/MapTests.java @@ -0,0 +1,120 @@ +/* + * Licensed to Elasticsearch under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.elasticsearch.common.collect; + +import org.elasticsearch.test.ESTestCase; + +import java.util.AbstractMap; + +import static org.hamcrest.CoreMatchers.equalTo; + +public class MapTests extends ESTestCase { + + private static final String[] numbers = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"}; + + public void testMapOfZero() { + final java.util.Map map = Map.of(); + validateMapContents(map, 0); + } + + public void testMapOfOne() { + final java.util.Map map = Map.of(numbers[0], 0); + validateMapContents(map, 1); + } + + public void testMapOfTwo() { + final java.util.Map map = Map.of(numbers[0], 0, numbers[1], 1); + validateMapContents(map, 2); + } + + public void testMapOfThree() { + final java.util.Map map = Map.of(numbers[0], 0, numbers[1], 1, numbers[2], 2); + validateMapContents(map, 3); + } + + public void testMapOfFour() { + final java.util.Map map = Map.of(numbers[0], 0, numbers[1], 1, numbers[2], 2, numbers[3], 3); + validateMapContents(map, 4); + } + + public void testMapOfFive() { + final java.util.Map map = Map.of(numbers[0], 0, numbers[1], 1, numbers[2], 2, numbers[3], 3, + numbers[4], 4); + validateMapContents(map, 5); + } + + public void testMapOfSix() { + final java.util.Map map = Map.of(numbers[0], 0, numbers[1], 1, numbers[2], 2, numbers[3], 3, + numbers[4], 4, numbers[5], 5); + validateMapContents(map, 6); + } + + public void testMapOfSeven() { + final java.util.Map map = Map.of(numbers[0], 0, numbers[1], 1, numbers[2], 2, numbers[3], 3, + numbers[4], 4, numbers[5], 5, numbers[6], 6); + validateMapContents(map, 7); + } + + public void testMapOfEight() { + final java.util.Map map = Map.of(numbers[0], 0, numbers[1], 1, numbers[2], 2, numbers[3], 3, + numbers[4], 4, numbers[5], 5, numbers[6], 6, numbers[7], 7); + validateMapContents(map, 8); + } + + public void testMapOfNine() { + final java.util.Map map = Map.of(numbers[0], 0, numbers[1], 1, numbers[2], 2, numbers[3], 3, + numbers[4], 4, numbers[5], 5, numbers[6], 6, numbers[7], 7, numbers[8], 8); + validateMapContents(map, 9); + } + + public void testMapOfTen() { + final java.util.Map map = Map.of(numbers[0], 0, numbers[1], 1, numbers[2], 2, numbers[3], 3, + numbers[4], 4, numbers[5], 5, numbers[6], 6, numbers[7], 7, numbers[8], 8, numbers[9], 9); + validateMapContents(map, 10); + } + + private static void validateMapContents(java.util.Map map, int size) { + assertThat(map.size(), equalTo(size)); + for (int k = 0; k < map.size(); k++) { + assertEquals(Integer.class, map.get(numbers[k]).getClass()); + assertThat(k, equalTo(map.get(numbers[k]))); + } + expectThrows(UnsupportedOperationException.class, () -> map.put("foo", 42)); + } + + public void testOfEntries() { + final java.util.Map map = Map.ofEntries( + new AbstractMap.SimpleEntry<>(numbers[0], 0), + new AbstractMap.SimpleEntry<>(numbers[1], 1), + new AbstractMap.SimpleEntry<>(numbers[2], 2) + ); + validateMapContents(map, 3); + } + + public void testCopyOf() { + final java.util.Map map1 = Map.of("fooK", "fooV", "barK", "barV", "bazK", "bazV"); + final java.util.Map copy = Map.copyOf(map1); + assertThat(map1.size(), equalTo(copy.size())); + for (java.util.Map.Entry entry : map1.entrySet()) { + assertEquals(entry.getValue(), copy.get(entry.getKey())); + } + expectThrows(UnsupportedOperationException.class, () -> copy.put("foo", "bar")); + } +} diff --git a/libs/core/src/test/java/org/elasticsearch/common/collect/SetTests.java b/libs/core/src/test/java/org/elasticsearch/common/collect/SetTests.java new file mode 100644 index 00000000000..89f5ee23a0c --- /dev/null +++ b/libs/core/src/test/java/org/elasticsearch/common/collect/SetTests.java @@ -0,0 +1,70 @@ +/* + * Licensed to Elasticsearch under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.elasticsearch.common.collect; + +import org.elasticsearch.test.ESTestCase; + +import java.util.Arrays; +import java.util.Collection; + +import static org.hamcrest.CoreMatchers.equalTo; + +public class SetTests extends ESTestCase { + + public void testStringSetOfZero() { + final String[] strings = {}; + final java.util.Set stringsSet = Set.of(strings); + assertThat(stringsSet.size(), equalTo(strings.length)); + assertTrue(stringsSet.containsAll(Arrays.asList(strings))); + expectThrows(UnsupportedOperationException.class, () -> stringsSet.add("foo")); + } + + public void testStringSetOfOne() { + final String[] strings = {"foo"}; + final java.util.Set stringsSet = Set.of(strings); + assertThat(stringsSet.size(), equalTo(strings.length)); + assertTrue(stringsSet.containsAll(Arrays.asList(strings))); + expectThrows(UnsupportedOperationException.class, () -> stringsSet.add("foo")); + } + + public void testStringSetOfTwo() { + final String[] strings = {"foo", "bar"}; + final java.util.Set stringsSet = Set.of(strings); + assertThat(stringsSet.size(), equalTo(strings.length)); + assertTrue(stringsSet.containsAll(Arrays.asList(strings))); + expectThrows(UnsupportedOperationException.class, () -> stringsSet.add("foo")); + } + + public void testStringSetOfN() { + final String[] strings = {"foo", "bar", "baz"}; + final java.util.Set stringsSet = Set.of(strings); + assertThat(stringsSet.size(), equalTo(strings.length)); + assertTrue(stringsSet.containsAll(Arrays.asList(strings))); + expectThrows(UnsupportedOperationException.class, () -> stringsSet.add("foo")); + } + + public void testCopyOf() { + final Collection coll = Arrays.asList("foo", "bar", "baz"); + final java.util.Set copy = Set.copyOf(coll); + assertThat(coll.size(), equalTo(copy.size())); + assertTrue(copy.containsAll(coll)); + expectThrows(UnsupportedOperationException.class, () -> copy.add("foo")); + } +}