summaryrefslogtreecommitdiff
path: root/src/main/java/au/id/zancanaro/javacheck/state/CommandList.java
blob: 6dc897667aa810ffb4aefe45c686d52c5ffefc18 (about) (plain)
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package au.id.zancanaro.javacheck.state;

import java.util.*;

public class CommandList<S> {
    private final List<GeneratedCommand<S, ?, ?>> commands;
    private final S initialState;

    public CommandList(S initialState, List<GeneratedCommand<S, ?, ?>> commands) {
        this.initialState = initialState;
        this.commands = new ArrayList<>(commands);
    }

    public int size() {
        return commands.size();
    }

    public CommandResult<S> run(S initialState) {
        Map<Integer, Object> values = new HashMap<>();
        CommandResult<S> result = CommandResult.success(initialState);
        for (GeneratedCommand<S, ?, ?> generated : commands) {
            result = runRealCommand(generated, result.getState(), values);
            if (result.isFailed()) {
                break;
            }
        }
        return result;
    }

    private static <S, A, R> CommandResult<S> runRealCommand(
            GeneratedCommand<S, A, R> generated,
            S state,
            Map<Integer, Object> values) {
        int id = generated.getId();
        Command<S, A, R> command = generated.getCommand();
        A args = generated.getArgs();
        try {
            if (!command.preCondition(state, args)) {
                return CommandResult.fail(state, new Error("Precondition failed"));
            }
            final S oldState = state;
            R result = CommandValue.withValues(values, () -> command.runCommand(oldState, args));
            values.put(id, result);
            final S newState = CommandValue.withValues(values, () ->
                    command.nextState(oldState, args, new CommandValue<>(id)));
            state = newState;
            try {
                CommandValue.withValues(values, () -> command.postCondition(oldState, newState, args, result));
            } catch (Throwable ex) {
                return CommandResult.fail(state, ex);
            }
            return CommandResult.success(state);
        } catch (Throwable ex) {
            return CommandResult.fail(state, ex);
        }
    }

    private static <S, A, R> CommandResult<S> runAbstractCommand(
            GeneratedCommand<S, A, R> generated,
            S state) {
        int id = generated.getId();
        Command<S, A, R> command = generated.getCommand();
        A args = generated.getArgs();
        try {
            if (!command.preCondition(state, args)) {
                return CommandResult.fail(state, new Error("Precondition failed"));
            }
            state = command.nextState(state, args, new CommandValue<>(id));
            return CommandResult.success(state);
        } catch (Throwable ex) {
            return CommandResult.fail(state, ex);
        }
    }

    public boolean isValid() {
        CommandResult<S> result = CommandResult.success(initialState);
        for (GeneratedCommand<S, ?, ?> generated : commands) {
            result = runAbstractCommand(generated, result.getState());
            if (result.isFailed()) {
                break;
            }
        }
        return !result.isFailed();
    }

    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder();
        Iterator<GeneratedCommand<S, ?, ?>> iterator = commands.iterator();
        while (iterator.hasNext()) {
            builder.append(iterator.next());
            if (iterator.hasNext()) {
                builder.append(", \n\t");
            }
        }
        return "\n\t" + builder.toString();
    }
}