Skip to content

Commit

Permalink
Bug 37329495 - [37167822->14.1.2.0.1] Fix the fromCollection methods …
Browse files Browse the repository at this point in the history
…in CollectionExtractor to properly support method references (14.1.2.0 cl 112640 --> ce/14.1.2.0)

[git-p4: depot-paths = "//dev/coherence-ce/release/coherence-ce-v14.1.2.0/": change = 113200]
  • Loading branch information
fryp committed Jan 5, 2025
1 parent 2828376 commit b38f3c7
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 58 deletions.
43 changes: 16 additions & 27 deletions prj/coherence-core/src/main/java/com/tangosol/util/Extractors.java
Original file line number Diff line number Diff line change
Expand Up @@ -250,56 +250,45 @@ public static <T, E> ValueExtractor<T, E> identityCast()
}

/**
* Returns a {@link CollectionExtractor} that extracts the specified fields
* where extraction occurs in a chain where the result of each
* field extraction is the input to the next extractor. The result
* returned is the result of the final extractor in the chain.
* Returns a {@link CollectionExtractor} that extracts the specified field.
*
* @param fields the field names to extract (if any field name contains a dot '.'
* that field name is split into multiple field names delimiting on
* the dots.
* @param from the field name to extract the value from
*
* @param <T> the type of the object to extract from
* @param <E> the type of the extracted value
*
* @return a {@link CollectionExtractor} that extracts the value(s) of the specified field(s)
* @return a {@link CollectionExtractor} that extracts the value(s) of the specified field
*
* @throws IllegalArgumentException if the fields parameter is {@code null} or an
* empty array
* @throws IllegalArgumentException if the fields parameter is {@code null} or empty
*
* @see CollectionExtractor
* @see ChainedExtractor
*/
public static <T, E> CollectionExtractor<T, E> fromCollection(String... fields)
public static <T, E> CollectionExtractor<T, E> fromCollection(String from)
{
return new CollectionExtractor(chained(fields));
if (Objects.requireNonNull(from).isBlank())
{
throw new IllegalArgumentException("The fromCollection parameter cannot be empty");
}
return new CollectionExtractor<>(extract(from));
}

/**
* Returns a {@link CollectionExtractor} that wraps the specified {@link ValueExtractor}s.
* <p>
* If the {@code extractors} parameter is a single {@link ValueExtractor} then a
* {@link CollectionExtractor} is returned wrapping that extractor. If the {@code extractors} is
* multiple {@link ValueExtractor} instances in a chain, a {@link CollectionExtractor} is returned
* that wraps a {@link ChainedExtractor} that wraps the chain of {@link ValueExtractor}
* instances
* Returns a {@link CollectionExtractor} that wraps the specified {@link ValueExtractor}.
*
* @param extractors the chain of {@link ValueExtractor}s to use to extract the value
* @param extractor the {@link ValueExtractor} to use to extract the value
* @param <T> the type of the object to extract from
* @param <E> the type of the extracted value
*
* @return a {@link CollectionExtractor} that extracts the value(s) of the specified field(s)
* @return a {@link CollectionExtractor} that extracts the value(s) of the specified field
*
* @throws IllegalArgumentException if the fields parameter is {@code null} or an
* empty array
* @throws IllegalArgumentException if the fields parameter is {@code null}
*
* @see CollectionExtractor
* @see ChainedExtractor
*
*/
public static <T, E> CollectionExtractor<T, E> fromCollection(ValueExtractor<?, ?>... extractors)
public static <T, E> CollectionExtractor<T, E> fromCollection(ValueExtractor<T, E> extractor)
{
return new CollectionExtractor(chained(extractors));
return new CollectionExtractor<>(extractor);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import java.io.IOException;

import java.util.Calendar;
import java.util.Collection;
import java.util.List;
import java.util.Set;

Expand Down Expand Up @@ -126,38 +127,11 @@ public void testReflection()
public void testCollection()
{
NamedCache cache = getNamedCache();

Country usa = new Country("USA");
usa.setArea(3_796_742);
usa.setPopulation(334_914_895);
usa.addCity(new City("New York", 8_258_035))
.addCity(new City("Los Angeles", 3_820_914))
.addCity(new City("Chicago", 2_664_452));

Country germany = new Country("Germany");
germany.setArea(357_569);
germany.setPopulation(82_719_540);
germany.addCity(new City("Berlin", 3_677_472))
.addCity(new City("Hamburg", 1_906_411))
.addCity(new City("Munich", 1_487_708));

Country taiwan = new Country("Taiwan");
taiwan.setArea(36_197);
taiwan.setPopulation(23_894_394);
taiwan.addCity(new City("New Taipei", 3_974_911))
.addCity(new City("Kaohsiung", 2_778_992))
.addCity(new City("Taichung", 2_759_887))
.addCity(new City("Taipei", 2_696_316));

cache.put("us", usa);
cache.put("de", germany);
cache.put("tw", taiwan);

cache.values(Filters.in(Extractors.extract("name"), Set.of("USA", "Germany"))).size();
addTestCountriesToCache(cache);

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

Filter countryFilter = Filters.in(Extractors.extract("name"), "USA", "Germany");
Filter countryFilter = Filters.in(countryNameExtractor, "USA", "Germany");

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

/**
* Test for {@link CollectionExtractor}.
*/
@Test
public void testCollectionInTypesafeManner()
{
NamedCache cache = getNamedCache();
addTestCountriesToCache(cache);

ValueExtractor<Country, String> countryNameExtractor = ValueExtractor.of(Country::getName);

Filter countryFilter = Filters.in(countryNameExtractor, "USA", "Germany");

ValueExtractor<Country, Collection<String>> listOfCityNamesExtractor = ValueExtractor.of(Country::getCities)
.andThen(Extractors.fromCollection(City::getName));

List<List<String>> listCityNames = cache.stream(countryFilter, listOfCityNamesExtractor).toList();

assertEquals("Expected 2 results (Countries) but got " + listCityNames.size(), 2, listCityNames.size());

List<String> listJustCities = listCityNames.stream().flatMap(list -> list.stream()).toList();

assertEquals("Expected 6 cities but got " + listJustCities.size(), 6, listJustCities.size());

assertThat(listJustCities, hasItems("Berlin", "Hamburg", "Munich", "Los Angeles", "New York", "Chicago"));

cache.destroy();
}

/**
* Test for {@link DeserializationAccelerator}.
*/
Expand Down Expand Up @@ -444,6 +447,35 @@ public void writeExternal(PofWriter out)
protected static final AtomicInteger f_deserializationCount = new AtomicInteger();
}

private void addTestCountriesToCache(NamedCache cache)
{
Country usa = new Country("USA");
usa.setArea(3_796_742);
usa.setPopulation(334_914_895);
usa.addCity(new City("New York", 8_258_035))
.addCity(new City("Los Angeles", 3_820_914))
.addCity(new City("Chicago", 2_664_452));

Country germany = new Country("Germany");
germany.setArea(357_569);
germany.setPopulation(82_719_540);
germany.addCity(new City("Berlin", 3_677_472))
.addCity(new City("Hamburg", 1_906_411))
.addCity(new City("Munich", 1_487_708));

Country taiwan = new Country("Taiwan");
taiwan.setArea(36_197);
taiwan.setPopulation(23_894_394);
taiwan.addCity(new City("New Taipei", 3_974_911))
.addCity(new City("Kaohsiung", 2_778_992))
.addCity(new City("Taichung", 2_759_887))
.addCity(new City("Taipei", 2_696_316));

cache.put("us", usa);
cache.put("de", germany);
cache.put("tw", taiwan);
}

// ----- inner class: CountingExtractor ---------------------------------

public static class CountingExtractor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public void testUseExtractorsFromCollectionWithStringArguments()
@Test(expected = IllegalArgumentException.class)
public void testUseExtractorsFromCollectionWithEmptyStringParams()
{
Extractors.fromCollection(new String[]{});
Extractors.fromCollection("");
}

@Test
Expand All @@ -108,7 +108,7 @@ public void testUseExtractorsFromCollection()
@Test(expected = IllegalArgumentException.class)
public void testUseExtractorsFromCollectionWithEmptyParams()
{
Extractors.fromCollection(new UniversalExtractor[]{});
Extractors.fromCollection((ValueExtractor) null);
}
}

Expand Down

0 comments on commit b38f3c7

Please sign in to comment.