Skip to content

Commit 8ed1a6c

Browse files
committed
Use List.of convenience methods in 7.x autoscaling
We do not have access to JDK 9 collection convenience methods in 7.x because we are compatible with JDK 8 there. Yet, we have recently added a substitute for these convenience methods that even delegate to the right places when running on JDK 9, to make backporting easier. This commit utilizes these new methods in the autoscaling codebase.
1 parent f5ccf93 commit 8ed1a6c

File tree

6 files changed

+26
-52
lines changed

6 files changed

+26
-52
lines changed

x-pack/plugin/autoscaling/src/main/java/org/elasticsearch/xpack/autoscaling/Autoscaling.java

Lines changed: 18 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,6 @@
3636
import org.elasticsearch.xpack.autoscaling.rest.RestPutAutoscalingPolicyHandler;
3737
import org.elasticsearch.xpack.core.XPackPlugin;
3838

39-
import java.util.Arrays;
40-
import java.util.Collections;
4139
import java.util.List;
4240
import java.util.function.Supplier;
4341

@@ -86,9 +84,9 @@ public Autoscaling(final Settings settings) {
8684
@Override
8785
public List<Setting<?>> getSettings() {
8886
if (isSnapshot() || (AUTOSCALING_FEATURE_FLAG_REGISTERED != null && AUTOSCALING_FEATURE_FLAG_REGISTERED)) {
89-
return Collections.singletonList(AUTOSCALING_ENABLED_SETTING);
87+
return org.elasticsearch.common.collect.List.of(AUTOSCALING_ENABLED_SETTING);
9088
} else {
91-
return Collections.emptyList();
89+
return org.elasticsearch.common.collect.List.of();
9290
}
9391
}
9492

@@ -99,14 +97,12 @@ boolean isSnapshot() {
9997
@Override
10098
public List<ActionHandler<? extends ActionRequest, ? extends ActionResponse>> getActions() {
10199
if (enabled) {
102-
return Collections.unmodifiableList(
103-
Arrays.asList(
104-
new ActionHandler<>(GetAutoscalingDecisionAction.INSTANCE, TransportGetAutoscalingDecisionAction.class),
105-
new ActionHandler<>(PutAutoscalingPolicyAction.INSTANCE, TransportPutAutoscalingPolicyAction.class)
106-
)
100+
return org.elasticsearch.common.collect.List.of(
101+
new ActionHandler<>(GetAutoscalingDecisionAction.INSTANCE, TransportGetAutoscalingDecisionAction.class),
102+
new ActionHandler<>(PutAutoscalingPolicyAction.INSTANCE, TransportPutAutoscalingPolicyAction.class)
107103
);
108104
} else {
109-
return Collections.emptyList();
105+
return org.elasticsearch.common.collect.List.of();
110106
}
111107
}
112108

@@ -121,43 +117,29 @@ public List<RestHandler> getRestHandlers(
121117
final Supplier<DiscoveryNodes> nodesInCluster
122118
) {
123119
if (enabled) {
124-
return Collections.unmodifiableList(
125-
Arrays.asList(new RestGetAutoscalingDecisionHandler(), new RestPutAutoscalingPolicyHandler())
126-
);
120+
return org.elasticsearch.common.collect.List.of(new RestGetAutoscalingDecisionHandler(), new RestPutAutoscalingPolicyHandler());
127121
} else {
128-
return Collections.emptyList();
122+
return org.elasticsearch.common.collect.List.of();
129123
}
130124
}
131125

132126
@Override
133127
public List<NamedWriteableRegistry.Entry> getNamedWriteables() {
134-
return Collections.unmodifiableList(
135-
Arrays.asList(
136-
new NamedWriteableRegistry.Entry(Metadata.Custom.class, AutoscalingMetadata.NAME, AutoscalingMetadata::new),
137-
new NamedWriteableRegistry.Entry(
138-
NamedDiff.class,
139-
AutoscalingMetadata.NAME,
140-
AutoscalingMetadata.AutoscalingMetadataDiff::new
141-
),
142-
new NamedWriteableRegistry.Entry(AutoscalingDecider.class, AlwaysAutoscalingDecider.NAME, AlwaysAutoscalingDecider::new)
143-
)
128+
return org.elasticsearch.common.collect.List.of(
129+
new NamedWriteableRegistry.Entry(Metadata.Custom.class, AutoscalingMetadata.NAME, AutoscalingMetadata::new),
130+
new NamedWriteableRegistry.Entry(NamedDiff.class, AutoscalingMetadata.NAME, AutoscalingMetadata.AutoscalingMetadataDiff::new),
131+
new NamedWriteableRegistry.Entry(AutoscalingDecider.class, AlwaysAutoscalingDecider.NAME, AlwaysAutoscalingDecider::new)
144132
);
145133
}
146134

147135
@Override
148136
public List<NamedXContentRegistry.Entry> getNamedXContent() {
149-
return Collections.unmodifiableList(
150-
Arrays.asList(
151-
new NamedXContentRegistry.Entry(
152-
Metadata.Custom.class,
153-
new ParseField(AutoscalingMetadata.NAME),
154-
AutoscalingMetadata::parse
155-
),
156-
new NamedXContentRegistry.Entry(
157-
AutoscalingDecider.class,
158-
new ParseField(AlwaysAutoscalingDecider.NAME),
159-
AlwaysAutoscalingDecider::parse
160-
)
137+
return org.elasticsearch.common.collect.List.of(
138+
new NamedXContentRegistry.Entry(Metadata.Custom.class, new ParseField(AutoscalingMetadata.NAME), AutoscalingMetadata::parse),
139+
new NamedXContentRegistry.Entry(
140+
AutoscalingDecider.class,
141+
new ParseField(AlwaysAutoscalingDecider.NAME),
142+
AlwaysAutoscalingDecider::parse
161143
)
162144
);
163145
}

x-pack/plugin/autoscaling/src/main/java/org/elasticsearch/xpack/autoscaling/rest/RestGetAutoscalingDecisionHandler.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,13 @@
1414

1515
import java.util.List;
1616

17-
import static java.util.Collections.singletonList;
1817
import static org.elasticsearch.rest.RestRequest.Method.GET;
1918

2019
public class RestGetAutoscalingDecisionHandler extends BaseRestHandler {
2120

2221
@Override
2322
public List<Route> routes() {
24-
return singletonList(new Route(GET, "/_autoscaling/decision"));
23+
return org.elasticsearch.common.collect.List.of(new Route(GET, "/_autoscaling/decision"));
2524
}
2625

2726
@Override

x-pack/plugin/autoscaling/src/main/java/org/elasticsearch/xpack/autoscaling/rest/RestPutAutoscalingPolicyHandler.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
import org.elasticsearch.xpack.autoscaling.action.PutAutoscalingPolicyAction;
1515

1616
import java.io.IOException;
17-
import java.util.Collections;
1817
import java.util.List;
1918

2019
import static org.elasticsearch.rest.RestRequest.Method.PUT;
@@ -23,7 +22,7 @@ public class RestPutAutoscalingPolicyHandler extends BaseRestHandler {
2322

2423
@Override
2524
public List<Route> routes() {
26-
return Collections.singletonList(new Route(PUT, "/_autoscaling/policy/{name}"));
25+
return org.elasticsearch.common.collect.List.of(new Route(PUT, "/_autoscaling/policy/{name}"));
2726
}
2827

2928
@Override

x-pack/plugin/autoscaling/src/test/java/org/elasticsearch/xpack/autoscaling/AutoscalingIntegTestCase.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,19 @@
66

77
package org.elasticsearch.xpack.autoscaling;
88

9+
import org.elasticsearch.common.collect.List;
910
import org.elasticsearch.common.settings.Settings;
1011
import org.elasticsearch.plugins.Plugin;
1112
import org.elasticsearch.test.ESIntegTestCase;
1213
import org.elasticsearch.xpack.core.XPackSettings;
1314

14-
import java.util.Arrays;
1515
import java.util.Collection;
16-
import java.util.Collections;
1716

1817
public abstract class AutoscalingIntegTestCase extends ESIntegTestCase {
1918

2019
@Override
2120
protected Collection<Class<? extends Plugin>> nodePlugins() {
22-
return Collections.singletonList(LocalStateAutoscaling.class);
21+
return List.of(LocalStateAutoscaling.class);
2322
}
2423

2524
@Override
@@ -32,7 +31,7 @@ protected Settings nodeSettings(final int nodeOrdinal) {
3231

3332
@Override
3433
protected Collection<Class<? extends Plugin>> transportClientPlugins() {
35-
return Collections.unmodifiableList(Arrays.asList(LocalStateAutoscaling.class, getTestTransportPlugin()));
34+
return List.of(LocalStateAutoscaling.class, getTestTransportPlugin());
3635
}
3736

3837
@Override

x-pack/plugin/autoscaling/src/test/java/org/elasticsearch/xpack/autoscaling/AutoscalingTestCase.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
import org.elasticsearch.xpack.autoscaling.policy.AutoscalingPolicyMetadata;
1818

1919
import java.util.ArrayList;
20-
import java.util.Collections;
2120
import java.util.List;
2221
import java.util.Map;
2322
import java.util.SortedMap;
@@ -68,7 +67,7 @@ public static AutoscalingDecisions randomAutoscalingDecisions(
6867

6968
public static SortedMap<String, AutoscalingDecider> randomAutoscalingDeciders() {
7069
return new TreeMap<>(
71-
Collections.singletonList(new AlwaysAutoscalingDecider())
70+
org.elasticsearch.common.collect.List.of(new AlwaysAutoscalingDecider())
7271
.stream()
7372
.collect(Collectors.toMap(AutoscalingDecider::name, Function.identity()))
7473
);

x-pack/plugin/autoscaling/src/test/java/org/elasticsearch/xpack/autoscaling/decision/AutoscalingDecisionsTests.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,15 @@
66

77
package org.elasticsearch.xpack.autoscaling.decision;
88

9+
import org.elasticsearch.common.collect.List;
910
import org.elasticsearch.xpack.autoscaling.AutoscalingTestCase;
1011

11-
import java.util.Collections;
12-
1312
import static org.hamcrest.Matchers.equalTo;
1413

1514
public class AutoscalingDecisionsTests extends AutoscalingTestCase {
1615

1716
public void testAutoscalingDecisionsRejectsEmptyDecisions() {
18-
final IllegalArgumentException e = expectThrows(
19-
IllegalArgumentException.class,
20-
() -> new AutoscalingDecisions(Collections.emptyList())
21-
);
17+
final IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> new AutoscalingDecisions(List.of()));
2218
assertThat(e.getMessage(), equalTo("decisions can not be empty"));
2319
}
2420

0 commit comments

Comments
 (0)