94

I'm reading about the new features at: http://www.javaworld.com/article/2078836/java-se/love-and-hate-for-java-8.html

I saw the example below:

Using Anonymous Class:

button.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent ae) {
        System.out.println("Action Detected");
    }
});

With Lambda:

button.addActionListener(e -> {
    System.out.println("Action Detected");
});

What would someone do with a MouseListener if they wanted to implement multiple methods within the anonymous class, e.g.:

public void mousePressed(MouseEvent e) {
    saySomething("Mouse pressed; # of clicks: "
               + e.getClickCount(), e);
}

public void mouseReleased(MouseEvent e) {
    saySomething("Mouse released; # of clicks: "
               + e.getClickCount(), e);
}

... and so on?

MC Emperor
  • 22,334
  • 15
  • 80
  • 130
Menelaos
  • 23,508
  • 18
  • 90
  • 155
  • 24
    Then you have to use anonymous classes only. Introduction of lambda doesn't make them deprecated. Lambdas can be used only with functional interfaces. – Rohit Jain Feb 17 '14 at 15:54
  • 1
    The usage of lambdas as an ActionListener method is very weird. How do you know, that you're implementing method `actionPerformed` of interface `ActionListener`?! – bobbel Feb 17 '14 at 15:59
  • 3
    @bobbel That is the whole concept of Functional Interfaces, I suggest you to read up on them before commenting on them. – skiwi Feb 17 '14 at 16:01
  • I know, how it will work! The point is, that this is not very readable code, is it? Just wanted to mention... – bobbel Feb 17 '14 at 16:04
  • 4
    @maythesource.com You can create two additional interfaces like `MousePressedListener` and `MouseReleasedListener` and two methods `addMousePressedListener(Button, MousePressedListener)` and `addMouseReleasedListener(Button, MouseReleasedListener)`. Then you can use lambdas to implement these event handlers. – ZhekaKozlov Feb 17 '14 at 16:06
  • 1
    @maythesource.com Or even better: you just need to implement two methods `addMousePressedListener(Button, Consumer)` and `addMouseReleasedListener(Button, Consumer)` without having to provide unnecessary interfaces. – ZhekaKozlov Feb 17 '14 at 16:15
  • Take a look at mixins too! – user3535778 May 29 '14 at 15:17

6 Answers6

96

From JLS 9.8

A functional interface is an interface that has just one abstract method, and thus represents a single function contract.

Lambdas require these functional interfaces so are restricted to their single method. Anonymous interfaces still need to be used for implementing multi-method interfaces.

addMouseListener(new MouseAdapter() {

    @Override
    public void mouseReleased(MouseEvent e) {
       ...
    }

    @Override
    public void mousePressed(MouseEvent e) {
      ...
    }
});
Reimeus
  • 158,255
  • 15
  • 216
  • 276
  • 13
    The question from the OP was "**What would someone do** with a MouseListener if they wanted to implement multiple methods within the anonymous class, e.g.:" (emphasis mine). The answer you gave was good, but simply says you cannot use interfaces containing more than one method for lambda description. [Holger's answer](http://stackoverflow.com/a/21851111/4229245) explains a workaround for this. You extend to your own interface, and provide defaults for the methods you don't want. This leaves one left for your lambda. **It wouldn't be the first cringeworthy thing people see in Java 8 code.** –  May 18 '15 at 13:23
85

You can use multi-method interfaces with lambdas by using helper interfaces. This works with such listener interfaces where the implementations of unwanted methods are trivial (i.e. we can just do what MouseAdapter offers too):

// note the absence of mouseClicked…
interface ClickedListener extends MouseListener
{
    @Override
    public default void mouseEntered(MouseEvent e) {}

    @Override
    public default void mouseExited(MouseEvent e) {}

    @Override
    public default void mousePressed(MouseEvent e) {}

    @Override
    public default void mouseReleased(MouseEvent e) {}
}

You need to define such a helper interface only once.

Now you can add a listener for click-events on a Component c like this:

c.addMouseListener((ClickedListener)(e)->System.out.println("Clicked !"));
Holger
  • 285,553
  • 42
  • 434
  • 765
  • 11
    This seems to me to be a rococco solution to this problem for MouseListeners. I think that attaching an anonymous object [the old way] works better. It seems to me to be a kludge. – ncmathsadist Dec 19 '14 at 15:03
  • 2
    @ncmathsadist: feel free to use the anonymous inner class approach if you like. This is only an option. You may also consider [this answer](http://stackoverflow.com/a/23373921/2711488). There’s also [the other dynamic listener generator](http://docs.oracle.com/javase/7/docs/api/java/beans/EventHandler.html#create(java.lang.Class,%20java.lang.Object,%20java.lang.String,%20java.lang.String,%20java.lang.String))… – Holger Dec 19 '14 at 15:10
  • 12
    I believe that this "kludge" should be the accepted answer. The question from the OP was clearly "**What would someone do** with a MouseListener if they wanted to implement multiple methods within the anonymous class, e.g.:" (emphasis mine). He's asking how to use lamdas when you have an interface with more than one method in it. @Holger answered this perfectly. The OP obviously *already* knows how to use an anonymous class in non-functional ways. –  May 18 '15 at 13:18
  • 1
    @Holger Here out of four which method will be called when we use lambda? As all of these methods have same parameter. – dreamcoder Apr 12 '18 at 02:39
  • 1
    @dreamcoder none of these four methods will be implemented by the lambda, as none of them is abstract. The lambda will implement the `mouseClicked` method which has been declared as an abstract method in the `MouseListener` interface and not overridden by the `ClickedListener` interface (hence the name). – Holger Apr 12 '18 at 05:59
35

The Lambda EG did consider this issue. Many libraries use functional interfaces, even though they were designed years before functional interface became a thing. But it does happen sometimes that a class has multiple abstract methods, and you only want to target one of them with a lambda.

The officially recommended pattern here is to define factory methods:

static MouseListener clickHandler(Consumer<MouseEvent> c) { return ... }

These can be done directly by the APIs themselves (these could be static methods inside of MouseListener) or could be external helper methods in some other library should the maintainers choose not to offer this convenience. Because the set of situations where this was needed is small, and the workaround is so simple, it did not seem compelling to extend the language further to rescue this corner case.

A similar trick was employed for ThreadLocal; see the new static factory method withInitial(Supplier<S>).

(By the way, when this issue comes up, the example is almost always MouseListener, which is encouraging as it suggests the set of classes which would like to be lambda friendly, but aren't, is actually pretty small.)

Thirumalai Parthasarathi
  • 4,541
  • 1
  • 25
  • 43
Brian Goetz
  • 90,105
  • 23
  • 150
  • 161
  • 3
    See also [this question](http://stackoverflow.com/q/25299653/1441122). There are a handful of other multi-method interfaces, such as `WindowListener`, but there relatively few of them. – Stuart Marks Aug 15 '14 at 23:37
4

A Java ActionListener must implement just one single method (actionPerformed(ActionEvent e)). This fits nicely into the Java 8 function so Java 8 provides a simple lambda to implement an ActionListener.

The MouseAdapter requires at least two methods so does not fit as a function.

OldCurmudgeon
  • 64,482
  • 16
  • 119
  • 213
2

I created adapter classes that allow me to write (single-line) lambda-function listeners, even if the original listener interface contains more than one method.

Note the InsertOrRemoveUpdate adapter, which collapses 2 events into one.

My original class also contained implementations for WindowFocusListener, TableModelListener, TableColumnModelListener and TreeModelListener, but that pushed the code over the SO limit of 30000 characters.

import java.awt.event.*;
import java.beans.*;
import javax.swing.*;
import javax.swing.event.*;
import org.slf4j.*;

public class Adapters {
  private static final Logger LOGGER = LoggerFactory.getLogger(Adapters.class);

  @FunctionalInterface
  public static interface AdapterEventHandler<EVENT> {
    void handle(EVENT e) throws Exception;

    default void handleWithRuntimeException(EVENT e) {
      try {
        handle(e);
      }
      catch(Exception exception) {
        throw exception instanceof RuntimeException ? (RuntimeException) exception : new RuntimeException(exception);
      }
    }
  }

  public static void main(String[] args) throws Exception {
    // -------------------------------------------------------------------------------------------------------------------------------------
    // demo usage
    // -------------------------------------------------------------------------------------------------------------------------------------
    JToggleButton toggleButton = new JToggleButton();
    JFrame frame = new JFrame();
    JTextField textField = new JTextField();
    JScrollBar scrollBar = new JScrollBar();
    ListSelectionModel listSelectionModel = new DefaultListSelectionModel();

    // ActionListener
    toggleButton.addActionListener(ActionPerformed.handle(e -> LOGGER.info(e.toString())));

    // ItemListener
    toggleButton.addItemListener(ItemStateChanged.handle(e -> LOGGER.info(e.toString())));

    // ChangeListener
    toggleButton.addChangeListener(StateChanged.handle(e -> LOGGER.info(e.toString())));

    // MouseListener
    frame.addMouseListener(MouseClicked.handle(e -> LOGGER.info(e.toString())));
    frame.addMouseListener(MousePressed.handle(e -> LOGGER.info(e.toString())));
    frame.addMouseListener(MouseReleased.handle(e -> LOGGER.info(e.toString())));
    frame.addMouseListener(MouseEntered.handle(e -> LOGGER.info(e.toString())));
    frame.addMouseListener(MouseExited.handle(e -> LOGGER.info(e.toString())));

    // MouseMotionListener
    frame.addMouseMotionListener(MouseDragged.handle(e -> LOGGER.info(e.toString())));
    frame.addMouseMotionListener(MouseMoved.handle(e -> LOGGER.info(e.toString())));

    // MouseWheelListenere
    frame.addMouseWheelListener(MouseWheelMoved.handle(e -> LOGGER.info(e.toString())));

    // KeyListener
    frame.addKeyListener(KeyTyped.handle(e -> LOGGER.info(e.toString())));
    frame.addKeyListener(KeyPressed.handle(e -> LOGGER.info(e.toString())));
    frame.addKeyListener(KeyReleased.handle(e -> LOGGER.info(e.toString())));

    // FocusListener
    frame.addFocusListener(FocusGained.handle(e -> LOGGER.info(e.toString())));
    frame.addFocusListener(FocusLost.handle(e -> LOGGER.info(e.toString())));

    // ComponentListener
    frame.addComponentListener(ComponentMoved.handle(e -> LOGGER.info(e.toString())));
    frame.addComponentListener(ComponentResized.handle(e -> LOGGER.info(e.toString())));
    frame.addComponentListener(ComponentShown.handle(e -> LOGGER.info(e.toString())));
    frame.addComponentListener(ComponentHidden.handle(e -> LOGGER.info(e.toString())));

    // ContainerListener
    frame.addContainerListener(ComponentAdded.handle(e -> LOGGER.info(e.toString())));
    frame.addContainerListener(ComponentRemoved.handle(e -> LOGGER.info(e.toString())));

    // HierarchyListener
    frame.addHierarchyListener(HierarchyChanged.handle(e -> LOGGER.info(e.toString())));

    // PropertyChangeListener
    frame.addPropertyChangeListener(PropertyChange.handle(e -> LOGGER.info(e.toString())));
    frame.addPropertyChangeListener("propertyName", PropertyChange.handle(e -> LOGGER.info(e.toString())));

    // WindowListener
    frame.addWindowListener(WindowOpened.handle(e -> LOGGER.info(e.toString())));
    frame.addWindowListener(WindowClosing.handle(e -> LOGGER.info(e.toString())));
    frame.addWindowListener(WindowClosed.handle(e -> LOGGER.info(e.toString())));
    frame.addWindowListener(WindowIconified.handle(e -> LOGGER.info(e.toString())));
    frame.addWindowListener(WindowDeiconified.handle(e -> LOGGER.info(e.toString())));
    frame.addWindowListener(WindowActivated.handle(e -> LOGGER.info(e.toString())));
    frame.addWindowListener(WindowDeactivated.handle(e -> LOGGER.info(e.toString())));

    // WindowStateListener
    frame.addWindowStateListener(WindowStateChanged.handle(e -> LOGGER.info(e.toString())));

    // DocumentListener
    textField.getDocument().addDocumentListener(InsertUpdate.handle(e -> LOGGER.info(e.toString())));
    textField.getDocument().addDocumentListener(RemoveUpdate.handle(e -> LOGGER.info(e.toString())));
    textField.getDocument().addDocumentListener(InsertOrRemoveUpdate.handle(e -> LOGGER.info(e.toString())));
    textField.getDocument().addDocumentListener(ChangedUpdate.handle(e -> LOGGER.info(e.toString())));

    // AdjustmentListener
    scrollBar.addAdjustmentListener(AdjustmentValueChanged.handle(e -> LOGGER.info(e.toString())));

    // ListSelectionListener
    listSelectionModel.addListSelectionListener(ValueChanged.handle(e -> LOGGER.info(e.toString())));

    // ...
    // enhance as needed
  }

  // @formatter:off
  // ---------------------------------------------------------------------------------------------------------------------------------------
  // ActionListener
  // ---------------------------------------------------------------------------------------------------------------------------------------
  public static class ActionPerformed implements ActionListener {
    private AdapterEventHandler<ActionEvent> m_handler = null;
    public static ActionPerformed handle(AdapterEventHandler<ActionEvent> handler) { ActionPerformed adapter = new ActionPerformed(); adapter.m_handler = handler; return adapter; }
    @Override public void actionPerformed(ActionEvent e) { m_handler.handleWithRuntimeException(e); }
  }

  // ---------------------------------------------------------------------------------------------------------------------------------------
  // ItemListener
  // ---------------------------------------------------------------------------------------------------------------------------------------
  public static class ItemStateChanged implements ItemListener {
    private AdapterEventHandler<ItemEvent> m_handler = null;
    public static ItemStateChanged handle(AdapterEventHandler<ItemEvent> handler) { ItemStateChanged adapter = new ItemStateChanged(); adapter.m_handler = handler; return adapter; }
    @Override public void itemStateChanged(ItemEvent e) { m_handler.handleWithRuntimeException(e); }
  }

  // ---------------------------------------------------------------------------------------------------------------------------------------
  // ChangeListener
  // ---------------------------------------------------------------------------------------------------------------------------------------
  public static class StateChanged implements ChangeListener {
    private AdapterEventHandler<ChangeEvent> m_handler = null;
    public static StateChanged handle(AdapterEventHandler<ChangeEvent> handler) { StateChanged adapter = new StateChanged(); adapter.m_handler = handler; return adapter; }
    @Override public void stateChanged(ChangeEvent e) { m_handler.handleWithRuntimeException(e); }
  }

  // ---------------------------------------------------------------------------------------------------------------------------------------
  // MouseListener
  // ---------------------------------------------------------------------------------------------------------------------------------------
  public static class MouseClicked extends MouseAdapter {
    private AdapterEventHandler<MouseEvent> m_handler = null;
    public static MouseClicked handle(AdapterEventHandler<MouseEvent> handler) { MouseClicked adapter = new MouseClicked(); adapter.m_handler = handler; return adapter; }
    @Override public void mouseClicked(MouseEvent e) { m_handler.handleWithRuntimeException(e); }
  }

  public static class MousePressed extends MouseAdapter {
    private AdapterEventHandler<MouseEvent> m_handler = null;
    public static MousePressed handle(AdapterEventHandler<MouseEvent> handler) { MousePressed adapter = new MousePressed(); adapter.m_handler = handler; return adapter; }
    @Override public void mousePressed(MouseEvent e) { m_handler.handleWithRuntimeException(e); }
  }

  public static class MouseReleased extends MouseAdapter {
    private AdapterEventHandler<MouseEvent> m_handler = null;
    public static MouseReleased handle(AdapterEventHandler<MouseEvent> handler) { MouseReleased adapter = new MouseReleased(); adapter.m_handler = handler; return adapter; }
    @Override public void mouseReleased(MouseEvent e) { m_handler.handleWithRuntimeException(e); }
  }

  public static class MouseEntered extends MouseAdapter {
    private AdapterEventHandler<MouseEvent> m_handler = null;
    public static MouseEntered handle(AdapterEventHandler<MouseEvent> handler) { MouseEntered adapter = new MouseEntered(); adapter.m_handler = handler; return adapter; }
    @Override public void mouseEntered(MouseEvent e) { m_handler.handleWithRuntimeException(e); }
  }

  public static class MouseExited extends MouseAdapter {
    private AdapterEventHandler<MouseEvent> m_handler = null;
    public static MouseExited handle(AdapterEventHandler<MouseEvent> handler) { MouseExited adapter = new MouseExited(); adapter.m_handler = handler; return adapter; }
    @Override public void mouseExited(MouseEvent e) { m_handler.handleWithRuntimeException(e); }
  }

  // ---------------------------------------------------------------------------------------------------------------------------------------
  // MouseMotionListener
  // ---------------------------------------------------------------------------------------------------------------------------------------
  public static class MouseDragged extends MouseAdapter {
    private AdapterEventHandler<MouseEvent> m_handler = null;
    public static MouseDragged handle(AdapterEventHandler<MouseEvent> handler) { MouseDragged adapter = new MouseDragged(); adapter.m_handler = handler; return adapter; }
    @Override public void mouseDragged(MouseEvent e) { m_handler.handleWithRuntimeException(e); }
  }

  public static class MouseMoved extends MouseAdapter {
    private AdapterEventHandler<MouseEvent> m_handler = null;
    public static MouseMoved handle(AdapterEventHandler<MouseEvent> handler) { MouseMoved adapter = new MouseMoved(); adapter.m_handler = handler; return adapter; }
    @Override public void mouseMoved(MouseEvent e) { m_handler.handleWithRuntimeException(e); }
  }

  // ---------------------------------------------------------------------------------------------------------------------------------------
  // MouseWheelListener
  // ---------------------------------------------------------------------------------------------------------------------------------------
  public static class MouseWheelMoved extends MouseAdapter {
    private AdapterEventHandler<MouseWheelEvent> m_handler = null;
    public static MouseWheelMoved handle(AdapterEventHandler<MouseWheelEvent> handler) { MouseWheelMoved adapter = new MouseWheelMoved(); adapter.m_handler = handler; return adapter; }
    @Override public void mouseWheelMoved(MouseWheelEvent e) { m_handler.handleWithRuntimeException(e); }
  }

  // ---------------------------------------------------------------------------------------------------------------------------------------
  // KeyListener
  // ---------------------------------------------------------------------------------------------------------------------------------------
  public static class KeyTyped extends KeyAdapter {
    private AdapterEventHandler<KeyEvent> m_handler = null;
    public static KeyTyped handle(AdapterEventHandler<KeyEvent> handler) { KeyTyped adapter = new KeyTyped(); adapter.m_handler = handler; return adapter; }
    @Override public void keyTyped(KeyEvent e) { m_handler.handleWithRuntimeException(e); }
  }

  public static class KeyPressed extends KeyAdapter {
    private AdapterEventHandler<KeyEvent> m_handler = null;
    public static KeyPressed handle(AdapterEventHandler<KeyEvent> handler) { KeyPressed adapter = new KeyPressed(); adapter.m_handler = handler; return adapter; }
    @Override public void keyPressed(KeyEvent e) { m_handler.handleWithRuntimeException(e); }
  }

  public static class KeyReleased extends KeyAdapter {
    private AdapterEventHandler<KeyEvent> m_handler = null;
    public static KeyReleased handle(AdapterEventHandler<KeyEvent> handler) { KeyReleased adapter = new KeyReleased(); adapter.m_handler = handler; return adapter; }
    @Override public void keyReleased(KeyEvent e) { m_handler.handleWithRuntimeException(e); }
  }

  // ---------------------------------------------------------------------------------------------------------------------------------------
  // FocusListener
  // ---------------------------------------------------------------------------------------------------------------------------------------
  public static class FocusGained extends FocusAdapter {
    private AdapterEventHandler<FocusEvent> m_handler = null;
    public static FocusGained handle(AdapterEventHandler<FocusEvent> handler) { FocusGained adapter = new FocusGained(); adapter.m_handler = handler; return adapter; }
    @Override public void focusGained(FocusEvent e) { m_handler.handleWithRuntimeException(e); }
  }

  public static class FocusLost extends FocusAdapter {
    private AdapterEventHandler<FocusEvent> m_handler = null;
    public static FocusLost handle(AdapterEventHandler<FocusEvent> handler) { FocusLost adapter = new FocusLost(); adapter.m_handler = handler; return adapter; }
    @Override public void focusLost(FocusEvent e) { m_handler.handleWithRuntimeException(e); }
  }

  // ---------------------------------------------------------------------------------------------------------------------------------------
  // ComponentListener
  // ---------------------------------------------------------------------------------------------------------------------------------------
  public static class ComponentMoved extends ComponentAdapter {
    private AdapterEventHandler<ComponentEvent> m_handler = null;
    public static ComponentMoved handle(AdapterEventHandler<ComponentEvent> handler) { ComponentMoved adapter = new ComponentMoved(); adapter.m_handler = handler; return adapter; }
    @Override public void componentMoved(ComponentEvent e) { m_handler.handleWithRuntimeException(e); }
  }

  public static class ComponentResized extends ComponentAdapter {
    private AdapterEventHandler<ComponentEvent> m_handler = null;
    public static ComponentResized handle(AdapterEventHandler<ComponentEvent> handler) { ComponentResized adapter = new ComponentResized(); adapter.m_handler = handler; return adapter; }
    @Override public void componentResized(ComponentEvent e) { m_handler.handleWithRuntimeException(e); }
  }

  public static class ComponentShown extends ComponentAdapter {
    private AdapterEventHandler<ComponentEvent> m_handler = null;
    public static ComponentShown handle(AdapterEventHandler<ComponentEvent> handler) { ComponentShown adapter = new ComponentShown(); adapter.m_handler = handler; return adapter; }
    @Override public void componentShown(ComponentEvent e) { m_handler.handleWithRuntimeException(e); }
  }

  public static class ComponentHidden extends ComponentAdapter {
    private AdapterEventHandler<ComponentEvent> m_handler = null;
    public static ComponentHidden handle(AdapterEventHandler<ComponentEvent> handler) { ComponentHidden adapter = new ComponentHidden(); adapter.m_handler = handler; return adapter; }
    @Override public void componentHidden(ComponentEvent e) { m_handler.handleWithRuntimeException(e); }
  }

  // ---------------------------------------------------------------------------------------------------------------------------------------
  // ContainerListener
  // ---------------------------------------------------------------------------------------------------------------------------------------
  public static class ComponentAdded extends ContainerAdapter {
    private AdapterEventHandler<ContainerEvent> m_handler = null;
    public static ComponentAdded handle(AdapterEventHandler<ContainerEvent> handler) { ComponentAdded adapter = new ComponentAdded(); adapter.m_handler = handler; return adapter; }
    @Override public void componentAdded(ContainerEvent e) { m_handler.handleWithRuntimeException(e); }
  }

  public static class ComponentRemoved extends ContainerAdapter {
    private AdapterEventHandler<ContainerEvent> m_handler = null;
    public static ComponentRemoved handle(AdapterEventHandler<ContainerEvent> handler) { ComponentRemoved adapter = new ComponentRemoved(); adapter.m_handler = handler; return adapter; }
    @Override public void componentRemoved(ContainerEvent e) { m_handler.handleWithRuntimeException(e); }
  }

  // ---------------------------------------------------------------------------------------------------------------------------------------
  // HierarchyListener
  // ---------------------------------------------------------------------------------------------------------------------------------------
  public static class HierarchyChanged implements HierarchyListener {
    private AdapterEventHandler<HierarchyEvent> m_handler = null;
    public static HierarchyChanged handle(AdapterEventHandler<HierarchyEvent> handler) { HierarchyChanged adapter = new HierarchyChanged(); adapter.m_handler = handler; return adapter; }
    @Override public void hierarchyChanged(HierarchyEvent e) { m_handler.handleWithRuntimeException(e); }
  }

  // ---------------------------------------------------------------------------------------------------------------------------------------
  // PropertyChangeListener
  // ---------------------------------------------------------------------------------------------------------------------------------------
  public static class PropertyChange implements PropertyChangeListener {
    private AdapterEventHandler<PropertyChangeEvent> m_handler = null;
    public static PropertyChange handle(AdapterEventHandler<PropertyChangeEvent> handler) { PropertyChange adapter = new PropertyChange(); adapter.m_handler = handler; return adapter; }
    @Override public void propertyChange(PropertyChangeEvent e) { m_handler.handleWithRuntimeException(e); }
  }

  // ---------------------------------------------------------------------------------------------------------------------------------------
  // WindowListener
  // ---------------------------------------------------------------------------------------------------------------------------------------
  public static class WindowOpened extends WindowAdapter {
    private AdapterEventHandler<WindowEvent> m_handler = null;
    public static WindowOpened handle(AdapterEventHandler<WindowEvent> handler) { WindowOpened adapter = new WindowOpened(); adapter.m_handler = handler; return adapter; }
    @Override public void windowOpened(WindowEvent e) { m_handler.handleWithRuntimeException(e); }
  }

  public static class WindowClosing extends WindowAdapter {
    private AdapterEventHandler<WindowEvent> m_handler = null;
    public static WindowClosing handle(AdapterEventHandler<WindowEvent> handler) { WindowClosing adapter = new WindowClosing(); adapter.m_handler = handler; return adapter; }
    @Override public void windowClosing(WindowEvent e) { m_handler.handleWithRuntimeException(e); }
  }

  public static class WindowClosed extends WindowAdapter {
    private AdapterEventHandler<WindowEvent> m_handler = null;
    public static WindowClosed handle(AdapterEventHandler<WindowEvent> handler) { WindowClosed adapter = new WindowClosed(); adapter.m_handler = handler; return adapter; }
    @Override public void windowClosed(WindowEvent e) { m_handler.handleWithRuntimeException(e); }
  }

  public static class WindowIconified extends WindowAdapter {
    private AdapterEventHandler<WindowEvent> m_handler = null;
    public static WindowIconified handle(AdapterEventHandler<WindowEvent> handler) { WindowIconified adapter = new WindowIconified(); adapter.m_handler = handler; return adapter; }
    @Override public void windowIconified(WindowEvent e) { m_handler.handleWithRuntimeException(e); }
  }

  public static class WindowDeiconified extends WindowAdapter {
    private AdapterEventHandler<WindowEvent> m_handler = null;
    public static WindowDeiconified handle(AdapterEventHandler<WindowEvent> handler) { WindowDeiconified adapter = new WindowDeiconified(); adapter.m_handler = handler; return adapter; }
    @Override public void windowDeiconified(WindowEvent e) { m_handler.handleWithRuntimeException(e); }
  }

  public static class WindowActivated extends WindowAdapter {
    private AdapterEventHandler<WindowEvent> m_handler = null;
    public static WindowActivated handle(AdapterEventHandler<WindowEvent> handler) { WindowActivated adapter = new WindowActivated(); adapter.m_handler = handler; return adapter; }
    @Override public void windowActivated(WindowEvent e) { m_handler.handleWithRuntimeException(e); }
  }

  public static class WindowDeactivated extends WindowAdapter {
    private AdapterEventHandler<WindowEvent> m_handler = null;
    public static WindowDeactivated handle(AdapterEventHandler<WindowEvent> handler) { WindowDeactivated adapter = new WindowDeactivated(); adapter.m_handler = handler; return adapter; }
    @Override public void windowDeactivated(WindowEvent e) { m_handler.handleWithRuntimeException(e); }
  }

  // ---------------------------------------------------------------------------------------------------------------------------------------
  // WindowStateListener
  // ---------------------------------------------------------------------------------------------------------------------------------------
  public static class WindowStateChanged extends WindowAdapter {
    private AdapterEventHandler<WindowEvent> m_handler = null;
    public static WindowStateChanged handle(AdapterEventHandler<WindowEvent> handler) { WindowStateChanged adapter = new WindowStateChanged(); adapter.m_handler = handler; return adapter; }
    @Override public void windowStateChanged(WindowEvent e) { m_handler.handleWithRuntimeException(e); }
  }

  // ---------------------------------------------------------------------------------------------------------------------------------------
  // DocumentListener
  // ---------------------------------------------------------------------------------------------------------------------------------------
  public static class DocumentAdapter implements DocumentListener {
    @Override public void insertUpdate(DocumentEvent e) { /* nothing */ }
    @Override public void removeUpdate(DocumentEvent e) { /* nothing */ }
    @Override public void changedUpdate(DocumentEvent e) { /* nothing */ }
  }

  public static class InsertUpdate extends DocumentAdapter {
    private AdapterEventHandler<DocumentEvent> m_handler = null;
    public static InsertUpdate handle(AdapterEventHandler<DocumentEvent> handler) { InsertUpdate adapter = new InsertUpdate(); adapter.m_handler = handler; return adapter; }
    @Override public void insertUpdate(DocumentEvent e) { m_handler.handleWithRuntimeException(e); }
  }

  public static class RemoveUpdate extends DocumentAdapter {
    private AdapterEventHandler<DocumentEvent> m_handler = null;
    public static RemoveUpdate handle(AdapterEventHandler<DocumentEvent> handler) { RemoveUpdate adapter = new RemoveUpdate(); adapter.m_handler = handler; return adapter; }
    @Override public void removeUpdate(DocumentEvent e) { m_handler.handleWithRuntimeException(e); }
  }

  public static class InsertOrRemoveUpdate extends DocumentAdapter {
    private AdapterEventHandler<DocumentEvent> m_handler = null;
    public static InsertOrRemoveUpdate handle(AdapterEventHandler<DocumentEvent> handler) { InsertOrRemoveUpdate adapter = new InsertOrRemoveUpdate(); adapter.m_handler = handler; return adapter; }
    @Override public void insertUpdate(DocumentEvent e) { m_handler.handleWithRuntimeException(e); }
    @Override public void removeUpdate(DocumentEvent e) { m_handler.handleWithRuntimeException(e); }
  }

  public static class ChangedUpdate extends DocumentAdapter {
    private AdapterEventHandler<DocumentEvent> m_handler = null;
    public static ChangedUpdate handle(AdapterEventHandler<DocumentEvent> handler) { ChangedUpdate adapter = new ChangedUpdate(); adapter.m_handler = handler; return adapter; }
    @Override public void changedUpdate(DocumentEvent e) { m_handler.handleWithRuntimeException(e); }
  }

  // ---------------------------------------------------------------------------------------------------------------------------------------
  // AdjustmentListener
  // ---------------------------------------------------------------------------------------------------------------------------------------
  public static class AdjustmentValueChanged implements AdjustmentListener {
    private AdapterEventHandler<AdjustmentEvent> m_handler = null;
    public static AdjustmentValueChanged handle(AdapterEventHandler<AdjustmentEvent> handler) { AdjustmentValueChanged adapter = new AdjustmentValueChanged(); adapter.m_handler = handler; return adapter; }
    @Override public void adjustmentValueChanged(AdjustmentEvent e) { m_handler.handleWithRuntimeException(e); }
  }

  // ---------------------------------------------------------------------------------------------------------------------------------------
  // ListSelectionListener
  // ---------------------------------------------------------------------------------------------------------------------------------------
  public static class ValueChanged implements ListSelectionListener {
    private AdapterEventHandler<ListSelectionEvent> m_handler = null;
    public static ValueChanged handle(AdapterEventHandler<ListSelectionEvent> handler) { ValueChanged adapter = new ValueChanged(); adapter.m_handler = handler; return adapter; }
    @Override public void valueChanged(ListSelectionEvent e) { m_handler.handleWithRuntimeException(e); }
  }
  // @formatter:on
}
Reto Höhener
  • 5,419
  • 4
  • 39
  • 79
-3

Default methods cannot be accessed from within lambda expressions. The following code does not compile:

interface Formula {
    double calculate(int a);

    default double sqrt(int a) {
        return Math.sqrt(a);
    }
}
Formula formula = (a) -> sqrt( a * 100);

only work with functional interface(single abstract method only +any number of default methods) so lambda expresion work with only abstract method

Pang
  • 9,564
  • 146
  • 81
  • 122