7 Replies - 780 Views - Last Post: 20 May 2014 - 10:13 AM Rate Topic: -----

#1 cfoley   User is offline

  • Cabbage
  • member icon

Reputation: 2425
  • View blog
  • Posts: 5,068
  • Joined: 11-December 07

Help me find the functional interface

Posted 20 May 2014 - 03:58 AM

I've been looking through the functional interfaces for something like this (It's part of a REPL):

public interface ActionInterface {
	void call();
}


I want to use it like this:

	public void setQuitAction(ActionInterface action) {
		quitAction = action;
	}

// and somewhere else:

evaluator.setQuitAction(() -> isLooping = false);



I feel like there should be something like ActionInterface included with all those functional interfaces. All I can come up with is Runnable, which makes you think of threads not lambdas. What's the way to do this in Java?

Spoiler


Is This A Good Question/Topic? 0
  • +

Replies To: Help me find the functional interface

#2 Flukeshot   User is offline

  • A little too OCD
  • member icon

Reputation: 418
  • View blog
  • Posts: 1,030
  • Joined: 14-November 12

Re: Help me find the functional interface

Posted 20 May 2014 - 08:01 AM

This is a wild guess, but based on the correct use of .addActionListener is ( e -> /*insert method or code body here*/ );

I would suggest the correct use of your example would be evaluator.setQuitAction( action -> isLooping = false );
Was This Post Helpful? 1
  • +
  • -

#3 cfoley   User is offline

  • Cabbage
  • member icon

Reputation: 2425
  • View blog
  • Posts: 5,068
  • Joined: 11-December 07

Re: Help me find the functional interface

Posted 20 May 2014 - 08:14 AM

Thanks for the thought but that would introduce an error. The e -> is short for (ActionEvent e) ->. My interface has no parameter so () -> is correct.

My example works. I'm not asking for help debugging. I just want to know if Java has an interface to replace my ActionInterface. If I thought Runnable had the correct semantics I would refactor to:

// Commenting as a metphore for deletion.
// public interface ActionInterface {
// 	void call();
// }
	public void setQuitAction(Runnable action) {
		quitAction = action;
	}

// and somewhere else:

evaluator.setQuitAction(() -> isLooping = false);

This post has been edited by cfoley: 20 May 2014 - 08:14 AM

Was This Post Helpful? 0
  • +
  • -

#4 Ryano121   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1461
  • View blog
  • Posts: 3,289
  • Joined: 30-January 11

Re: Help me find the functional interface

Posted 20 May 2014 - 08:34 AM

Interesting. Seems the java.util.functions package doesn't include a generic functional interface that returns void and takes no parameters. I guess they deemed it to be a rare use case - not sure why though. Linq in .Net put in Action - which is essentially what you have done yourself.

I agree though that Runnable doesn't really fit that well. I don't really see an alternative though other than rolling your own.
Was This Post Helpful? 1
  • +
  • -

#5 Ryano121   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1461
  • View blog
  • Posts: 3,289
  • Joined: 30-January 11

Re: Help me find the functional interface

Posted 20 May 2014 - 08:50 AM

After a bit more digging it seems like all the examples I've found are using Runnable in this use case e.g.

() -> { System.out.println("Hello World"); }


etc etc.

I think they are pushing to use Runnable in this case. My guess is they didn't want to have two identical functional interfaces.
Was This Post Helpful? 2
  • +
  • -

#6 cfoley   User is offline

  • Cabbage
  • member icon

Reputation: 2425
  • View blog
  • Posts: 5,068
  • Joined: 11-December 07

Re: Help me find the functional interface

Posted 20 May 2014 - 09:12 AM

Thanks for digging around. I hoped that wasn't the case.

While reading your post, I remembered that the Javadocs have "uses of" pages. Check it out:
http://docs.oracle.c...e/Runnable.html

Every one of them is to do with threading except maybe one. It's in the BaseStream class. Now, I don't really know what close handlers are for but I wonder if it's to do with cleaning up parallel streams.
Was This Post Helpful? 1
  • +
  • -

#7 CasiOo   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1578
  • View blog
  • Posts: 3,551
  • Joined: 05-April 11

Re: Help me find the functional interface

Posted 20 May 2014 - 09:53 AM

The Runnable interface is in the java.lang package and is not found in java.util.concurrent
But yeah it makes you think about threading, especially when you read the java docs for the class
Though I see no reason why not to use it if it fits your needs
Edit: ah I see that many of the threading classes are in the lang package :D I thought they were located in concurrent actually

If you don't want to use Runnable, and not implement your own interface, then you could use Consumer
Still a bit messy. I would still prefer Runnable, and after that your ActionInterface :)
	public Main() {
		setQuitAction((v) -> System.out.println("Test" + v)); 
	}
	
	public void setQuitAction(Consumer<Void> func) {
		func.accept(null);
	}


This post has been edited by CasiOo: 20 May 2014 - 09:56 AM

Was This Post Helpful? 0
  • +
  • -

#8 cfoley   User is offline

  • Cabbage
  • member icon

Reputation: 2425
  • View blog
  • Posts: 5,068
  • Joined: 11-December 07

Re: Help me find the functional interface

Posted 20 May 2014 - 10:13 AM

Yeah, threads are one of the core concepts in Java. Whatever you think about the threading model, easy threading within the process was a design goal for Java. Threads are as indispensable as String or System.

java.util.concurrent is for abstractions over threading to make our lives easier.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1