summaryrefslogtreecommitdiff
path: root/dicore3/command/src/main/java/io/dico/dicore/command/ChildCommandAddress.java
blob: 9a26f61fd69757559eb7667761f61f372d42d14e (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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package io.dico.dicore.command;

import io.dico.dicore.command.predef.DefaultGroupCommand;
import io.dico.dicore.command.predef.HelpCommand;

import java.util.*;

public class ChildCommandAddress extends ModifiableCommandAddress {
    ModifiableCommandAddress parent;
    final List<String> namesModifiable = new ArrayList<>(4);
    List<String> names = namesModifiable;
    Command command;
    boolean isCommandTrailing;

    public ChildCommandAddress() {
    }

    public ChildCommandAddress(Command command) {
        this.command = command;
    }

    public ChildCommandAddress(Command command, String name, String... aliases) {
        this(command);
        addNameAndAliases(name, aliases);
    }

    public static ChildCommandAddress newPlaceHolderCommand(String name, String... aliases) {
        ChildCommandAddress rv = new ChildCommandAddress();
        rv.setupAsPlaceholder(name, aliases);
        return rv;
    }

    public void setupAsPlaceholder(String name, String... aliases) {
        if (!hasCommand()) {
            setCommand(DefaultGroupCommand.getInstance());
        }

        addNameAndAliases(name, aliases);
        HelpCommand.registerAsChild(this);
    }

    @Override
    public boolean isRoot() {
        return false;
    }

    @Override
    public ModifiableCommandAddress getParent() {
        return parent;
    }

    @Override
    public Command getCommand() {
        return command;
    }

    @Override
    public void setCommand(Command command) {
        if (hasUserDeclaredCommand()) {
            throw new IllegalStateException("Command is already set at address \"" + getAddress() + "\"");
        }
        this.command = command;
    }

    @Override
    public List<String> getNames() {
        return names;
    }

    public void addNameAndAliases(String name, String... aliases) {
        names.add(name);
        names.addAll(Arrays.asList(aliases));
    }

    @Override
    public String getMainKey() {
        return namesModifiable.isEmpty() ? null : namesModifiable.get(0);
    }

    @Override
    public String getAddress() {
        ICommandAddress address = this;
        int depth = getDepth();
        String[] keys = new String[depth];
        for (int i = depth - 1; i >= 0; i--) {
            keys[i] = address.getMainKey();
            address = address.getParent();
        }
        return String.join(" ", keys);
    }

    public void finalizeNames() {
        if (names == namesModifiable) {
            names = Collections.unmodifiableList(namesModifiable);
        }
    }

    Iterator<String> modifiableNamesIterator() {
        return namesModifiable.iterator();
    }

    void setParent(ModifiableCommandAddress parent) {
        finalizeNames();
        this.parent = parent;
    }

    @Override
    public boolean isCommandTrailing() {
        return isCommandTrailing;
    }

    @Override
    public void setCommandTrailing(boolean trailing) {
        if (hasChildren()) {
            throw new IllegalStateException("Address already has children, this property can't be modified");
        }
        isCommandTrailing = trailing;
    }

}