Java8 implementations of collection initializers (#54183)

This commit is contained in:
Dan Hermann 2020-04-01 10:27:18 -05:00 committed by GitHub
parent b43eb5ac32
commit 1d0a9a1b27
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 897 additions and 0 deletions

View File

@ -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 <T> the {@code List}'s element type
* @return an empty {@code List}
*/
public static <T> java.util.List<T> of() {
return Collections.emptyList();
}
/**
* Returns an unmodifiable list containing one element.
*
* @param <T> the {@code List}'s element type
* @param e1 the single element
* @return a {@code List} containing the specified element
*/
public static <T> java.util.List<T> of(T e1) {
return Collections.singletonList(e1);
}
/**
* Returns an unmodifiable list containing two elements.
*
* @param <T> 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 <T> java.util.List<T> 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 <T> the {@code List}'s element type
* @return an unmodifiable list containing the specified elements.
*/
@SafeVarargs
@SuppressWarnings("varargs")
public static <T> java.util.List<T> 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 <T> 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 <T> java.util.List<T> copyOf(Collection<? extends T> coll) {
return (java.util.List<T>) List.of(coll.toArray());
}
}

View File

@ -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 <K, V> java.util.Map<K, V> of() {
return Collections.emptyMap();
}
/**
* Returns an unmodifiable map containing one mapping.
*/
public static <K, V> java.util.Map<K, V> of(K k1, V v1) {
return Collections.singletonMap(k1, v1);
}
/**
* Returns an unmodifiable map containing two mappings.
*/
public static <K, V> java.util.Map<K, V> of(K k1, V v1, K k2, V v2) {
return mapN(k1, v1, k2, v2);
}
/**
* Returns an unmodifiable map containing three mappings.
*/
public static <K, V> java.util.Map<K, V> 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 <K, V> java.util.Map<K, V> 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 <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) {
return mapN(k1, v1, k2, v2, k3, v3, k4, v4, k5, v5);
}
/**
* Returns an unmodifiable map containing six mappings.
*/
public 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,
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 <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,
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 <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,
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 <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,
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 <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,
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 <K, V> java.util.Map<K, V> 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<K, V> 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 <K> the {@code Map}'s key type
* @param <V> 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 <K, V> java.util.Map<K, V> ofEntries(java.util.Map.Entry<? extends K, ? extends V>... entries) {
if (entries.length == 0) {
return Collections.emptyMap();
} else if (entries.length == 1) {
return Collections.singletonMap(entries[0].getKey(), entries[0].getValue());
} else {
HashMap<K, V> map = new HashMap<>();
for (java.util.Map.Entry<? extends K, ? extends V> 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 <K> the {@code Map}'s key type
* @param <V> 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 <K, V> java.util.Map<K, V> copyOf(java.util.Map<? extends K, ? extends V> map) {
return (java.util.Map<K, V>) Map.ofEntries(map.entrySet().toArray(new java.util.Map.Entry[0]));
}
}

View File

@ -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 <T> the {@code Set}'s element type
* @return an empty {@code Set}
*/
public static <T> java.util.Set<T> of() {
return Collections.emptySet();
}
/**
* Returns an unmodifiable set containing one element.
*
* @param <T> the {@code Set}'s element type
* @param e1 the single element
* @return a {@code Set} containing the specified element
*/
public static <T> java.util.Set<T> of(T e1) {
return Collections.singleton(e1);
}
/**
* Returns an unmodifiable set containing two elements.
*
* @param <T> 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 <T> java.util.Set<T> 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 <T> the {@code Set}'s element type
* @return an unmodifiable set containing the specified elements.
*/
@SafeVarargs
@SuppressWarnings("varargs")
public static <T> java.util.Set<T> 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 <T> 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 <T> java.util.Set<T> copyOf(Collection<? extends T> coll) {
return (java.util.Set<T>) Set.of(new HashSet<>(coll).toArray());
}
}

View File

@ -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 <T> the {@code List}'s element type
* @return an empty {@code List}
*/
public static <T> java.util.List<T> of() {
return java.util.List.of();
}
/**
* Delegates to the Java9 {@code List.of()} method.
*
* @param <T> the {@code List}'s element type
* @param e1 the single element
* @return a {@code List} containing the specified element
*/
public static <T> java.util.List<T> of(T e1) {
return java.util.List.of(e1);
}
/**
* Delegates to the Java9 {@code List.of()} method.
*
* @param <T> the {@code List}'s element type
* @param e1 the single element
* @return a {@code List} containing the specified element
*/
public static <T> java.util.List<T> 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 <T> the {@code List}'s element type
* @return an unmodifiable list containing the specified elements.
*/
@SafeVarargs
@SuppressWarnings("varargs")
public static <T> java.util.List<T> of(T... entries) {
return java.util.List.of(entries);
}
/**
* Delegates to the Java9 {@code List.copyOf()} method.
*
* @param <T> 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 <T> java.util.List<T> copyOf(Collection<? extends T> coll) {
return java.util.List.copyOf(coll);
}
}

View File

@ -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 <K, V> java.util.Map<K, V> of() {
return java.util.Map.of();
}
/**
* Delegates to the Java9 {@code Map.of()} method.
*/
public static <K, V> java.util.Map<K, V> of(K k1, V v1) {
return java.util.Map.of(k1, v1);
}
/**
* Delegates to the Java9 {@code Map.of()} method.
*/
public static <K, V> java.util.Map<K, V> 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 <K, V> java.util.Map<K, V> 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 <K, V> java.util.Map<K, V> 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 <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) {
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 <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,
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 <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,
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 <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,
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 <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,
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 <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,
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 <K, V> java.util.Map<K, V> ofEntries(java.util.Map.Entry<? extends K, ? extends V>... entries) {
return java.util.Map.ofEntries(entries);
}
/**
* Delegates to the Java10 {@code Map.copyOf()} method.
*/
public static <K, V> java.util.Map<K, V> copyOf(java.util.Map<? extends K, ? extends V> map) {
return java.util.Map.copyOf(map);
}
}

View File

@ -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 <T> the {@code Set}'s element type
* @return an empty {@code Set}
*/
public static <T> java.util.Set<T> of() {
return java.util.Set.of();
}
/**
* Delegates to the Java9 {@code Set.of()} method.
*
* @param <T> the {@code Set}'s element type
* @param e1 the single element
* @return a {@code Set} containing the specified element
*/
public static <T> java.util.Set<T> of(T e1) {
return java.util.Set.of(e1);
}
/**
* Delegates to the Java9 {@code Set.of()} method.
*
* @param <T> 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 <T> java.util.Set<T> 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 <T> the {@code Set}'s element type
* @return an unmodifiable set containing the specified elements.
*/
@SafeVarargs
@SuppressWarnings("varargs")
public static <T> java.util.Set<T> of(T... entries) {
return java.util.Set.of(entries);
}
/**
* Delegates to the Java10 {@code Set.copyOf} method.
*
* @param <T> 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 <T> java.util.Set<T> copyOf(Collection<? extends T> coll) {
return java.util.Set.copyOf(coll);
}
}

View File

@ -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<String> 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<String> 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<String> 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<String> 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<String> coll = Arrays.asList("foo", "bar", "baz");
final java.util.List<String> copy = List.copyOf(coll);
assertThat(coll.size(), equalTo(copy.size()));
assertTrue(copy.containsAll(coll));
expectThrows(UnsupportedOperationException.class, () -> copy.add("foo"));
}
}

View File

@ -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<String, Integer> map = Map.of();
validateMapContents(map, 0);
}
public void testMapOfOne() {
final java.util.Map<String, Integer> map = Map.of(numbers[0], 0);
validateMapContents(map, 1);
}
public void testMapOfTwo() {
final java.util.Map<String, Integer> map = Map.of(numbers[0], 0, numbers[1], 1);
validateMapContents(map, 2);
}
public void testMapOfThree() {
final java.util.Map<String, Integer> map = Map.of(numbers[0], 0, numbers[1], 1, numbers[2], 2);
validateMapContents(map, 3);
}
public void testMapOfFour() {
final java.util.Map<String, Integer> 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<String, Integer> 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<String, Integer> 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<String, Integer> 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<String, Integer> 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<String, Integer> 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<String, Integer> 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<String, Integer> 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<String, Integer> 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<String, String> map1 = Map.of("fooK", "fooV", "barK", "barV", "bazK", "bazV");
final java.util.Map<String, String> copy = Map.copyOf(map1);
assertThat(map1.size(), equalTo(copy.size()));
for (java.util.Map.Entry<String, String> entry : map1.entrySet()) {
assertEquals(entry.getValue(), copy.get(entry.getKey()));
}
expectThrows(UnsupportedOperationException.class, () -> copy.put("foo", "bar"));
}
}

View File

@ -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<String> 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<String> 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<String> 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<String> 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<String> coll = Arrays.asList("foo", "bar", "baz");
final java.util.Set<String> copy = Set.copyOf(coll);
assertThat(coll.size(), equalTo(copy.size()));
assertTrue(copy.containsAll(coll));
expectThrows(UnsupportedOperationException.class, () -> copy.add("foo"));
}
}