deadfire19
deadfire19

Reputation: 329

Java 8 Supplier without double colon

I am unable to create a Supplier without using :: notation. All the tutorials seem to be using :: to get the method out.

Function<String, Object> beanFactory = m_context::getBean;
Function<Void, IRCPublic> ircPublicSupplier = a -> (IRCPublic) beanFactory.apply("developerPublicConnection");
ircPublicSupplier.apply(null);

How would I go about turning line 2 into a single line statement to create a Supplier, and simply being able to use '.get()' . I can use '.apply(null)' but that just seems dirty.

Thank you!

Upvotes: 3

Views: 266

Answers (2)

fps
fps

Reputation: 34460

You could do it inline:

Supplier<IRCPublic> ircPublicSupplier = 
    () -> (IRCPublic) beanFactory.apply("developerPublicConnection");

Or you could also wrap the IRCPublic instance returned by the beanFactory in a method:

IRCPublic getIrcPublic() {
    return (IRCPublic) beanFactory.apply("developerPublicConnection");
}

And then use the :::

Supplier<IRCPublic> ircPublicSupplier = this::getIrcPublic;

This assumes that the above line is in the same class where the getIrcPublic method is defined.

Upvotes: 3

deadfire19
deadfire19

Reputation: 329

Intellij to the rescue. I defined it the long way round:

Supplier<IRCPublic> ircPublicSupplier = new Supplier<IRCPublic>() {
        @Override
        public IRCPublic get() {
            return (IRCPublic) beanFactory.apply("developerPublicConnection");
        }
    };

And Intellij shortened it to:

Supplier<IRCPublic> ircPublicSupplier = () -> (IRCPublic) beanFactory.apply("developerPublicConnection");

Upvotes: 6

Related Questions