Java 8 Lambda 표현식-중첩 클래스의 여러 메서드는 어떻습니까?
새로운 기능에 대한 내용은 http://www.javaworld.com/article/2078836/java-se/love-and-hate-for-java-8.html 에서 읽고 있습니다 .
아래 예를 보았습니다.
익명 클래스 사용 :
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
System.out.println("Action Detected");
}
});
Lambda 사용 :
button.addActionListener(e -> {
System.out.println("Action Detected");
});
MouseListener
익명 클래스 내에서 여러 메서드를 구현하려는 경우 누군가를 사용하여 무엇을 할 수 있습니까? 예 :
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);
}
... 등등?
JLS 9.8 부터
기능 인터페이스는 하나의 추상 메서드 만있는 인터페이스이므로 단일 함수 계약을 나타냅니다.
Lambda에는 이러한 기능 인터페이스가 필요하므로 단일 메서드로 제한됩니다. 다중 메서드 인터페이스를 구현하려면 익명 인터페이스를 사용해야합니다.
addMouseListener(new MouseAdapter() {
@Override
public void mouseReleased(MouseEvent e) {
...
}
@Override
public void mousePressed(MouseEvent e) {
...
}
});
도우미 인터페이스를 사용하여 람다와 함께 다중 메서드 인터페이스를 사용할 수 있습니다. 이것은 원치 않는 메소드의 구현이 사소한 리스너 인터페이스와 함께 작동합니다 (즉, 제공하는 것도 할 수 MouseAdapter
있습니다).
// 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) {}
}
이러한 도우미 인터페이스는 한 번만 정의하면됩니다.
이제 다음 Component
c
과 같이 클릭 이벤트에 대한 리스너를 추가 할 수 있습니다 .
c.addMouseListener((ClickedListener)(e)->System.out.println("Clicked !"));
Lambda EG는이 문제를 고려했습니다. 많은 라이브러리는 기능 인터페이스가 기능 인터페이스가되기 몇 년 전에 설계되었지만 기능 인터페이스를 사용합니다. 그러나 클래스에 여러 추상 메서드가 있고 그중 하나만 람다로 대상으로 지정하려는 경우가 있습니다.
여기서 공식적으로 권장되는 패턴은 팩토리 메서드를 정의하는 것입니다.
static MouseListener clickHandler(Consumer<MouseEvent> c) { return ... }
이는 API 자체에 의해 직접 수행 될 수 있으며 (내부 정적 메서드 일 MouseListener
수 있음) 관리자가 이러한 편의를 제공하지 않기로 선택한 경우 다른 라이브러리의 외부 도우미 메서드 일 수 있습니다. 이것이 필요한 상황이 적고 해결 방법이 매우 간단하기 때문에이 문제를 해결하기 위해 언어를 더 확장하는 것은 매력적이지 않은 것 같습니다.
유사한 트릭이 사용되었습니다 ThreadLocal
. 새로운 정적 팩토리 메소드를 참조하십시오 withInitial(Supplier<S>)
.
(그런데,이 문제가 발생하면 예제는 거의 항상 MouseListener
이며 이는 람다 친화적이고 싶지만 그렇지 않은 클래스 집합이 실제로 매우 작다는 것을 암시하므로 고무적입니다.)
Java ActionListener
는 하나의 단일 메소드 ( actionPerformed(ActionEvent e)
) 만 구현해야합니다 . 이것은 Java 8 함수 에 잘 맞으 므로 Java 8은 ActionListener
.
는 MouseAdapter
A와 맞지 않는 그래서 적어도 두 가지 방법이 필요합니다 function
.
원래 리스너 인터페이스에 둘 이상의 메서드가 포함되어 있어도 (단일 라인) 람다 함수 리스너를 작성할 수있는 어댑터 클래스를 만들었습니다.
두 개의 이벤트를 하나로 축소하는 InsertOrRemoveUpdate 어댑터에 유의하십시오.
내 원래 클래스에는 WindowFocusListener, TableModelListener, TableColumnModelListener 및 TreeModelListener에 대한 구현도 포함되어 있었지만 코드가 30000 자의 SO 제한을 초과했습니다.
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
}
기본 메서드는 람다 식 내에서 액세스 할 수 없습니다. 다음 코드는 컴파일되지 않습니다.
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
'program story' 카테고리의 다른 글
javascript는 html 입력에서 "disabled"속성을 제거합니다. (0) | 2020.10.09 |
---|---|
'vagrant up'에 매개 변수를 전달하고 Vagrantfile 범위에 포함하는 방법은 무엇입니까? (0) | 2020.10.09 |
Docker 실행 및 생성 (0) | 2020.10.09 |
신속한 3.0 데이터를 문자열로? (0) | 2020.10.09 |
교차 엔트로피 란 무엇입니까? (0) | 2020.10.09 |