package com.doumee.lib_coremodel.binding.command; import io.reactivex.functions.Function; /** * About : kelin的ResponseCommand * 执行的命令事件转换 */ public class ResponseCommand { private BindingFunction execute; private Function function; private BindingFunction canExecute; /** * like {@link BindingCommand},but ResponseCommand can return result when command has executed! * * @param execute function to execute when event occur. */ public ResponseCommand(BindingFunction execute) { this.execute = execute; } public ResponseCommand(Function execute) { this.function = execute; } public ResponseCommand(BindingFunction execute, BindingFunction canExecute) { this.execute = execute; this.canExecute = canExecute; } public ResponseCommand(Function execute, BindingFunction canExecute) { this.function = execute; this.canExecute = canExecute; } public R execute() { if (execute != null && canExecute()) { return execute.call(); } return null; } private boolean canExecute() { if (canExecute == null) { return true; } return canExecute.call(); } public R execute(T parameter) throws Exception { if (function != null && canExecute()) { return function.apply(parameter); } return null; } }