2

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>

1
  • I'm trying to use the isExisting function and pass a Function<CommonNumber, CommonNumber> parameter Commented Apr 29, 2019 at 0:51

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

6 Comments

Thank you for this informative response. I'm not very familiar in using functional interface so I'm kinda doing a trial and error (and google search) for this one. This really helped me a lot
There's a really good book I recommend..."manning.com/books/java-8-in-action". It's how I got started with all the new Functional stuff in Java, including Streams. Streams are so super cool. Just working through the examples in the book will get you super excited. Highly recommended.
I just noticed that on that page it says that there's a newer version available. I'll have to check that out. I have an Oreilly Learning subscription that gives me access to almost every tech book, so I'm spoiled :) . But maybe this means you could find a copy of the original really cheaply, and it's still a great book for learning this stuff.
...or sign up for a free trial of Oreilly, lol.
An expression like Optional.of(t).filter(…).get() is broken, regardless of what predicate you’ll use. The result is either, the same t as before or an exception, as a rejected value results in an empty optional which has no value to return on get(). There’s no way to fix it here, as the task itself is logically flawed, a function returning whatever based on a filter that is based on the function, a perfect circle without an actual application logic.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.