Skip to content

Commit ace62dc

Browse files
committed
Simplify collection setters
1 parent 5dafa5b commit ace62dc

File tree

3 files changed

+22
-65
lines changed

3 files changed

+22
-65
lines changed

java-client/src/main/java/co/elastic/clients/util/ApiTypeHelper.java

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import javax.annotation.Nullable;
2323
import java.util.AbstractList;
2424
import java.util.AbstractMap;
25-
import java.util.Collection;
2625
import java.util.Collections;
2726
import java.util.Iterator;
2827
import java.util.List;
@@ -100,7 +99,6 @@ public Iterator<Object> iterator() {
10099
};
101100

102101
static final List<Object> UNDEFINED_LIST = new EmptyList();
103-
static final List<Object> RESET_LIST = new EmptyList();
104102

105103
/**
106104
* Returns an empty list that is undefined from a JSON perspective. It will not be serialized
@@ -111,19 +109,6 @@ public static <T> List<T> undefinedList() {
111109
return (List<T>)UNDEFINED_LIST;
112110
}
113111

114-
/**
115-
* Returns a "magic list" that will reset list properties of API types to {@code null}.
116-
* <p>
117-
* List setters on API type builders add items to the property using {@link List#addAll(Collection)} and do not accept
118-
* a {@code null} value which is considered as a bug. In some rare occasions it may however be necessary to reset
119-
* the builder's property, and this is what the result of this method is meant for: setting a builder's list property
120-
* to the value returned by this method will reset that property to {@code null}.
121-
*/
122-
@SuppressWarnings("unchecked")
123-
public static <T> List<T> resetList() {
124-
return (List<T>)RESET_LIST;
125-
}
126-
127112
/**
128113
* Is {@code list} defined according to JSON/JavaScript semantics?
129114
*
@@ -167,7 +152,6 @@ public Set<Entry<Object, Object>> entrySet() {
167152
}
168153

169154
static final Map<Object, Object> UNDEFINED_MAP = new EmptyMap();
170-
static final Map<Object, Object> RESET_MAP = new EmptyMap();
171155

172156
/**
173157
* Returns an empty list that is undefined from a JSON perspective. It will not be serialized
@@ -178,19 +162,6 @@ public static <K, V> Map<K, V> undefinedMap() {
178162
return (Map<K, V>)UNDEFINED_MAP;
179163
}
180164

181-
/**
182-
* Returns a "magic map" that will reset map properties of API types to {@code null}.
183-
* <p>
184-
* Map setters on API type builders add entries to the property using {@link Map#putAll(Map)} and do not accept
185-
* a {@code null} value which is considered as a bug. In some rare occasions it may however be necessary to reset
186-
* the builder's property, and this is what the result of this method is meant for: setting a builder's map property
187-
* to the value returned by this method will reset that property to {@code null}.
188-
*/
189-
@SuppressWarnings("unchecked")
190-
public static <K, V> Map<K, V> resetMap() {
191-
return (Map<K, V>)RESET_MAP;
192-
}
193-
194165
/**
195166
* Is {@code map} defined according to JSON/JavaScript semantics?
196167
*

java-client/src/main/java/co/elastic/clients/util/ObjectBuilderBase.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import java.util.HashMap;
2626
import java.util.List;
2727
import java.util.Map;
28+
import java.util.Objects;
2829

2930
public class ObjectBuilderBase {
3031
private boolean _used = false;
@@ -76,9 +77,7 @@ protected static <T> List<T> _listAddAll(List<T> list, List<T> values) {
7677
if (list == null) {
7778
// Keep the original list to avoid an unnecessary copy.
7879
// It will be copied if we add more values.
79-
return values;
80-
} else if (values == ApiTypeHelper.RESET_LIST) {
81-
return null;
80+
return Objects.requireNonNull(values);
8281
} else {
8382
list = _mutableList(list);
8483
list.addAll(values);
@@ -119,11 +118,14 @@ protected static <K, V> Map<K, V> _mapPut(Map<K, V> map, K key, V value) {
119118

120119
/** Add all elements of a list to a (possibly {@code null}) map */
121120
protected static <K, V> Map<K, V> _mapPutAll(Map<K, V> map, Map<K, V> entries) {
122-
if (entries == ApiTypeHelper.RESET_MAP) {
123-
return null;
121+
if (map == null) {
122+
// Keep the original map to avoid an unnecessary copy.
123+
// It will be copied if we add more entries.
124+
return Objects.requireNonNull(entries);
125+
} else {
126+
map = _mutableMap(map);
127+
map.putAll(entries);
128+
return map;
124129
}
125-
map = _mutableMap(map);
126-
map.putAll(entries);
127-
return map;
128130
}
129131
}

java-client/src/test/java/co/elastic/clients/elasticsearch/model/ClassStructureTest.java

Lines changed: 12 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -214,19 +214,13 @@ public void testListSetters() {
214214

215215

216216
{
217-
// Reset builder property
218-
SearchRequest search = SearchRequest.of(b -> b
219-
.storedFields(fields)
220-
.storedFields(ApiTypeHelper.resetList())
221-
);
222-
assertFalse(ApiTypeHelper.isDefined(search.storedFields()));
223-
224-
search = SearchRequest.of(b -> b
225-
.storedFields(fields)
226-
.storedFields(ApiTypeHelper.resetList())
227-
.storedFields("d", "e")
228-
);
229-
assertEquals(Arrays.asList("d", "e"), search.storedFields());
217+
// List cannot be null
218+
List<String> nullFields = null;
219+
assertThrows(NullPointerException.class, () -> {
220+
SearchRequest.of(b -> b
221+
.storedFields(nullFields)
222+
);
223+
});
230224
}
231225

232226
{
@@ -255,7 +249,6 @@ public void testMapSetters() {
255249

256250
{
257251
// Appending doesn't modify the original collection
258-
259252
SearchRequest search = SearchRequest.of(b -> b
260253
.aggregations(aggs)
261254
.aggregations("aggC", countC._toAggregation())
@@ -273,20 +266,11 @@ public void testMapSetters() {
273266
}
274267

275268
{
276-
// Reset builder property
277-
SearchRequest search = SearchRequest.of(b -> b
278-
.aggregations(aggs)
279-
.aggregations(ApiTypeHelper.resetMap())
280-
);
281-
assertFalse(ApiTypeHelper.isDefined(search.storedFields()));
282-
283-
search = SearchRequest.of(b -> b
284-
.aggregations(aggs)
285-
.aggregations(ApiTypeHelper.resetMap())
286-
.aggregations("aggC", countC._toAggregation())
287-
);
288-
assertEquals(1, search.aggregations().size());
289-
assertEquals("c", search.aggregations().get("aggC").valueCount().field());
269+
// Map cannot be null
270+
assertThrows(NullPointerException.class, () -> {
271+
Map<String, Aggregation> nullMap = null;
272+
SearchRequest.of(b -> b.aggregations(nullMap));
273+
});
290274
}
291275
}
292276

0 commit comments

Comments
 (0)