23
23
import java .io .IOException ;
24
24
import java .util .List ;
25
25
import java .util .Objects ;
26
+ import java .util .Set ;
27
+ import java .util .SortedSet ;
28
+ import java .util .TreeSet ;
29
+ import java .util .stream .Collectors ;
26
30
27
31
/** View of data in OpenSearch indices */
28
32
@ ExperimentalApi
@@ -32,18 +36,18 @@ public class View extends AbstractDiffable<View> implements ToXContentObject {
32
36
private final String description ;
33
37
private final long createdAt ;
34
38
private final long modifiedAt ;
35
- private final List <Target > targets ;
39
+ private final SortedSet <Target > targets ;
36
40
37
- public View (final String name , final String description , final Long createdAt , final Long modifiedAt , final List <Target > targets ) {
41
+ public View (final String name , final String description , final Long createdAt , final Long modifiedAt , final Set <Target > targets ) {
38
42
this .name = Objects .requireNonNull (name , "Name must be provided" );
39
43
this .description = description ;
40
44
this .createdAt = createdAt != null ? createdAt : -1 ;
41
45
this .modifiedAt = modifiedAt != null ? modifiedAt : -1 ;
42
- this .targets = Objects .requireNonNull (targets , "Targets are required on a view" );
46
+ this .targets = new TreeSet <>( Objects .requireNonNull (targets , "Targets are required on a view" ) );
43
47
}
44
48
45
49
public View (final StreamInput in ) throws IOException {
46
- this (in .readString (), in .readOptionalString (), in .readZLong (), in .readZLong (), in .readList (Target ::new ));
50
+ this (in .readString (), in .readOptionalString (), in .readZLong (), in .readZLong (), new TreeSet <>( in .readList (Target ::new ) ));
47
51
}
48
52
49
53
public String getName () {
@@ -62,8 +66,8 @@ public long getModifiedAt() {
62
66
return modifiedAt ;
63
67
}
64
68
65
- public List <Target > getTargets () {
66
- return targets ;
69
+ public SortedSet <Target > getTargets () {
70
+ return new TreeSet <>( targets ) ;
67
71
}
68
72
69
73
public static Diff <View > readDiffFrom (final StreamInput in ) throws IOException {
@@ -89,7 +93,7 @@ public int hashCode() {
89
93
90
94
/** The source of data used to project the view */
91
95
@ ExperimentalApi
92
- public static class Target implements Writeable , ToXContentObject {
96
+ public static class Target implements Writeable , ToXContentObject , Comparable < Target > {
93
97
94
98
private final String indexPattern ;
95
99
@@ -144,6 +148,14 @@ public static Target fromXContent(final XContentParser parser) throws IOExceptio
144
148
public void writeTo (final StreamOutput out ) throws IOException {
145
149
out .writeString (indexPattern );
146
150
}
151
+
152
+ @ Override
153
+ public int compareTo (final Target o ) {
154
+ if (this == o ) return 0 ;
155
+
156
+ final Target other = (Target ) o ;
157
+ return this .indexPattern .compareTo (other .indexPattern );
158
+ }
147
159
}
148
160
149
161
public static final ParseField NAME_FIELD = new ParseField ("name" );
@@ -155,7 +167,7 @@ public void writeTo(final StreamOutput out) throws IOException {
155
167
@ SuppressWarnings ("unchecked" )
156
168
public static final ConstructingObjectParser <View , Void > PARSER = new ConstructingObjectParser <>(
157
169
"view" ,
158
- args -> new View ((String ) args [0 ], (String ) args [1 ], (Long ) args [2 ], (Long ) args [3 ], ( List <Target >) args [4 ])
170
+ args -> new View ((String ) args [0 ], (String ) args [1 ], (Long ) args [2 ], (Long ) args [3 ], new TreeSet <>(( List <Target >) args [4 ]) )
159
171
);
160
172
161
173
static {
@@ -188,6 +200,6 @@ public void writeTo(final StreamOutput out) throws IOException {
188
200
out .writeOptionalString (description );
189
201
out .writeZLong (createdAt );
190
202
out .writeZLong (modifiedAt );
191
- out .writeList (targets );
203
+ out .writeList (targets . stream (). collect ( Collectors . toList ()) );
192
204
}
193
205
}
0 commit comments