|
1 | 1 | package com.algolia.model.recommend;
|
2 | 2 |
|
3 |
| -import com.google.gson.annotations.SerializedName; |
4 |
| -import java.util.ArrayList; |
| 3 | +import com.algolia.utils.CompoundType; |
| 4 | +import com.algolia.utils.JSON; |
| 5 | +import com.google.gson.TypeAdapter; |
| 6 | +import com.google.gson.annotations.JsonAdapter; |
| 7 | +import com.google.gson.reflect.TypeToken; |
| 8 | +import com.google.gson.stream.JsonReader; |
| 9 | +import com.google.gson.stream.JsonWriter; |
| 10 | +import java.io.IOException; |
5 | 11 | import java.util.List;
|
6 |
| -import java.util.Objects; |
7 | 12 |
|
8 |
| -/** Highlighted attributes. */ |
9 |
| -public class HighlightResult { |
| 13 | +@JsonAdapter(HighlightResult.Adapter.class) |
| 14 | +/** HighlightResult */ |
| 15 | +public abstract class HighlightResult implements CompoundType { |
10 | 16 |
|
11 |
| - @SerializedName("value") |
12 |
| - private String value; |
13 |
| - |
14 |
| - @SerializedName("matchLevel") |
15 |
| - private MatchLevel matchLevel; |
16 |
| - |
17 |
| - @SerializedName("matchedWords") |
18 |
| - private List<String> matchedWords; |
19 |
| - |
20 |
| - @SerializedName("fullyHighlighted") |
21 |
| - private Boolean fullyHighlighted; |
22 |
| - |
23 |
| - public HighlightResult setValue(String value) { |
24 |
| - this.value = value; |
25 |
| - return this; |
| 17 | + public static HighlightResult of(HighlightResultOption inside) { |
| 18 | + return new HighlightResultHighlightResultOption(inside); |
26 | 19 | }
|
27 | 20 |
|
28 |
| - /** |
29 |
| - * Markup text with occurrences highlighted. |
30 |
| - * |
31 |
| - * @return value |
32 |
| - */ |
33 |
| - @javax.annotation.Nullable |
34 |
| - public String getValue() { |
35 |
| - return value; |
| 21 | + public static HighlightResult of(List<HighlightResultOption> inside) { |
| 22 | + return new HighlightResultListOfHighlightResultOption(inside); |
36 | 23 | }
|
37 | 24 |
|
38 |
| - public HighlightResult setMatchLevel(MatchLevel matchLevel) { |
39 |
| - this.matchLevel = matchLevel; |
40 |
| - return this; |
41 |
| - } |
| 25 | + public static class Adapter extends TypeAdapter<HighlightResult> { |
42 | 26 |
|
43 |
| - /** |
44 |
| - * Get matchLevel |
45 |
| - * |
46 |
| - * @return matchLevel |
47 |
| - */ |
48 |
| - @javax.annotation.Nullable |
49 |
| - public MatchLevel getMatchLevel() { |
50 |
| - return matchLevel; |
51 |
| - } |
52 |
| - |
53 |
| - public HighlightResult setMatchedWords(List<String> matchedWords) { |
54 |
| - this.matchedWords = matchedWords; |
55 |
| - return this; |
56 |
| - } |
| 27 | + @Override |
| 28 | + public void write(final JsonWriter out, final HighlightResult oneOf) throws IOException { |
| 29 | + TypeAdapter runtimeTypeAdapter = (TypeAdapter) JSON.getGson().getAdapter(TypeToken.get(oneOf.getInsideValue().getClass())); |
| 30 | + runtimeTypeAdapter.write(out, oneOf.getInsideValue()); |
| 31 | + } |
57 | 32 |
|
58 |
| - public HighlightResult addMatchedWords(String matchedWordsItem) { |
59 |
| - if (this.matchedWords == null) { |
60 |
| - this.matchedWords = new ArrayList<>(); |
| 33 | + @Override |
| 34 | + public HighlightResult read(final JsonReader jsonReader) throws IOException { |
| 35 | + HighlightResultOption highlightresultoption = JSON.tryDeserialize(jsonReader, new TypeToken<HighlightResultOption>() {}.getType()); |
| 36 | + if (highlightresultoption != null) { |
| 37 | + return HighlightResult.of(highlightresultoption); |
| 38 | + } |
| 39 | + List<HighlightResultOption> listofhighlightresultoption = JSON.tryDeserialize( |
| 40 | + jsonReader, |
| 41 | + new TypeToken<List<HighlightResultOption>>() {}.getType() |
| 42 | + ); |
| 43 | + if (listofhighlightresultoption != null) { |
| 44 | + return HighlightResult.of(listofhighlightresultoption); |
| 45 | + } |
| 46 | + return null; |
61 | 47 | }
|
62 |
| - this.matchedWords.add(matchedWordsItem); |
63 |
| - return this; |
64 | 48 | }
|
| 49 | +} |
65 | 50 |
|
66 |
| - /** |
67 |
| - * List of words from the query that matched the object. |
68 |
| - * |
69 |
| - * @return matchedWords |
70 |
| - */ |
71 |
| - @javax.annotation.Nullable |
72 |
| - public List<String> getMatchedWords() { |
73 |
| - return matchedWords; |
74 |
| - } |
| 51 | +@JsonAdapter(HighlightResult.Adapter.class) |
| 52 | +class HighlightResultHighlightResultOption extends HighlightResult { |
75 | 53 |
|
76 |
| - public HighlightResult setFullyHighlighted(Boolean fullyHighlighted) { |
77 |
| - this.fullyHighlighted = fullyHighlighted; |
78 |
| - return this; |
79 |
| - } |
| 54 | + private final HighlightResultOption insideValue; |
80 | 55 |
|
81 |
| - /** |
82 |
| - * Whether the entire attribute value is highlighted. |
83 |
| - * |
84 |
| - * @return fullyHighlighted |
85 |
| - */ |
86 |
| - @javax.annotation.Nullable |
87 |
| - public Boolean getFullyHighlighted() { |
88 |
| - return fullyHighlighted; |
| 56 | + HighlightResultHighlightResultOption(HighlightResultOption insideValue) { |
| 57 | + this.insideValue = insideValue; |
89 | 58 | }
|
90 | 59 |
|
91 | 60 | @Override
|
92 |
| - public boolean equals(Object o) { |
93 |
| - if (this == o) { |
94 |
| - return true; |
95 |
| - } |
96 |
| - if (o == null || getClass() != o.getClass()) { |
97 |
| - return false; |
98 |
| - } |
99 |
| - HighlightResult highlightResult = (HighlightResult) o; |
100 |
| - return ( |
101 |
| - Objects.equals(this.value, highlightResult.value) && |
102 |
| - Objects.equals(this.matchLevel, highlightResult.matchLevel) && |
103 |
| - Objects.equals(this.matchedWords, highlightResult.matchedWords) && |
104 |
| - Objects.equals(this.fullyHighlighted, highlightResult.fullyHighlighted) |
105 |
| - ); |
| 61 | + public HighlightResultOption getInsideValue() { |
| 62 | + return insideValue; |
106 | 63 | }
|
| 64 | +} |
107 | 65 |
|
108 |
| - @Override |
109 |
| - public int hashCode() { |
110 |
| - return Objects.hash(value, matchLevel, matchedWords, fullyHighlighted); |
111 |
| - } |
| 66 | +@JsonAdapter(HighlightResult.Adapter.class) |
| 67 | +class HighlightResultListOfHighlightResultOption extends HighlightResult { |
112 | 68 |
|
113 |
| - @Override |
114 |
| - public String toString() { |
115 |
| - StringBuilder sb = new StringBuilder(); |
116 |
| - sb.append("class HighlightResult {\n"); |
117 |
| - sb.append(" value: ").append(toIndentedString(value)).append("\n"); |
118 |
| - sb.append(" matchLevel: ").append(toIndentedString(matchLevel)).append("\n"); |
119 |
| - sb.append(" matchedWords: ").append(toIndentedString(matchedWords)).append("\n"); |
120 |
| - sb.append(" fullyHighlighted: ").append(toIndentedString(fullyHighlighted)).append("\n"); |
121 |
| - sb.append("}"); |
122 |
| - return sb.toString(); |
| 69 | + private final List<HighlightResultOption> insideValue; |
| 70 | + |
| 71 | + HighlightResultListOfHighlightResultOption(List<HighlightResultOption> insideValue) { |
| 72 | + this.insideValue = insideValue; |
123 | 73 | }
|
124 | 74 |
|
125 |
| - /** |
126 |
| - * Convert the given object to string with each line indented by 4 spaces (except the first line). |
127 |
| - */ |
128 |
| - private String toIndentedString(Object o) { |
129 |
| - if (o == null) { |
130 |
| - return "null"; |
131 |
| - } |
132 |
| - return o.toString().replace("\n", "\n "); |
| 75 | + @Override |
| 76 | + public List<HighlightResultOption> getInsideValue() { |
| 77 | + return insideValue; |
133 | 78 | }
|
134 | 79 | }
|
0 commit comments