blog210904. Eclipse之EclipseContext的lazy compute, 延迟计算
本文以eclipse 4.20为参考.
Lazy compute
EclipseContext实现了IEclipseContext接口. 其主要的作用是将application与container隔离开来.
其基本使用方法是container将值set写入EclipseContext, application读取get使用, 或反之. 也就是大致等同于一个map.
EclipseContext中一个明显的增强是lazy compute.
即允许set写入EclipseContext的值是一个compute(实则是一个function), 而当get读取时, 再执行这个compute, 取其结果.这样的实际效果就是lazy compute.
IContextFunction
描述上述compute的是IContextFunction
public interface IContextFunction {
//..........
Object compute(IEclipseContext context, String contextKey);
//..........
}
典型的用法,
public class WorkbenchPlugin extends AbstractUIPlugin {
//... ...
public void initializeContext(IEclipseContext context) {
e4Context = context;
//... ...
context.set(IEditorRegistry.class.getName(), new ContextFunction() {
@Override
public Object compute(IEclipseContext context, String contextKey) {
if (editorRegistry == null) {
editorRegistry = new EditorRegistry(Platform.getContentTypeManager());
}
return editorRegistry;
}
});
//... ...
}
//... ...
}
Lazy compute的实现
参与lazy compute的类包括
- EclipseContext
- ValueComputation
EclipseContext.set()时, 直接保存IContextFunction instance.
当EclipseContext.get()时, 封装该instance到ValueComputation中, 并运行IContextFunction.compute()计算结果, 返回.
至此完成整个lazy compute过程.
驽马一架 2021/9/4