Skip to content

Commit

Permalink
Removed java11 source folders since JDK-11 is the baseline now (#2898)
Browse files Browse the repository at this point in the history
Signed-off-by: Andriy Redko <andriy.redko@aiven.io>
  • Loading branch information
reta committed Apr 18, 2022
1 parent fc378c7 commit d61d170
Show file tree
Hide file tree
Showing 10 changed files with 55 additions and 674 deletions.
34 changes: 11 additions & 23 deletions libs/core/src/main/java/org/opensearch/common/collect/List.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,48 +32,44 @@

package org.opensearch.common.collect;

import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;

public class List {

/**
* Returns an unmodifiable list containing zero elements.
* 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 Collections.emptyList();
return java.util.List.of();
}

/**
* Returns an unmodifiable list containing one element.
* 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 Collections.singletonList(e1);
return java.util.List.of(e1);
}

/**
* Returns an unmodifiable list containing two elements.
* Delegates to the Java9 {@code List.of()} method.
*
* @param <T> the {@code List}'s element type
* @param e1 the first element
* @param e2 the second element
* @param e1 the single 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 });
return java.util.List.of(e1, e2);
}

/**
* Returns an unmodifiable list containing an arbitrary number of elements.
* 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
Expand All @@ -82,25 +78,17 @@ public static <T> java.util.List<T> of(T e1, T e2) {
@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));
}
return java.util.List.of(entries);
}

/**
* Returns an unmodifiable {@code List} containing the elements of the given {@code Collection} in iteration order.
* 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}
*/
@SuppressWarnings("unchecked")
public static <T> java.util.List<T> copyOf(Collection<? extends T> coll) {
return (java.util.List<T>) List.of(coll.toArray());
return java.util.List.copyOf(coll);
}
}
102 changes: 30 additions & 72 deletions libs/core/src/main/java/org/opensearch/common/collect/Map.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,70 +32,66 @@

package org.opensearch.common.collect;

import java.util.AbstractMap;
import java.util.Collections;
import java.util.HashMap;

public class Map {

/**
* Returns an unmodifiable map containing one mapping.
* Delegates to the Java9 {@code Map.of()} method.
*/
public static <K, V> java.util.Map<K, V> of() {
return Collections.emptyMap();
return java.util.Map.of();
}

/**
* Returns an unmodifiable map containing one mapping.
* Delegates to the Java9 {@code Map.of()} method.
*/
public static <K, V> java.util.Map<K, V> of(K k1, V v1) {
return Collections.singletonMap(k1, v1);
return java.util.Map.of(k1, v1);
}

/**
* Returns an unmodifiable map containing two mappings.
* 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 mapN(k1, v1, k2, v2);
return java.util.Map.of(k1, v1, k2, v2);
}

/**
* Returns an unmodifiable map containing three mappings.
* 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 mapN(k1, v1, k2, v2, k3, v3);
return java.util.Map.of(k1, v1, k2, v2, k3, v3);
}

/**
* Returns an unmodifiable map containing four mappings.
* 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 mapN(k1, v1, k2, v2, k3, v3, k4, v4);
return java.util.Map.of(k1, v1, k2, v2, k3, v3, k4, v4);
}

/**
* Returns an unmodifiable map containing five mappings.
* 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 mapN(k1, v1, k2, v2, k3, v3, k4, v4, k5, v5);
return java.util.Map.of(k1, v1, k2, v2, k3, v3, k4, v4, k5, v5);
}

/**
* Returns an unmodifiable map containing six mappings.
* 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 mapN(k1, v1, k2, v2, k3, v3, k4, v4, k5, v5, k6, v6);
return java.util.Map.of(k1, v1, k2, v2, k3, v3, k4, v4, k5, v5, k6, v6);
}

/**
* Returns an unmodifiable map containing seven mappings.
* 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 mapN(k1, v1, k2, v2, k3, v3, k4, v4, k5, v5, k6, v6, k7, v7);
return java.util.Map.of(k1, v1, k2, v2, k3, v3, k4, v4, k5, v5, k6, v6, k7, v7);
}

/**
* Returns an unmodifiable map containing eight mappings.
* Delegates to the Java9 {@code Map.of()} method.
*/
public static <K, V> java.util.Map<K, V> of(
K k1,
Expand All @@ -115,11 +111,11 @@ public static <K, V> java.util.Map<K, V> of(
K k8,
V v8
) {
return mapN(k1, v1, k2, v2, k3, v3, k4, v4, k5, v5, k6, v6, k7, v7, k8, v8);
return java.util.Map.of(k1, v1, k2, v2, k3, v3, k4, v4, k5, v5, k6, v6, k7, v7, k8, v8);
}

/**
* Returns an unmodifiable map containing nine mappings.
* Delegates to the Java9 {@code Map.of()} method.
*/
public static <K, V> java.util.Map<K, V> of(
K k1,
Expand All @@ -141,11 +137,11 @@ public static <K, V> java.util.Map<K, V> of(
K k9,
V v9
) {
return mapN(k1, v1, k2, v2, k3, v3, k4, v4, k5, v5, k6, v6, k7, v7, k8, v8, k9, v9);
return java.util.Map.of(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.
* Delegates to the Java9 {@code Map.of()} method.
*/
public static <K, V> java.util.Map<K, V> of(
K k1,
Expand All @@ -169,68 +165,30 @@ public static <K, V> java.util.Map<K, V> of(
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);
}
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);
}

/**
* 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
* 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) {
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);
}
return java.util.Map.ofEntries(entries);
}

/**
* Returns an unmodifiable Map.Entry for the provided key and value.
* Delegates to the Java9 {@code Map.entry()} method.
*/
public static <K, V> java.util.Map.Entry<K, V> entry(K k, V v) {
return new AbstractMap.SimpleImmutableEntry<>(k, v);
return java.util.Map.entry(k, v);
}

/**
* 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}
* Delegates to the Java10 {@code Map.copyOf()} method.
*/
@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]));
return java.util.Map.copyOf(map);
}

}
36 changes: 11 additions & 25 deletions libs/core/src/main/java/org/opensearch/common/collect/Set.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,49 +32,45 @@

package org.opensearch.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.
* 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 Collections.emptySet();
return java.util.Set.of();
}

/**
* Returns an unmodifiable set containing one element.
* 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 Collections.singleton(e1);
return java.util.Set.of(e1);
}

/**
* Returns an unmodifiable set containing two elements.
* 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
*/
@SuppressWarnings("unchecked")
public static <T> java.util.Set<T> of(T e1, T e2) {
return Set.of((T[]) new Object[] { e1, e2 });
return java.util.Set.of(e1, e2);
}

/**
* Returns an unmodifiable set containing an arbitrary number of elements.
* 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
Expand All @@ -83,27 +79,17 @@ public static <T> java.util.Set<T> of(T e1, T e2) {
@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)));
}
return java.util.Set.of(entries);
}

/**
* Returns an unmodifiable {@code Set} containing the elements of the given Collection.
* Delegates to the Java10 {@code Set.copyOf} method.
*
* @param <T> the {@code Set}'s element type
* @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());
return java.util.Set.copyOf(coll);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,15 @@

/**
* Simple utility methods for file and stream copying.
* All copy methods use a block size of 4096 bytes,
* and close all affected streams when done.
* All copy methods close all affected streams when done.
* <p>
* Mainly for use within the framework,
* but also useful for application code.
*/
public class Streams {
public abstract class Streams {

private static final ThreadLocal<byte[]> buffer = ThreadLocal.withInitial(() -> new byte[8 * 1024]);

private Streams() {

}

/**
* Copy the contents of the given InputStream to the given OutputStream. Optionally, closes both streams when done.
*
Expand Down
Loading

0 comments on commit d61d170

Please sign in to comment.