Skip to content

Commit f48ddd5

Browse files
authored
Use Java 11 collections conveniences everywhere (#41399)
This commit replaces all applicable uses with Java 11 collections convenience methods.
1 parent ce59d45 commit f48ddd5

File tree

142 files changed

+1948
-2010
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

142 files changed

+1948
-2010
lines changed

client/rest-high-level/src/main/java/org/elasticsearch/client/ml/filestructurefinder/FileStructure.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import org.elasticsearch.common.xcontent.XContentParser;
2626

2727
import java.io.IOException;
28-
import java.util.ArrayList;
2928
import java.util.Collections;
3029
import java.util.LinkedHashMap;
3130
import java.util.List;
@@ -148,22 +147,20 @@ private FileStructure(int numLinesAnalyzed, int numMessagesAnalyzed, String samp
148147
this.format = Objects.requireNonNull(format);
149148
this.multilineStartPattern = multilineStartPattern;
150149
this.excludeLinesPattern = excludeLinesPattern;
151-
this.columnNames = (columnNames == null) ? null : Collections.unmodifiableList(new ArrayList<>(columnNames));
150+
this.columnNames = (columnNames == null) ? null : List.copyOf(columnNames);
152151
this.hasHeaderRow = hasHeaderRow;
153152
this.delimiter = delimiter;
154153
this.quote = quote;
155154
this.shouldTrimFields = shouldTrimFields;
156155
this.grokPattern = grokPattern;
157156
this.timestampField = timestampField;
158-
this.jodaTimestampFormats =
159-
(jodaTimestampFormats == null) ? null : Collections.unmodifiableList(new ArrayList<>(jodaTimestampFormats));
160-
this.javaTimestampFormats =
161-
(javaTimestampFormats == null) ? null : Collections.unmodifiableList(new ArrayList<>(javaTimestampFormats));
157+
this.jodaTimestampFormats = (jodaTimestampFormats == null) ? null : List.copyOf(jodaTimestampFormats);
158+
this.javaTimestampFormats = (javaTimestampFormats == null) ? null : List.copyOf(javaTimestampFormats);
162159
this.needClientTimezone = needClientTimezone;
163160
this.mappings = Collections.unmodifiableSortedMap(new TreeMap<>(mappings));
164161
this.ingestPipeline = (ingestPipeline == null) ? null : Collections.unmodifiableMap(new LinkedHashMap<>(ingestPipeline));
165162
this.fieldStats = Collections.unmodifiableSortedMap(new TreeMap<>(fieldStats));
166-
this.explanation = (explanation == null) ? null : Collections.unmodifiableList(new ArrayList<>(explanation));
163+
this.explanation = (explanation == null) ? null : List.copyOf(explanation);
167164
}
168165

169166
public int getNumLinesAnalyzed() {

client/rest-high-level/src/main/java/org/elasticsearch/client/security/GetPrivilegesResponse.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import java.io.IOException;
2727
import java.util.ArrayList;
2828
import java.util.Collection;
29-
import java.util.Collections;
3029
import java.util.HashSet;
3130
import java.util.List;
3231
import java.util.Objects;
@@ -44,7 +43,7 @@ public Set<ApplicationPrivilege> getPrivileges() {
4443
}
4544

4645
public GetPrivilegesResponse(Collection<ApplicationPrivilege> privileges) {
47-
this.privileges = Collections.unmodifiableSet(new HashSet<>(privileges));
46+
this.privileges = Set.copyOf(privileges);
4847
}
4948

5049
public static GetPrivilegesResponse fromXContent(XContentParser parser) throws IOException {

client/rest-high-level/src/main/java/org/elasticsearch/client/security/GetUsersResponse.java

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,14 @@
2727
import org.elasticsearch.common.xcontent.XContentParserUtils;
2828

2929
import java.io.IOException;
30+
import java.util.ArrayList;
3031
import java.util.Collection;
3132
import java.util.Collections;
32-
import java.util.HashSet;
33+
import java.util.List;
3334
import java.util.Map;
3435
import java.util.Objects;
35-
import java.util.Set;
36+
import java.util.function.Function;
37+
import java.util.stream.Collectors;
3638

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

48-
public GetUsersResponse(Set<User> users, Set<User> enabledUsers) {
49-
this.users = Collections.unmodifiableSet(users);
50-
this.enabledUsers = Collections.unmodifiableSet(enabledUsers);
48+
private final Map<String, User> users;
49+
private final Map<String, User> enabledUsers;
50+
51+
GetUsersResponse(final Map<String, User> users, final Map<String, User> enabledUsers) {
52+
this.users = Map.copyOf(users);
53+
this.enabledUsers = Map.copyOf(enabledUsers);
5154
}
5255

53-
public Set<User> getUsers() {
54-
return users;
56+
public List<User> getUsers() {
57+
return List.copyOf(users.values());
5558
}
5659

57-
public Set<User> getEnabledUsers() {
58-
return enabledUsers;
60+
public List<User> getEnabledUsers() {
61+
return List.copyOf(enabledUsers.values());
5962
}
6063

6164
public static GetUsersResponse fromXContent(XContentParser parser) throws IOException {
6265
XContentParserUtils.ensureExpectedToken(Token.START_OBJECT, parser.nextToken(), parser::getTokenLocation);
63-
final Set<User> users = new HashSet<>();
64-
final Set<User> enabledUsers = new HashSet<>();
66+
final List<User> users = new ArrayList<>();
67+
final List<User> enabledUsers = new ArrayList<>();
6568
Token token;
6669
while ((token = parser.nextToken()) != Token.END_OBJECT) {
6770
XContentParserUtils.ensureExpectedToken(Token.FIELD_NAME, token, parser::getTokenLocation);
@@ -71,7 +74,11 @@ public static GetUsersResponse fromXContent(XContentParser parser) throws IOExce
7174
enabledUsers.add(parsedUser.user);
7275
}
7376
}
74-
return new GetUsersResponse(users, enabledUsers);
77+
return new GetUsersResponse(toMap(users), toMap(enabledUsers));
78+
}
79+
80+
static Map<String, User> toMap(final Collection<User> users) {
81+
return users.stream().collect(Collectors.toUnmodifiableMap(User::getUsername, Function.identity()));
7582
}
7683

7784
@Override
@@ -99,7 +106,7 @@ public int hashCode() {
99106
(constructorObjects) -> {
100107
int i = 0;
101108
final String username = (String) constructorObjects[i++];
102-
final Collection<String> roles = (Collection<String>) constructorObjects[i++];
109+
final List<String> roles = (List<String>) constructorObjects[i++];
103110
final Map<String, Object> metadata = (Map<String, Object>) constructorObjects[i++];
104111
final Boolean enabled = (Boolean) constructorObjects[i++];
105112
final String fullName = (String) constructorObjects[i++];
@@ -120,13 +127,13 @@ protected static final class ParsedUser {
120127
protected User user;
121128
protected boolean enabled;
122129

123-
public ParsedUser(String username, Collection<String> roles, Map<String, Object> metadata, Boolean enabled,
130+
public ParsedUser(String username, List<String> roles, Map<String, Object> metadata, Boolean enabled,
124131
@Nullable String fullName, @Nullable String email) {
125-
String checkedUsername = username = Objects.requireNonNull(username, "`username` is required, cannot be null");
126-
Collection<String> checkedRoles = Collections.unmodifiableSet(new HashSet<>(
127-
Objects.requireNonNull(roles, "`roles` is required, cannot be null. Pass an empty Collection instead.")));
128-
Map<String, Object> checkedMetadata = Collections
129-
.unmodifiableMap(Objects.requireNonNull(metadata, "`metadata` is required, cannot be null. Pass an empty map instead."));
132+
String checkedUsername = Objects.requireNonNull(username, "`username` is required, cannot be null");
133+
List<String> checkedRoles =
134+
List.copyOf(Objects.requireNonNull(roles, "`roles` is required, cannot be null. Pass an empty list instead."));
135+
Map<String, Object> checkedMetadata = Collections.unmodifiableMap(
136+
Objects.requireNonNull(metadata, "`metadata` is required, cannot be null. Pass an empty map instead."));
130137
this.user = new User(checkedUsername, checkedRoles, checkedMetadata, fullName, email);
131138
this.enabled = enabled;
132139
}

client/rest-high-level/src/main/java/org/elasticsearch/client/security/support/expressiondsl/expressions/CompositeRoleMapperExpression.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@
2323
import org.elasticsearch.common.xcontent.XContentBuilder;
2424

2525
import java.io.IOException;
26-
import java.util.Arrays;
27-
import java.util.Collections;
2826
import java.util.List;
2927
import java.util.Objects;
3028

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

5957
public String getName() {

client/rest-high-level/src/main/java/org/elasticsearch/client/security/support/expressiondsl/fields/FieldRoleMapperExpression.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@
2323
import org.elasticsearch.common.xcontent.XContentBuilder;
2424

2525
import java.io.IOException;
26-
import java.util.Arrays;
27-
import java.util.Collections;
2826
import java.util.List;
2927
import java.util.Objects;
3028

@@ -53,7 +51,7 @@ public FieldRoleMapperExpression(final String field, final Object... values) {
5351
throw new IllegalArgumentException("null or empty values for field (" + field + ")");
5452
}
5553
this.field = field;
56-
this.values = Collections.unmodifiableList(Arrays.asList(values));
54+
this.values = List.of(values);
5755
}
5856

5957
public String getField() {

client/rest-high-level/src/main/java/org/elasticsearch/client/security/user/User.java

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,10 @@
2222
import org.elasticsearch.common.Nullable;
2323
import org.elasticsearch.common.Strings;
2424

25-
import java.util.Collection;
2625
import java.util.Collections;
27-
import java.util.HashSet;
26+
import java.util.List;
2827
import java.util.Map;
2928
import java.util.Objects;
30-
import java.util.Set;
3129

3230
/**
3331
* A user to be utilized with security APIs.
@@ -36,7 +34,7 @@
3634
public final class User {
3735

3836
private final String username;
39-
private final Set<String> roles;
37+
private final List<String> roles;
4038
private final Map<String, Object> metadata;
4139
@Nullable private final String fullName;
4240
@Nullable private final String email;
@@ -50,13 +48,12 @@ public final class User {
5048
* @param fullName the full name of the user that may be used for display purposes
5149
* @param email the email address of the user
5250
*/
53-
public User(String username, Collection<String> roles, Map<String, Object> metadata, @Nullable String fullName,
51+
public User(String username, List<String> roles, Map<String, Object> metadata, @Nullable String fullName,
5452
@Nullable String email) {
55-
this.username = username = Objects.requireNonNull(username, "`username` is required, cannot be null");
56-
this.roles = Collections.unmodifiableSet(new HashSet<>(
57-
Objects.requireNonNull(roles, "`roles` is required, cannot be null. Pass an empty Collection instead.")));
58-
this.metadata = Collections
59-
.unmodifiableMap(Objects.requireNonNull(metadata, "`metadata` is required, cannot be null. Pass an empty map instead."));
53+
this.username = Objects.requireNonNull(username, "`username` is required, cannot be null");
54+
this.roles = List.copyOf(Objects.requireNonNull(roles, "`roles` is required, cannot be null. Pass an empty list instead."));
55+
this.metadata = Collections.unmodifiableMap(
56+
Objects.requireNonNull(metadata, "`metadata` is required, cannot be null. Pass an empty map instead."));
6057
this.fullName = fullName;
6158
this.email = email;
6259
}
@@ -67,7 +64,7 @@ public User(String username, Collection<String> roles, Map<String, Object> metad
6764
* @param username the username, also known as the principal, unique for in the scope of a realm
6865
* @param roles the roles that this user is assigned
6966
*/
70-
public User(String username, Collection<String> roles) {
67+
public User(String username, List<String> roles) {
7168
this(username, roles, Collections.emptyMap(), null, null);
7269
}
7370

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

client/rest-high-level/src/main/java/org/elasticsearch/client/security/user/privileges/AbstractIndicesPrivileges.java

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,8 @@
3131
import java.io.UncheckedIOException;
3232
import java.util.Collection;
3333
import java.util.Collections;
34-
import java.util.HashSet;
34+
import java.util.List;
3535
import java.util.Objects;
36-
import java.util.Set;
3736

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

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

47-
protected final Set<String> indices;
48-
protected final Set<String> privileges;
46+
protected final List<String> indices;
47+
protected final List<String> privileges;
4948
protected final boolean allowRestrictedIndices;
5049

5150
AbstractIndicesPrivileges(Collection<String> indices, Collection<String> privileges, boolean allowRestrictedIndices) {
@@ -55,15 +54,15 @@ public abstract class AbstractIndicesPrivileges {
5554
if (null == privileges || privileges.isEmpty()) {
5655
throw new IllegalArgumentException("indices privileges must define at least one privilege");
5756
}
58-
this.indices = Collections.unmodifiableSet(new HashSet<>(indices));
59-
this.privileges = Collections.unmodifiableSet(new HashSet<>(privileges));
57+
this.indices = List.copyOf(indices);
58+
this.privileges = List.copyOf(privileges);
6059
this.allowRestrictedIndices = allowRestrictedIndices;
6160
}
6261

6362
/**
6463
* The indices names covered by the privileges.
6564
*/
66-
public Set<String> getIndices() {
65+
public List<String> getIndices() {
6766
return this.indices;
6867
}
6968

@@ -72,7 +71,7 @@ public Set<String> getIndices() {
7271
* such privileges, but the {@code String} datatype allows for flexibility in defining
7372
* finer grained privileges.
7473
*/
75-
public Set<String> getPrivileges() {
74+
public List<String> getPrivileges() {
7675
return this.privileges;
7776
}
7877

@@ -106,8 +105,8 @@ public static class FieldSecurity implements ToXContentObject {
106105
@SuppressWarnings("unchecked")
107106
private static FieldSecurity buildObjectFromParserArgs(Object[] args) {
108107
return new FieldSecurity(
109-
(Collection<String>) args[0],
110-
(Collection<String>) args[1]
108+
(List<String>) args[0],
109+
(List<String>) args[1]
111110
);
112111
}
113112

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

123122
// null or singleton '*' means all fields are granted, empty means no fields are granted
124-
private final Set<String> grantedFields;
123+
private final List<String> grantedFields;
125124
// null or empty means no fields are denied
126-
private final Set<String> deniedFields;
125+
private final List<String> deniedFields;
127126

128127
FieldSecurity(Collection<String> grantedFields, Collection<String> deniedFields) {
129128
// unspecified granted fields means no restriction
130-
this.grantedFields = grantedFields == null ? null : Collections.unmodifiableSet(new HashSet<>(grantedFields));
129+
this.grantedFields = grantedFields == null ? null : List.copyOf(grantedFields);
131130
// unspecified denied fields means no restriction
132-
this.deniedFields = deniedFields == null ? null : Collections.unmodifiableSet(new HashSet<>(deniedFields));
131+
this.deniedFields = deniedFields == null ? null : List.copyOf(deniedFields);
133132
}
134133

135134
/**
136135
* The document fields that can be read or queried. Can be null, in this case
137136
* all the document's fields are granted access to. Can also be empty, in which
138137
* case no fields are granted access to.
139138
*/
140-
public Set<String> getGrantedFields() {
139+
public List<String> getGrantedFields() {
141140
return grantedFields;
142141
}
143142

144143
/**
145144
* The document fields that cannot be accessed or queried. Can be null or empty,
146145
* in which case no fields are denied.
147146
*/
148-
public Set<String> getDeniedFields() {
147+
public List<String> getDeniedFields() {
149148
return deniedFields;
150149
}
151150

0 commit comments

Comments
 (0)