Skip to content

Commit 766e25a

Browse files
authored
Use records to make code more concise (#445)
I believe this is a compatible change. Also since source compatibility is set to Java 17 I think records are guaranteed to be available in all compatible runtimes. Signed-off-by: Andrew Ross <andrross@amazon.com>
1 parent 8c248f6 commit 766e25a

File tree

4 files changed

+20
-44
lines changed

4 files changed

+20
-44
lines changed

spring-data-opensearch/src/main/java/org/opensearch/data/client/orhlc/ScriptField.java

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,9 @@
1313

1414
/**
1515
* Scripted field
16+
*
1617
* @since 0.1
1718
*/
18-
public class ScriptField {
19-
20-
private final String fieldName;
21-
private final Script script;
22-
23-
public ScriptField(String fieldName, Script script) {
24-
this.fieldName = fieldName;
25-
this.script = script;
26-
}
27-
28-
public String fieldName() {
29-
return fieldName;
30-
}
19+
public record ScriptField(String fieldName, Script script) {
3120

32-
public Script script() {
33-
return script;
34-
}
3521
}

spring-data-opensearch/src/main/java/org/opensearch/data/client/osc/Aggregation.java

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,27 +18,26 @@
1818
import org.opensearch.client.opensearch._types.aggregations.Aggregate;
1919

2020
/**
21-
* Class to combine an OpenSearch {@link org.opensearch.client.opensearch._types.aggregations.Aggregate} with its
22-
* name. Necessary as the OpenSearch Aggregate does not know its name.
21+
* Class to combine an OpenSearch {@link Aggregate} with its name. Necessary as the OpenSearch Aggregate does not know its name.
2322
*
2423
* @author Peter-Josef Meisch
2524
* @since 4.4
2625
*/
27-
public class Aggregation {
28-
29-
private final String name;
30-
private final Aggregate aggregate;
31-
32-
public Aggregation(String name, Aggregate aggregate) {
33-
this.name = name;
34-
this.aggregate = aggregate;
35-
}
26+
public record Aggregation(String name, Aggregate aggregate) {
3627

28+
/**
29+
* @deprecated Use {@link #name()} instead
30+
*/
31+
@Deprecated
3732
public String getName() {
38-
return name;
33+
return name();
3934
}
4035

36+
/**
37+
* @deprecated Use {@link #aggregate()} instead
38+
*/
39+
@Deprecated
4140
public Aggregate getAggregate() {
42-
return aggregate;
41+
return aggregate();
4342
}
4443
}

spring-data-opensearch/src/main/java/org/opensearch/data/client/osc/OpenSearchAggregation.java

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,11 @@
1818
import org.springframework.data.elasticsearch.core.AggregationContainer;
1919

2020
/**
21-
* {@link AggregationContainer} for a {@link Aggregation} that holds OpenEearch data.
21+
* {@link AggregationContainer} for a {@link Aggregation} that holds OpenSearch data.
22+
*
2223
* @author Peter-Josef Meisch
2324
* @since 4.4
2425
*/
25-
public class OpenSearchAggregation implements AggregationContainer<Aggregation> {
26-
27-
private final Aggregation aggregation;
28-
29-
public OpenSearchAggregation(Aggregation aggregation) {
30-
this.aggregation = aggregation;
31-
}
26+
public record OpenSearchAggregation(Aggregation aggregation) implements AggregationContainer<Aggregation> {
3227

33-
@Override
34-
public Aggregation aggregation() {
35-
return aggregation;
36-
}
3728
}

spring-data-opensearch/src/test/java/org/opensearch/data/client/core/aggregation/AggregationOSCIntegrationTests.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ protected void assertThatAggsHasResult(AggregationsContainer<?> aggregationsCont
5454
List<OpenSearchAggregation> aggregations = ((OpenSearchAggregations) aggregationsContainer).aggregations();
5555
List<String> aggNames = aggregations.stream() //
5656
.map(OpenSearchAggregation::aggregation) //
57-
.map(org.opensearch.data.client.osc.Aggregation::getName) //
57+
.map(org.opensearch.data.client.osc.Aggregation::name) //
5858
.collect(Collectors.toList());
5959
assertThat(aggNames).contains(aggsName);
6060

@@ -77,8 +77,8 @@ protected void assertThatPipelineAggsAreCorrect(
7777
AggregationsContainer<?> aggregationsContainer, String aggsName, String pipelineAggsName) {
7878
Map<String, Aggregate> aggregates = ((OpenSearchAggregations) aggregationsContainer).aggregations().stream() //
7979
.map(OpenSearchAggregation::aggregation) //
80-
.collect(Collectors.toMap(org.opensearch.data.client.osc.Aggregation::getName,
81-
org.opensearch.data.client.osc.Aggregation::getAggregate));
80+
.collect(Collectors.toMap(org.opensearch.data.client.osc.Aggregation::name,
81+
org.opensearch.data.client.osc.Aggregation::aggregate));
8282

8383
assertThat(aggregates).containsKey(aggsName);
8484
Aggregate aggregate = aggregates.get(pipelineAggsName);

0 commit comments

Comments
 (0)