Skip to content

Commit

Permalink
Use Java 11 collections conveniences everywhere (#41399)
Browse files Browse the repository at this point in the history
This commit replaces all applicable uses with Java 11 collections
convenience methods.
  • Loading branch information
jasontedor authored Apr 26, 2019
1 parent ce59d45 commit f48ddd5
Show file tree
Hide file tree
Showing 142 changed files with 1,948 additions and 2,010 deletions.
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,12 +27,14 @@
import org.elasticsearch.common.xcontent.XContentParserUtils;

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.Map;
import java.util.Objects;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;

import static org.elasticsearch.common.xcontent.ConstructingObjectParser.constructorArg;
import static org.elasticsearch.common.xcontent.ConstructingObjectParser.optionalConstructorArg;
Expand All @@ -42,26 +44,27 @@
* Returns a List of {@link User} objects
*/
public class GetUsersResponse {
private final Set<User> users;
private final Set<User> enabledUsers;

public GetUsersResponse(Set<User> users, Set<User> enabledUsers) {
this.users = Collections.unmodifiableSet(users);
this.enabledUsers = Collections.unmodifiableSet(enabledUsers);
private final Map<String, User> users;
private final Map<String, User> enabledUsers;

GetUsersResponse(final Map<String, User> users, final Map<String, User> enabledUsers) {
this.users = Map.copyOf(users);
this.enabledUsers = Map.copyOf(enabledUsers);
}

public Set<User> getUsers() {
return users;
public List<User> getUsers() {
return List.copyOf(users.values());
}

public Set<User> getEnabledUsers() {
return enabledUsers;
public List<User> getEnabledUsers() {
return List.copyOf(enabledUsers.values());
}

public static GetUsersResponse fromXContent(XContentParser parser) throws IOException {
XContentParserUtils.ensureExpectedToken(Token.START_OBJECT, parser.nextToken(), parser::getTokenLocation);
final Set<User> users = new HashSet<>();
final Set<User> enabledUsers = new HashSet<>();
final List<User> users = new ArrayList<>();
final List<User> enabledUsers = new ArrayList<>();
Token token;
while ((token = parser.nextToken()) != Token.END_OBJECT) {
XContentParserUtils.ensureExpectedToken(Token.FIELD_NAME, token, parser::getTokenLocation);
Expand All @@ -71,7 +74,11 @@ public static GetUsersResponse fromXContent(XContentParser parser) throws IOExce
enabledUsers.add(parsedUser.user);
}
}
return new GetUsersResponse(users, enabledUsers);
return new GetUsersResponse(toMap(users), toMap(enabledUsers));
}

static Map<String, User> toMap(final Collection<User> users) {
return users.stream().collect(Collectors.toUnmodifiableMap(User::getUsername, Function.identity()));
}

@Override
Expand Down Expand Up @@ -99,7 +106,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 +127,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 list instead."));
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 list instead."));
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,9 +31,8 @@
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;

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

Expand All @@ -44,8 +43,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 +54,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 +71,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 @@ -106,8 +105,8 @@ public static class FieldSecurity implements ToXContentObject {
@SuppressWarnings("unchecked")
private static FieldSecurity buildObjectFromParserArgs(Object[] args) {
return new FieldSecurity(
(Collection<String>) args[0],
(Collection<String>) args[1]
(List<String>) args[0],
(List<String>) args[1]
);
}

Expand All @@ -121,31 +120,31 @@ static FieldSecurity parse(XContentParser parser, Void context) throws IOExcepti
}

// null or singleton '*' means all fields are granted, empty means no fields are granted
private final Set<String> grantedFields;
private final List<String> grantedFields;
// null or empty means no fields are denied
private final Set<String> deniedFields;
private final List<String> deniedFields;

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 : List.copyOf(grantedFields);
// unspecified denied fields means no restriction
this.deniedFields = deniedFields == null ? null : Collections.unmodifiableSet(new HashSet<>(deniedFields));
this.deniedFields = deniedFields == null ? null : List.copyOf(deniedFields);
}

/**
* The document fields that can be read or queried. Can be null, in this case
* all the document's fields are granted access to. Can also be empty, in which
* case no fields are granted access to.
*/
public Set<String> getGrantedFields() {
public List<String> getGrantedFields() {
return grantedFields;
}

/**
* The document fields that cannot be accessed or queried. Can be null or empty,
* in which case no fields are denied.
*/
public Set<String> getDeniedFields() {
public List<String> getDeniedFields() {
return deniedFields;
}

Expand Down
Loading

0 comments on commit f48ddd5

Please sign in to comment.