这里有一个棘手的情况,我想从代码的角度进行优化。有没有办法通过Lambda / Java8表达式缩短以下方法?
// total amount of audiences
registry.register("metric.persons.total", new CachedGauge<Integer>(1,TimeUnit.MINUTES) {
@Override
protected Integer loadValue() {
return personService.findAll().size();
}
});CachedGauge类如下所示:
public abstract class CachedGauge<T> implements Gauge<T> {
protected CachedGauge(long timeout, TimeUnit timeoutUnit) {
...
}
protected abstract T loadValue();
...
}
}如果有一种方法的话,那就太好了,这里棘手的部分是有一个默认的构造函数,类是参数化的。
最佳,弗瑞
发布于 2015-05-29 23:31:41
registry.register("metric.persons.total",
CachedGauge.of(1,TimeUnit.MINUTES, ()->personService.findAll().size() )
);我想你可以想出如何实现CachedGauge.of(long, TimeUnit, Supplier<T>)
发布于 2015-05-29 23:49:08
为了完成这个线程,我的Utils类如下所示
public class MetricUtils {
public static <T> CachedGauge<T> cachedGauge(long timeout, TimeUnit timeoutUnit, Supplier<T> supplier) {
return new CachedGauge<T>(timeout, timeoutUnit) {
@Override
protected T loadValue() {
return supplier.get();
}
};
}
}https://stackoverflow.com/questions/30540508
复制相似问题