How to implement generic Function method parameters?

crmepham :

Currently I have to define different methods based on the type of the Function I want to pass into the method, e.g:

addCustomParameter(orderLinePrefix + "quantity=", concatenateLong(orderLines, OrderPart::getQuantity));
addCustomParameter(orderLinePrefix + "surcharge=", concatenateDouble(orderLines, OrderPart::getSurcharge));
addCustomParameter(orderLinePrefix + "dropship=", concatenateBoolean(orderLines, OrderPart::isDropship));

The methods look like this:

@Nonnull
    public <T> String concatenateBoolean(@Nonnull final Collection<T> items, @Nonnull final Function<T, Boolean> mapper)
    {
        return items.stream()
                .map(mapper)
                .map(String::valueOf)
                .collect(joining(","));
    }

    @Nonnull
    public <T> String concatenateLong(@Nonnull final Collection<T> items, @Nonnull final Function<T, Long> mapper)
    {
        return items.stream()
                .map(mapper)
                .map(String::valueOf)
                .collect(joining(","));
    }

    @Nonnull
    public <T> String concatenateDouble(@Nonnull final Collection<T> items, @Nonnull final Function<T, Double> mapper)
    {
        return items.stream()
                .map(mapper)
                .map(String::valueOf)
                .collect(joining(","));
    }

How can I refactor the above 3 methods into a single method that will accept any type and return a String?

Andy Turner :

Use a wildcard return type for the mapper:

@Nonnull
public <T> String concatenateAny(@Nonnull final Collection<T> items, @Nonnull final Function<? super T, ?> mapper)
{
    return items.stream()
            .map(mapper)
            .map(String::valueOf)
            .collect(joining(","));
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=135294&siteId=1