summaryrefslogtreecommitdiff
path: root/dicore3/command/src/main/java/io/dico/dicore/command/parameter/ParameterList.java
blob: f35628c93920435c433abe9f632296c9936a1864 (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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package io.dico.dicore.command.parameter;

import java.util.*;

/**
 * IParameter definition for a command
 */
@SuppressWarnings("UnusedReturnValue")
public class ParameterList {
    //private ParameterList parent;
    private List<Parameter<?, ?>> indexedParameters;
    private Map<String, Parameter<?, ?>> byName;
    //private IArgumentPreProcessor argumentPreProcessor = IArgumentPreProcessor.NONE;
    private int requiredCount = -1;
    private boolean repeatFinalParameter;

    // if the final parameter is repeated and the command is implemented through reflection,
    // the repeated parameter is simply the last parameter of the method, rather than the last
    // indexed parameter. This might be a flag. As such, this field exists to ensure the correct
    // parameter is taken for repeating
    private boolean finalParameterMayBeFlag;

    /*
    public ParameterList(ParameterList parent) {
        this();
        if (parent.repeatFinalParameter) {
            throw new IllegalArgumentException("Parent may not have repeating parameters");
        }
        this.parent = parent;
    }*/

    public ParameterList() {
        this.indexedParameters = new ArrayList<>();
        this.byName = new LinkedHashMap<>();
        this.repeatFinalParameter = false;
    }

    /*
    public IArgumentPreProcessor getArgumentPreProcessor() {
        return argumentPreProcessor;
    }

    public ParameterList setArgumentPreProcessor(IArgumentPreProcessor argumentPreProcessor) {
        this.argumentPreProcessor = argumentPreProcessor == null ? IArgumentPreProcessor.NONE : argumentPreProcessor;
        return this;
    }*/

    public boolean repeatFinalParameter() {
        return repeatFinalParameter;
    }

    public ParameterList setRepeatFinalParameter(boolean repeatFinalParameter) {
        this.repeatFinalParameter = repeatFinalParameter;
        return this;
    }

    public boolean finalParameterMayBeFlag() {
        return finalParameterMayBeFlag;
    }

    public ParameterList setFinalParameterMayBeFlag(boolean finalParameterMayBeFlag) {
        this.finalParameterMayBeFlag = finalParameterMayBeFlag;
        return this;
    }

    public int getRequiredCount() {
        return requiredCount == -1 ? indexedParameters.size() : requiredCount;
    }

    public ParameterList setRequiredCount(int requiredCount) {
        this.requiredCount = requiredCount;
        return this;
    }

    public boolean hasAnyParameters() {
        return !byName.isEmpty();
    }

    public int getIndexedParameterCount() {
        return indexedParameters.size();
    }

    public List<Parameter<?, ?>> getIndexedParameters() {
        return Collections.unmodifiableList(indexedParameters);
    }

    public Parameter<?, ?> getParameterByName(String name) {
        return byName.get(name);
    }

    public String getIndexedParameterName(int index) {
        return indexedParameters.get(index).getName();
    }

    public Map<String, Parameter<?, ?>> getParametersByName() {
        return Collections.unmodifiableMap(byName);
    }

    /**
     * Add the given parameter to the end of this parameter list
     * Can be a flag
     *
     * @param parameter the parameter
     * @return this
     */
    public ParameterList addParameter(Parameter<?, ?> parameter) {
        return addParameter(-1, parameter);
    }

    /**
     * Add the given parameter to this parameter list
     * If the parameter is a flag, the index is ignored
     *
     * @param index     parameter index number, -1 if end
     * @param parameter the parameter
     * @return this
     * @throws NullPointerException if parameter is null
     */
    public ParameterList addParameter(int index, Parameter<?, ?> parameter) {
        //System.out.println("Added parameter " + parameter.getName() + ", flag: " + parameter.isFlag());
        byName.put(parameter.getName(), parameter);
        if (!parameter.isFlag()) {
            indexedParameters.add(index == -1 ? indexedParameters.size() : index, parameter);
        }
        return this;
    }

    public Parameter<?, ?> getRepeatedParameter() {
        if (!repeatFinalParameter) {
            return null;
        }
        if (finalParameterMayBeFlag) {
            Iterator<Parameter<?, ?>> iterator = byName.values().iterator();
            Parameter<?, ?> result = null;
            while (iterator.hasNext()) {
                result = iterator.next();
            }
            return result;
        }

        if (indexedParameters.isEmpty()) {
            return null;
        }

        return indexedParameters.get(indexedParameters.size() - 1);
    }

}