doum
6 天以前 2b287056e2f59518888d05a1bbc7e5a55fbd84d5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package com.doumee.lib_coremodel.binding.command;
 
 
import io.reactivex.functions.Function;
 
/**
 * About : kelin的ResponseCommand
 * 执行的命令事件转换
 */
public class ResponseCommand<T, R> {
 
    private BindingFunction<R> execute;
    private Function<T, R> function;
    private BindingFunction<Boolean> canExecute;
 
    /**
     * like {@link BindingCommand},but ResponseCommand can return result when command has executed!
     *
     * @param execute function to execute when event occur.
     */
    public ResponseCommand(BindingFunction<R> execute) {
        this.execute = execute;
    }
 
 
    public ResponseCommand(Function<T, R> execute) {
        this.function = execute;
    }
 
 
    public ResponseCommand(BindingFunction<R> execute, BindingFunction<Boolean> canExecute) {
        this.execute = execute;
        this.canExecute = canExecute;
    }
 
 
    public ResponseCommand(Function<T, R> execute, BindingFunction<Boolean> 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;
    }
}