2323import org .elasticsearch .common .Strings ;
2424import org .elasticsearch .common .io .stream .StreamInput ;
2525import org .elasticsearch .common .io .stream .StreamOutput ;
26- import org .elasticsearch .common .io .stream .Streamable ;
26+ import org .elasticsearch .common .io .stream .Writeable ;
2727import org .elasticsearch .common .xcontent .ConstructingObjectParser ;
2828import org .elasticsearch .common .xcontent .ToXContentObject ;
2929import org .elasticsearch .common .xcontent .XContentBuilder ;
4343
4444public class AnalyzeResponse extends ActionResponse implements Iterable <AnalyzeResponse .AnalyzeToken >, ToXContentObject {
4545
46- public static class AnalyzeToken implements Streamable , ToXContentObject {
47- private String term ;
48- private int startOffset ;
49- private int endOffset ;
50- private int position ;
51- private int positionLength = 1 ;
52- private Map <String , Object > attributes ;
53- private String type ;
54-
55- AnalyzeToken () {
56- }
46+ public static class AnalyzeToken implements Writeable , ToXContentObject {
47+ private final String term ;
48+ private final int startOffset ;
49+ private final int endOffset ;
50+ private final int position ;
51+ private final int positionLength ;
52+ private final Map <String , Object > attributes ;
53+ private final String type ;
5754
5855 @ Override
5956 public boolean equals (Object o ) {
@@ -85,6 +82,21 @@ public AnalyzeToken(String term, int position, int startOffset, int endOffset, i
8582 this .attributes = attributes ;
8683 }
8784
85+ public AnalyzeToken (StreamInput in ) throws IOException {
86+ term = in .readString ();
87+ startOffset = in .readInt ();
88+ endOffset = in .readInt ();
89+ position = in .readVInt ();
90+ Integer len = in .readOptionalVInt ();
91+ if (len != null ) {
92+ positionLength = len ;
93+ } else {
94+ positionLength = 1 ;
95+ }
96+ type = in .readOptionalString ();
97+ attributes = in .readMap ();
98+ }
99+
88100 public String getTerm () {
89101 return this .term ;
90102 }
@@ -134,12 +146,6 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
134146 return builder ;
135147 }
136148
137- public static AnalyzeToken readAnalyzeToken (StreamInput in ) throws IOException {
138- AnalyzeToken analyzeToken = new AnalyzeToken ();
139- analyzeToken .readFrom (in );
140- return analyzeToken ;
141- }
142-
143149 public static AnalyzeToken fromXContent (XContentParser parser ) throws IOException {
144150 ensureExpectedToken (XContentParser .Token .START_OBJECT , parser .currentToken (), parser ::getTokenLocation );
145151 String field = null ;
@@ -184,22 +190,6 @@ public static AnalyzeToken fromXContent(XContentParser parser) throws IOExceptio
184190 return new AnalyzeToken (term , position , startOffset , endOffset , positionLength , type , attributes );
185191 }
186192
187- @ Override
188- public void readFrom (StreamInput in ) throws IOException {
189- term = in .readString ();
190- startOffset = in .readInt ();
191- endOffset = in .readInt ();
192- position = in .readVInt ();
193- Integer len = in .readOptionalVInt ();
194- if (len != null ) {
195- positionLength = len ;
196- } else {
197- positionLength = 1 ;
198- }
199- type = in .readOptionalString ();
200- attributes = in .readMap ();
201- }
202-
203193 @ Override
204194 public void writeTo (StreamOutput out ) throws IOException {
205195 out .writeString (term );
@@ -212,18 +202,35 @@ public void writeTo(StreamOutput out) throws IOException {
212202 }
213203 }
214204
215- private DetailAnalyzeResponse detail ;
216-
217- private List <AnalyzeToken > tokens ;
205+ private final DetailAnalyzeResponse detail ;
218206
219- AnalyzeResponse () {
220- }
207+ private final List <AnalyzeToken > tokens ;
221208
222209 public AnalyzeResponse (List <AnalyzeToken > tokens , DetailAnalyzeResponse detail ) {
223210 this .tokens = tokens ;
224211 this .detail = detail ;
225212 }
226213
214+ public AnalyzeResponse (StreamInput in ) throws IOException {
215+ super .readFrom (in );
216+ int size = in .readVInt ();
217+ if (size > 0 ) {
218+ tokens = new ArrayList <>(size );
219+ for (int i = 0 ; i < size ; i ++) {
220+ tokens .add (new AnalyzeToken (in ));
221+ }
222+ }
223+ else {
224+ tokens = null ;
225+ }
226+ detail = in .readOptionalWriteable (DetailAnalyzeResponse ::new );
227+ }
228+
229+ @ Override
230+ public void readFrom (StreamInput in ) throws IOException {
231+ throw new UnsupportedOperationException ("usage of Streamable is to be replaced by Writeable" );
232+ }
233+
227234 public List <AnalyzeToken > getTokens () {
228235 return this .tokens ;
229236 }
@@ -268,20 +275,6 @@ public static AnalyzeResponse fromXContent(XContentParser parser) throws IOExcep
268275 return PARSER .parse (parser , null );
269276 }
270277
271- @ Override
272- public void readFrom (StreamInput in ) throws IOException {
273- super .readFrom (in );
274- int size = in .readVInt ();
275- tokens = new ArrayList <>(size );
276- for (int i = 0 ; i < size ; i ++) {
277- tokens .add (AnalyzeToken .readAnalyzeToken (in ));
278- }
279- if (tokens .size () == 0 ) {
280- tokens = null ;
281- }
282- detail = in .readOptionalStreamable (DetailAnalyzeResponse ::new );
283- }
284-
285278 @ Override
286279 public void writeTo (StreamOutput out ) throws IOException {
287280 super .writeTo (out );
@@ -293,7 +286,7 @@ public void writeTo(StreamOutput out) throws IOException {
293286 } else {
294287 out .writeVInt (0 );
295288 }
296- out .writeOptionalStreamable (detail );
289+ out .writeOptionalWriteable (detail );
297290 }
298291
299292 @ Override
0 commit comments