I am learning about FunctionalInterface which is present in Java 8. After doing some basic Functional examples, I tried to do the same with GenericType parameters.
public class Main {
public enum LocType {
Area, Country
}
public <T> Function<T, T> getCreateFunction(LocType type) {
AreaService areaService = new AreaService();
CountryService countryService = new CountryService();
switch(type) {
case Area : return areaService::createArea;
case Country : return countryService::createCountry;
default : return null;
}
}
}
public class AreaService {
public Area createArea(Area area) {
// some logic
return area;
}
}
public class CountryService {
public Country createCountry(Country country) {
// some logic
return country;
}
}
// Area & Country are Model Classes
But eclipse compiler throws error as
The type AreaService does not define createArea(T) that is applicable here
Isn't it possible to define Generic Type Parameters in FunctionalInterface..?
createArea()andcreateCountry()are. Also, you might want to considerUnaryOperator<T>in place ofFunction<T, T>(assuming that's correct here).LocTypewith valuesAreaandCountry, but from the comments in the code it appearsAreaandCountryare classes. And, as previously commented, what are the return types ofcreateAreaandcreateCountry. (Maybe just post the API forAreaServiceandCountryService...)