Passing a functional interface as a parameter

Uzi :

I'm pretty new with functional interface and have no idea how to do this when passing it as a parameter. I hope you could help me with it. So I have this class called NumberValidation:

public class NumberValidation {

    public static Predicate<CommonNumber> isExisting(Function<CommonNumber, CommonNumber> retrieve){
        return (p ->{
            return Optional.ofNullable(retrieve.apply(p)).isPresent();
        });
    }

    public static Predicate<CommonNumber> isNotExisting(Function<CommonNumber, CommonNumber> retrieve){
        return (p ->{
            return !Optional.ofNullable(retrieve.apply(p)).isPresent();
        });
    }
}

As you can see I have two functions namely isExisting and isNotExisting with a parameter Function<CommomNumber, CommonNumber>. Say I want to use one of these functions like this:

public CommonNumber apply(CommonNumber t) {
    return Optional.of(t).filter(p -> {
        return NumberValidation.isExisting(//parameter here);
    });
}

I have no idea how I'm going to pass that parameter. I tried return NumberValidation.isExisting(t) and as well as return NumberValidation.isExisting(p) but I keep getting an error since the required parameter is a Function<CommonNumber,CommonNumber>

Steve :

I'm not sure what you're trying to do here. I see three problems with your code, the first of which maybe answers your question:

1) You need to pass a Function<CommonNumber, CommonNumber> into numberValidating.isExisting(). That is, a function that takes a CommonNumber and returns a CommonNumber. I don't know where that's supposed to come from. This seems to be the problem you're asking about, but if you had one of those, or you constructed one with a Lambda, you should have no problem passing that where you have //parameter here.

2) You are passing a Lambda that returns a Predicate into your filter() method, but filter() takes a Predicate. So I think you don't want the extra Lambda. I think you want to directly pass the result of calling NumberValidation.isExisting().

3) Your call to filter() will return an Optional<CommonNumber> but you're trying to return that as a CommonNumber. So you need to get the CommonNumber out of the Optional and return that.

Applying these three ideas to your code, here is something that compiles. I figure you probably want to be passing in a more interesting function than p -> p. Also, I don't know if you want to check the Optional and pass back something different if it doesn't contain a CommonNumber. Anyway, this should get you started:

public CommonNumber apply(CommonNumber t) {
    return Optional.of(t).filter(NumberValidation.isExisting(p -> p)).get();
}

Your NumberValidation class seems OK as is.

In case the p -> p doesn't fully answer how you'd pass a Function<CommonNumber, CommonNumber> into your method, here's another example that more explicitly creates such a function:

static CommonNumber someCommonNumberProcessor(CommonNumber cn) {
    return cn;
}

public CommonNumber apply(CommonNumber t) {
    return Optional.of(t).filter(NumberValidation.isExisting(Test2::someCommonNumberProcessor)).get();
}

This is all inside a class named Test2.

Guess you like

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