gpt4 book ai didi

JavaFX:错误记录 ChangeListener

转载 作者:行者123 更新时间:2023-11-30 02:25:52 27 4
gpt4 key购买 nike

我想概括以下模式:

setChangeListener = c -> {
try {
// do something dangerous
} catch (final IOException e) {
logger.error(e.getLocalizedMessage(), e);
}
};

我想这样使用它:

errorLoggingSetChangeListener = c -> {
// do something dangerous
};

我在想这个:

public class ErrorLoggingSetChangeListener<T> implements SetChangeListener<T> {
private static final Logger logger = Logger.getLogger(ErrorLoggingSetChangeListener.class);
private final SetChangeListener<T> delegate;

@Override
public void onChanged(final SetChangeListener.Change<? extends T> change) {
try {
delegate.onChanged(change);
} catch (final Exception e) {
if (logger.isEnabledFor(Level.ERROR)) {
logger.error(e.getLocalizedMessage(), e);
}
}
}

public ErrorLoggingSetChangeListener(final SetChangeListener<T> delegate) {
super();
this.delegate = delegate;
}
}

但这不可能,因为 ErrorLoggingSetChangeListener 不是功能接口(interface)。

有机会将此类转换为函数式接口(interface)吗?

这不会编译:

public interface ErrorLoggingSetChangeListener<T> extends SetChangeListener<T> {

static final Logger logger = Logger.getLogger(ErrorLoggingSetChangeListener.class);

@Override
default void onChanged(final SetChangeListener.Change<? extends T> change) {
try {
SetChangeListener.super.onChanged(change);
} catch (final Exception e) {
if (logger.isEnabledFor(Level.ERROR)) {
logger.error(e.getLocalizedMessage(), e);
}
}
}
}

这也无法编译:

errorLoggingSetChangeListener = new ErrorLoggingSetChangeListener<>(c -> {
throw new IOException();
});

错误信息是

Unhandled exception [..]

.

最佳答案

这与 @JonnyAW 的解决方案类似,但将两个类组合到一个接口(interface)中:

import javafx.collections.SetChangeListener;

@FunctionalInterface
public interface ErrorLoggingSetChangeListener<E> extends SetChangeListener<E> {


public void delegate(Change<? extends E> change) throws Exception ;

@Override
public default void onChanged(Change<? extends E> change) {
try {
delegate(change);
} catch (Exception exc) {
// just do a System.out.println here to demo we reach this block:
System.out.println("Custom error handling...");
exc.printStackTrace();
}
}
}

这是使用此功能的演示:

import javafx.collections.FXCollections;
import javafx.collections.ObservableSet;

public class Test {

public static void main(String[] args) {
ObservableSet<String> set = FXCollections.observableSet();
ErrorLoggingSetChangeListener<String> listener = c -> {
if (c.wasAdded()) {
int i = Integer.parseInt(c.getElementAdded());
System.out.println("Value added: "+i);
}
};
set.addListener(listener);

set.add("42");
set.add("What do you get when you multiply 6 by 9?");
}

}

生成预期的输出:

Value added: 42
Custom error handling...
java.lang.NumberFormatException: For input string: "What do you get when you multiply 6 by 9?"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:580)
at java.lang.Integer.parseInt(Integer.java:615)
at Test.lambda$0(Test.java:10)
at ErrorLoggingSetChangeListener.onChanged(ErrorLoggingSetChangeListener.java:12)
at com.sun.javafx.collections.SetListenerHelper$SingleChange.fireValueChangedEvent(SetListenerHelper.java:163)
at com.sun.javafx.collections.SetListenerHelper.fireValueChangedEvent(SetListenerHelper.java:72)
at com.sun.javafx.collections.ObservableSetWrapper.callObservers(ObservableSetWrapper.java:128)
at com.sun.javafx.collections.ObservableSetWrapper.add(ObservableSetWrapper.java:269)
at Test.main(Test.java:17)

关于JavaFX:错误记录 ChangeListener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45671862/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com