4848import java .io .IOException ;
4949
5050import java .util .Calendar ;
51+ import java .util .Collection ;
5152import java .util .List ;
5253import 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
0 commit comments