Skip to content

Commit 617aaba

Browse files
committed
Bug 37205327 - [37167822->24.09.1] CollectionExtractor - fix fromCollection methods (merge ce/main -> ce/24.09 @ 112106)
[git-p4: depot-paths = "//dev/coherence-ce/release/coherence-ce-v24.09/": change = 112110]
1 parent a62dcd3 commit 617aaba

File tree

3 files changed

+79
-58
lines changed

3 files changed

+79
-58
lines changed

prj/coherence-core/src/main/java/com/tangosol/util/Extractors.java

Lines changed: 16 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -250,56 +250,45 @@ public static <T, E> ValueExtractor<T, E> identityCast()
250250
}
251251

252252
/**
253-
* Returns a {@link CollectionExtractor} that extracts the specified fields
254-
* where extraction occurs in a chain where the result of each
255-
* field extraction is the input to the next extractor. The result
256-
* returned is the result of the final extractor in the chain.
253+
* Returns a {@link CollectionExtractor} that extracts the specified field.
257254
*
258-
* @param fields the field names to extract (if any field name contains a dot '.'
259-
* that field name is split into multiple field names delimiting on
260-
* the dots.
255+
* @param from the field name to extract the value from
261256
*
262257
* @param <T> the type of the object to extract from
263258
* @param <E> the type of the extracted value
264259
*
265-
* @return a {@link CollectionExtractor} that extracts the value(s) of the specified field(s)
260+
* @return a {@link CollectionExtractor} that extracts the value(s) of the specified field
266261
*
267-
* @throws IllegalArgumentException if the fields parameter is {@code null} or an
268-
* empty array
262+
* @throws IllegalArgumentException if the fields parameter is {@code null} or empty
269263
*
270264
* @see CollectionExtractor
271-
* @see ChainedExtractor
272265
*/
273-
public static <T, E> CollectionExtractor<T, E> fromCollection(String... fields)
266+
public static <T, E> CollectionExtractor<T, E> fromCollection(String from)
274267
{
275-
return new CollectionExtractor(chained(fields));
268+
if (Objects.requireNonNull(from).isBlank())
269+
{
270+
throw new IllegalArgumentException("The fromCollection parameter cannot be empty");
271+
}
272+
return new CollectionExtractor<>(extract(from));
276273
}
277274

278275
/**
279-
* Returns a {@link CollectionExtractor} that wraps the specified {@link ValueExtractor}s.
280-
* <p>
281-
* If the {@code extractors} parameter is a single {@link ValueExtractor} then a
282-
* {@link CollectionExtractor} is returned wrapping that extractor. If the {@code extractors} is
283-
* multiple {@link ValueExtractor} instances in a chain, a {@link CollectionExtractor} is returned
284-
* that wraps a {@link ChainedExtractor} that wraps the chain of {@link ValueExtractor}
285-
* instances
276+
* Returns a {@link CollectionExtractor} that wraps the specified {@link ValueExtractor}.
286277
*
287-
* @param extractors the chain of {@link ValueExtractor}s to use to extract the value
278+
* @param extractor the {@link ValueExtractor} to use to extract the value
288279
* @param <T> the type of the object to extract from
289280
* @param <E> the type of the extracted value
290281
*
291-
* @return a {@link CollectionExtractor} that extracts the value(s) of the specified field(s)
282+
* @return a {@link CollectionExtractor} that extracts the value(s) of the specified field
292283
*
293-
* @throws IllegalArgumentException if the fields parameter is {@code null} or an
294-
* empty array
284+
* @throws IllegalArgumentException if the fields parameter is {@code null}
295285
*
296286
* @see CollectionExtractor
297-
* @see ChainedExtractor
298287
*
299288
*/
300-
public static <T, E> CollectionExtractor<T, E> fromCollection(ValueExtractor<?, ?>... extractors)
289+
public static <T, E> CollectionExtractor<T, E> fromCollection(ValueExtractor<T, E> extractor)
301290
{
302-
return new CollectionExtractor(chained(extractors));
291+
return new CollectionExtractor<>(extractor);
303292
}
304293

305294
/**

prj/test/functional/extractor/src/main/java/extractor/ExtractorTests.java

Lines changed: 61 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
import java.io.IOException;
4949

5050
import java.util.Calendar;
51+
import java.util.Collection;
5152
import java.util.List;
5253
import java.util.Set;
5354

@@ -126,38 +127,11 @@ public void testReflection()
126127
public void testCollection()
127128
{
128129
NamedCache cache = getNamedCache();
129-
130-
Country usa = new Country("USA");
131-
usa.setArea(3_796_742);
132-
usa.setPopulation(334_914_895);
133-
usa.addCity(new City("New York", 8_258_035))
134-
.addCity(new City("Los Angeles", 3_820_914))
135-
.addCity(new City("Chicago", 2_664_452));
136-
137-
Country germany = new Country("Germany");
138-
germany.setArea(357_569);
139-
germany.setPopulation(82_719_540);
140-
germany.addCity(new City("Berlin", 3_677_472))
141-
.addCity(new City("Hamburg", 1_906_411))
142-
.addCity(new City("Munich", 1_487_708));
143-
144-
Country taiwan = new Country("Taiwan");
145-
taiwan.setArea(36_197);
146-
taiwan.setPopulation(23_894_394);
147-
taiwan.addCity(new City("New Taipei", 3_974_911))
148-
.addCity(new City("Kaohsiung", 2_778_992))
149-
.addCity(new City("Taichung", 2_759_887))
150-
.addCity(new City("Taipei", 2_696_316));
151-
152-
cache.put("us", usa);
153-
cache.put("de", germany);
154-
cache.put("tw", taiwan);
155-
156-
cache.values(Filters.in(Extractors.extract("name"), Set.of("USA", "Germany"))).size();
130+
addTestCountriesToCache(cache);
157131

158132
ValueExtractor<Country, String> countryNameExtractor = Extractors.extract("name");
159133

160-
Filter countryFilter = Filters.in(Extractors.extract("name"), "USA", "Germany");
134+
Filter countryFilter = Filters.in(countryNameExtractor, "USA", "Germany");
161135

162136
ValueExtractor<Country, List<City>> listOfCitiesExtractor = Extractors.extract("cities");
163137
ValueExtractor<City, String> cityNameExtractor = Extractors.extract("name");
@@ -178,6 +152,35 @@ public void testCollection()
178152
cache.destroy();
179153
}
180154

155+
/**
156+
* Test for {@link CollectionExtractor}.
157+
*/
158+
@Test
159+
public void testCollectionInTypesafeManner()
160+
{
161+
NamedCache cache = getNamedCache();
162+
addTestCountriesToCache(cache);
163+
164+
ValueExtractor<Country, String> countryNameExtractor = ValueExtractor.of(Country::getName);
165+
166+
Filter countryFilter = Filters.in(countryNameExtractor, "USA", "Germany");
167+
168+
ValueExtractor<Country, Collection<String>> listOfCityNamesExtractor = ValueExtractor.of(Country::getCities)
169+
.andThen(Extractors.fromCollection(City::getName));
170+
171+
List<List<String>> listCityNames = cache.stream(countryFilter, listOfCityNamesExtractor).toList();
172+
173+
assertEquals("Expected 2 results (Countries) but got " + listCityNames.size(), 2, listCityNames.size());
174+
175+
List<String> listJustCities = listCityNames.stream().flatMap(list -> list.stream()).toList();
176+
177+
assertEquals("Expected 6 cities but got " + listJustCities.size(), 6, listJustCities.size());
178+
179+
assertThat(listJustCities, hasItems("Berlin", "Hamburg", "Munich", "Los Angeles", "New York", "Chicago"));
180+
181+
cache.destroy();
182+
}
183+
181184
/**
182185
* Test for {@link DeserializationAccelerator}.
183186
*/
@@ -444,6 +447,35 @@ public void writeExternal(PofWriter out)
444447
protected static final AtomicInteger f_deserializationCount = new AtomicInteger();
445448
}
446449

450+
private void addTestCountriesToCache(NamedCache cache)
451+
{
452+
Country usa = new Country("USA");
453+
usa.setArea(3_796_742);
454+
usa.setPopulation(334_914_895);
455+
usa.addCity(new City("New York", 8_258_035))
456+
.addCity(new City("Los Angeles", 3_820_914))
457+
.addCity(new City("Chicago", 2_664_452));
458+
459+
Country germany = new Country("Germany");
460+
germany.setArea(357_569);
461+
germany.setPopulation(82_719_540);
462+
germany.addCity(new City("Berlin", 3_677_472))
463+
.addCity(new City("Hamburg", 1_906_411))
464+
.addCity(new City("Munich", 1_487_708));
465+
466+
Country taiwan = new Country("Taiwan");
467+
taiwan.setArea(36_197);
468+
taiwan.setPopulation(23_894_394);
469+
taiwan.addCity(new City("New Taipei", 3_974_911))
470+
.addCity(new City("Kaohsiung", 2_778_992))
471+
.addCity(new City("Taichung", 2_759_887))
472+
.addCity(new City("Taipei", 2_696_316));
473+
474+
cache.put("us", usa);
475+
cache.put("de", germany);
476+
cache.put("tw", taiwan);
477+
}
478+
447479
// ----- inner class: CountingExtractor ---------------------------------
448480

449481
public static class CountingExtractor

prj/test/unit/coherence-tests/src/test/java/com/tangosol/util/extractor/CollectionExtractorTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public void testUseExtractorsFromCollectionWithStringArguments()
8787
@Test(expected = IllegalArgumentException.class)
8888
public void testUseExtractorsFromCollectionWithEmptyStringParams()
8989
{
90-
Extractors.fromCollection(new String[]{});
90+
Extractors.fromCollection("");
9191
}
9292

9393
@Test
@@ -108,7 +108,7 @@ public void testUseExtractorsFromCollection()
108108
@Test(expected = IllegalArgumentException.class)
109109
public void testUseExtractorsFromCollectionWithEmptyParams()
110110
{
111-
Extractors.fromCollection(new UniversalExtractor[]{});
111+
Extractors.fromCollection((ValueExtractor) null);
112112
}
113113
}
114114

0 commit comments

Comments
 (0)