Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Java 11 collections conveniences everywhere #41399

Merged
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import org.elasticsearch.common.xcontent.XContentParser;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
Expand Down Expand Up @@ -148,22 +147,20 @@ private FileStructure(int numLinesAnalyzed, int numMessagesAnalyzed, String samp
this.format = Objects.requireNonNull(format);
this.multilineStartPattern = multilineStartPattern;
this.excludeLinesPattern = excludeLinesPattern;
this.columnNames = (columnNames == null) ? null : Collections.unmodifiableList(new ArrayList<>(columnNames));
this.columnNames = (columnNames == null) ? null : List.copyOf(columnNames);
this.hasHeaderRow = hasHeaderRow;
this.delimiter = delimiter;
this.quote = quote;
this.shouldTrimFields = shouldTrimFields;
this.grokPattern = grokPattern;
this.timestampField = timestampField;
this.jodaTimestampFormats =
(jodaTimestampFormats == null) ? null : Collections.unmodifiableList(new ArrayList<>(jodaTimestampFormats));
this.javaTimestampFormats =
(javaTimestampFormats == null) ? null : Collections.unmodifiableList(new ArrayList<>(javaTimestampFormats));
this.jodaTimestampFormats = (jodaTimestampFormats == null) ? null : List.copyOf(jodaTimestampFormats);
this.javaTimestampFormats = (javaTimestampFormats == null) ? null : List.copyOf(javaTimestampFormats);
this.needClientTimezone = needClientTimezone;
this.mappings = Collections.unmodifiableSortedMap(new TreeMap<>(mappings));
this.ingestPipeline = (ingestPipeline == null) ? null : Collections.unmodifiableMap(new LinkedHashMap<>(ingestPipeline));
this.fieldStats = Collections.unmodifiableSortedMap(new TreeMap<>(fieldStats));
this.explanation = (explanation == null) ? null : Collections.unmodifiableList(new ArrayList<>(explanation));
this.explanation = (explanation == null) ? null : List.copyOf(explanation);
}

public int getNumLinesAnalyzed() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
Expand All @@ -44,7 +43,7 @@ public Set<ApplicationPrivilege> getPrivileges() {
}

public GetPrivilegesResponse(Collection<ApplicationPrivilege> privileges) {
this.privileges = Collections.unmodifiableSet(new HashSet<>(privileges));
this.privileges = Set.copyOf(privileges);
}

public static GetPrivilegesResponse fromXContent(XContentParser parser) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@
import org.elasticsearch.common.xcontent.XContentParserUtils;

import java.io.IOException;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
Expand Down Expand Up @@ -99,7 +99,7 @@ public int hashCode() {
(constructorObjects) -> {
int i = 0;
final String username = (String) constructorObjects[i++];
final Collection<String> roles = (Collection<String>) constructorObjects[i++];
final List<String> roles = (List<String>) constructorObjects[i++];
final Map<String, Object> metadata = (Map<String, Object>) constructorObjects[i++];
final Boolean enabled = (Boolean) constructorObjects[i++];
final String fullName = (String) constructorObjects[i++];
Expand All @@ -120,13 +120,13 @@ protected static final class ParsedUser {
protected User user;
protected boolean enabled;

public ParsedUser(String username, Collection<String> roles, Map<String, Object> metadata, Boolean enabled,
public ParsedUser(String username, List<String> roles, Map<String, Object> metadata, Boolean enabled,
@Nullable String fullName, @Nullable String email) {
String checkedUsername = username = Objects.requireNonNull(username, "`username` is required, cannot be null");
Collection<String> checkedRoles = Collections.unmodifiableSet(new HashSet<>(
Objects.requireNonNull(roles, "`roles` is required, cannot be null. Pass an empty Collection instead.")));
Map<String, Object> checkedMetadata = Collections
.unmodifiableMap(Objects.requireNonNull(metadata, "`metadata` is required, cannot be null. Pass an empty map instead."));
String checkedUsername = Objects.requireNonNull(username, "`username` is required, cannot be null");
List<String> checkedRoles =
List.copyOf(Objects.requireNonNull(roles, "`roles` is required, cannot be null. Pass an empty Collection instead."));
jasontedor marked this conversation as resolved.
Show resolved Hide resolved
Map<String, Object> checkedMetadata = Collections.unmodifiableMap(
Objects.requireNonNull(metadata, "`metadata` is required, cannot be null. Pass an empty map instead."));
this.user = new User(checkedUsername, checkedRoles, checkedMetadata, fullName, email);
this.enabled = enabled;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@
import org.elasticsearch.common.xcontent.XContentBuilder;

import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

Expand Down Expand Up @@ -53,7 +51,7 @@ public abstract class CompositeRoleMapperExpression implements RoleMapperExpress
assert name != null : "field name cannot be null";
assert elements != null : "at least one field expression is required";
this.name = name;
this.elements = Collections.unmodifiableList(Arrays.asList(elements));
this.elements = List.of(elements);
}

public String getName() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@
import org.elasticsearch.common.xcontent.XContentBuilder;

import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

Expand Down Expand Up @@ -53,7 +51,7 @@ public FieldRoleMapperExpression(final String field, final Object... values) {
throw new IllegalArgumentException("null or empty values for field (" + field + ")");
}
this.field = field;
this.values = Collections.unmodifiableList(Arrays.asList(values));
this.values = List.of(values);
}

public String getField() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,10 @@
import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.Strings;

import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

/**
* A user to be utilized with security APIs.
Expand All @@ -36,7 +34,7 @@
public final class User {

private final String username;
private final Set<String> roles;
private final List<String> roles;
private final Map<String, Object> metadata;
@Nullable private final String fullName;
@Nullable private final String email;
Expand All @@ -50,13 +48,12 @@ public final class User {
* @param fullName the full name of the user that may be used for display purposes
* @param email the email address of the user
*/
public User(String username, Collection<String> roles, Map<String, Object> metadata, @Nullable String fullName,
public User(String username, List<String> roles, Map<String, Object> metadata, @Nullable String fullName,
@Nullable String email) {
this.username = username = Objects.requireNonNull(username, "`username` is required, cannot be null");
this.roles = Collections.unmodifiableSet(new HashSet<>(
Objects.requireNonNull(roles, "`roles` is required, cannot be null. Pass an empty Collection instead.")));
this.metadata = Collections
.unmodifiableMap(Objects.requireNonNull(metadata, "`metadata` is required, cannot be null. Pass an empty map instead."));
this.username = Objects.requireNonNull(username, "`username` is required, cannot be null");
this.roles = List.copyOf(Objects.requireNonNull(roles, "`roles` is required, cannot be null. Pass an empty Collection instead."));
jasontedor marked this conversation as resolved.
Show resolved Hide resolved
this.metadata = Collections.unmodifiableMap(
Objects.requireNonNull(metadata, "`metadata` is required, cannot be null. Pass an empty map instead."));
this.fullName = fullName;
this.email = email;
}
Expand All @@ -67,7 +64,7 @@ public User(String username, Collection<String> roles, Map<String, Object> metad
* @param username the username, also known as the principal, unique for in the scope of a realm
* @param roles the roles that this user is assigned
*/
public User(String username, Collection<String> roles) {
public User(String username, List<String> roles) {
this(username, roles, Collections.emptyMap(), null, null);
}

Expand All @@ -84,7 +81,7 @@ public String getUsername() {
* identified by their unique names and each represents as
* set of permissions. Can never be {@code null}.
*/
public Set<String> getRoles() {
public List<String> getRoles() {
return this.roles;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
import java.io.UncheckedIOException;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;

Expand All @@ -44,8 +44,8 @@ public abstract class AbstractIndicesPrivileges {
static final ParseField FIELD_PERMISSIONS = new ParseField("field_security");
static final ParseField QUERY = new ParseField("query");

protected final Set<String> indices;
protected final Set<String> privileges;
protected final List<String> indices;
protected final List<String> privileges;
protected final boolean allowRestrictedIndices;

AbstractIndicesPrivileges(Collection<String> indices, Collection<String> privileges, boolean allowRestrictedIndices) {
Expand All @@ -55,15 +55,15 @@ public abstract class AbstractIndicesPrivileges {
if (null == privileges || privileges.isEmpty()) {
throw new IllegalArgumentException("indices privileges must define at least one privilege");
}
this.indices = Collections.unmodifiableSet(new HashSet<>(indices));
this.privileges = Collections.unmodifiableSet(new HashSet<>(privileges));
this.indices = List.copyOf(indices);
this.privileges = List.copyOf(privileges);
this.allowRestrictedIndices = allowRestrictedIndices;
}

/**
* The indices names covered by the privileges.
*/
public Set<String> getIndices() {
public List<String> getIndices() {
return this.indices;
}

Expand All @@ -72,7 +72,7 @@ public Set<String> getIndices() {
* such privileges, but the {@code String} datatype allows for flexibility in defining
* finer grained privileges.
*/
public Set<String> getPrivileges() {
public List<String> getPrivileges() {
return this.privileges;
}

Expand Down Expand Up @@ -127,9 +127,9 @@ static FieldSecurity parse(XContentParser parser, Void context) throws IOExcepti

FieldSecurity(Collection<String> grantedFields, Collection<String> deniedFields) {
// unspecified granted fields means no restriction
this.grantedFields = grantedFields == null ? null : Collections.unmodifiableSet(new HashSet<>(grantedFields));
this.grantedFields = grantedFields == null ? null : Set.copyOf(grantedFields);
// unspecified denied fields means no restriction
this.deniedFields = deniedFields == null ? null : Collections.unmodifiableSet(new HashSet<>(deniedFields));
this.deniedFields = deniedFields == null ? null : Set.copyOf(deniedFields);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,11 @@
import org.elasticsearch.common.xcontent.XContentParser;

import java.io.IOException;

import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

import static org.elasticsearch.common.xcontent.ConstructingObjectParser.constructorArg;
import static org.elasticsearch.common.xcontent.ConstructingObjectParser.optionalConstructorArg;
Expand All @@ -55,10 +52,10 @@ public final class ApplicationPrivilege implements ToXContentObject {

private final String application;
private final String name;
private final Set<String> actions;
private final List<String> actions;
private final Map<String, Object> metadata;

public ApplicationPrivilege(String application, String name, Collection<String> actions, @Nullable Map<String, Object> metadata) {
public ApplicationPrivilege(String application, String name, List<String> actions, @Nullable Map<String, Object> metadata) {
if (Strings.isNullOrEmpty(application)) {
throw new IllegalArgumentException("application name must be provided");
} else {
Expand All @@ -72,12 +69,12 @@ public ApplicationPrivilege(String application, String name, Collection<String>
if (actions == null || actions.isEmpty()) {
throw new IllegalArgumentException("actions must be provided");
} else {
this.actions = Collections.unmodifiableSet(new HashSet<>(actions));
this.actions = List.copyOf(actions);
}
if (metadata == null || metadata.isEmpty()) {
this.metadata = Collections.emptyMap();
} else {
this.metadata = Collections.unmodifiableMap(metadata);
this.metadata = Map.copyOf(metadata);
}
}

Expand All @@ -89,7 +86,7 @@ public String getName() {
return name;
}

public Set<String> getActions() {
public List<String> getActions() {
return actions;
}

Expand All @@ -100,7 +97,7 @@ public Map<String, Object> getMetadata() {
@SuppressWarnings("unchecked")
public static final ConstructingObjectParser<ApplicationPrivilege, String> PARSER = new ConstructingObjectParser<>(
"application_privilege",
true, args -> new ApplicationPrivilege((String) args[0], (String) args[1], (Collection<String>) args[2],
true, args -> new ApplicationPrivilege((String) args[0], (String) args[1], (List<String>) args[2],
(Map<String, Object>) args[3]));

static {
Expand Down Expand Up @@ -137,7 +134,7 @@ public static Builder builder() {
public static final class Builder {
private String applicationName = null;
private String privilegeName = null;
private Collection<String> actions = null;
private List<String> actions = null;
private Map<String, Object> metadata = null;

private Builder() {
Expand All @@ -158,7 +155,7 @@ public Builder actions(String... actions) {
return this;
}

public Builder actions(Collection<String> actions) {
public Builder actions(List<String> actions) {
this.actions = Objects.requireNonNull(actions);
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,8 @@
import org.elasticsearch.common.xcontent.XContentType;

import java.io.IOException;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;

import static org.elasticsearch.common.xcontent.ConstructingObjectParser.constructorArg;

Expand All @@ -57,8 +54,8 @@ public final class ApplicationResourcePrivileges implements ToXContentObject {
// end up being implicitly set to null in that request.
int i = 0;
final String application = (String) constructorObjects[i++];
final Collection<String> privileges = (Collection<String>) constructorObjects[i++];
final Collection<String> resources = (Collection<String>) constructorObjects[i];
final List<String> privileges = (List<String>) constructorObjects[i++];
final List<String> resources = (List<String>) constructorObjects[i];
return new ApplicationResourcePrivileges(application, privileges, resources);
});

Expand All @@ -69,8 +66,8 @@ public final class ApplicationResourcePrivileges implements ToXContentObject {
}

private final String application;
private final Set<String> privileges;
private final Set<String> resources;
private final List<String> privileges;
jasontedor marked this conversation as resolved.
Show resolved Hide resolved
private final List<String> resources;

/**
* Constructs privileges for resources under an application scope.
Expand All @@ -85,7 +82,7 @@ public final class ApplicationResourcePrivileges implements ToXContentObject {
* The resources names. Cannot be null or empty. Resource identifiers
* are completely under the clients control.
*/
public ApplicationResourcePrivileges(String application, Collection<String> privileges, Collection<String> resources) {
public ApplicationResourcePrivileges(String application, List<String> privileges, List<String> resources) {
if (Strings.isNullOrEmpty(application)) {
throw new IllegalArgumentException("application privileges must have an application name");
}
Expand All @@ -96,19 +93,19 @@ public ApplicationResourcePrivileges(String application, Collection<String> priv
throw new IllegalArgumentException("application privileges must refer to at least one resource");
}
this.application = application;
this.privileges = Collections.unmodifiableSet(new HashSet<>(privileges));
this.resources = Collections.unmodifiableSet(new HashSet<>(resources));
this.privileges = List.copyOf(privileges);
this.resources = List.copyOf(resources);
}

public String getApplication() {
return application;
}

public Set<String> getResources() {
public List<String> getResources() {
return this.resources;
}

public Set<String> getPrivileges() {
public List<String> getPrivileges() {
return this.privileges;
}

Expand Down
Loading