mirror of https://github.com/OpenIdentityPlatform/OpenDJ.git

Valery Kharseko
05.48.2024 4bddd152a9e15207d8003f6f74e70ebc6f07cc7e
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
149
150
/*
 * The contents of this file are subject to the terms of the Common Development and
 * Distribution License (the License). You may not use this file except in compliance with the
 * License.
 *
 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
 * specific language governing permission and limitations under the License.
 *
 * When distributing Covered Software, include this CDDL Header Notice in each file and include
 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
 * Header, with the fields enclosed by brackets [] replaced by your own identifying
 * information: "Portions Copyright [year] [name of copyright owner]".
 *
 * Copyright 2006-2008 Sun Microsystems, Inc.
 * Portions copyright 2014-2016 ForgeRock AS.
 */
package com.forgerock.opendj.cli;
 
import static com.forgerock.opendj.cli.CliMessages.ERR_MCARG_VALUE_NOT_ALLOWED;
 
import java.util.Arrays;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
 
import org.forgerock.i18n.LocalizableMessageBuilder;
 
/**
 * This class defines an argument type that will only accept one or more of a
 * specific set of string values.
 *
 * @param <V>
 *            The type of values returned by this argument.
 */
public final class MultiChoiceArgument<V> extends Argument {
 
    /**
     * Returns a builder which can be used for incrementally constructing a new
     * {@link MultiChoiceArgument<V>}.
     *
     * @param <V>
     *         The type of values returned by this argument.
     * @param longIdentifier
     *         The generic long identifier that will be used to refer to this argument.
     * @return A builder to continue building the {@link MultiChoiceArgument}.
     */
    public static <V> Builder<V> builder(final String longIdentifier) {
        return new Builder<>(longIdentifier);
    }
 
    /** A fluent API for incrementally constructing {@link MultiChoiceArgument<V>}. */
    public static final class Builder<V> extends ArgumentBuilder<Builder<V>, V, MultiChoiceArgument<V>> {
        private final List<V> allowedValues = new LinkedList<>();
 
        private Builder(final String longIdentifier) {
            super(longIdentifier);
        }
 
        @Override
        Builder<V> getThis() {
            return this;
        }
 
        /**
         * Specifies the set of values that are allowed for the {@link MultiChoiceArgument<V>}.
         *
         * @param allowedValues
         *         The {@link MultiChoiceArgument<V>} allowed values.
         * @return This builder.
         */
        public Builder<V> allowedValues(final Collection<V> allowedValues) {
            this.allowedValues.addAll(allowedValues);
            return getThis();
        }
 
        /**
         * Specifies the set of values that are allowed for the {@link MultiChoiceArgument<V>}.
         *
         * @param allowedValues
         *         The {@link MultiChoiceArgument<V>} allowed values.
         * @return This builder.
         */
        @SuppressWarnings("unchecked")
        public final Builder<V> allowedValues(final V... allowedValues) {
            this.allowedValues.addAll(Arrays.asList(allowedValues));
            return getThis();
        }
 
        @Override
        public MultiChoiceArgument<V> buildArgument() throws ArgumentException {
            return new MultiChoiceArgument<>(this, allowedValues);
        }
    }
 
    /** The set of values that will be allowed for use with this argument. */
    private final Collection<V> allowedValues;
 
    private <V1> MultiChoiceArgument(final Builder<V1> builder, final Collection<V> allowedValues)
            throws ArgumentException {
        super(builder);
        this.allowedValues = allowedValues;
    }
 
    /**
     * Retrieves the string value for this argument. If it has multiple values,
     * then the first will be returned. If it does not have any values, then the
     * default value will be returned.
     *
     * @return The string value for this argument, or <CODE>null</CODE> if there
     *         are no values and no default value has been given.
     * @throws ArgumentException
     *             The value cannot be parsed.
     */
    public V getTypedValue() throws ArgumentException {
        final String v = super.getValue();
        if (v == null) {
            return null;
        }
        for (final V allowedValue : allowedValues) {
            if (allowedValue.toString().equalsIgnoreCase(v)) {
                return allowedValue;
            }
        }
        throw new IllegalStateException("This MultiChoiceArgument value is not part of the allowed values.");
    }
 
    /**
     * Indicates whether the provided value is acceptable for use in this
     * argument.
     *
     * @param valueString
     *            The value for which to make the determination.
     * @param invalidReason
     *            A buffer into which the invalid reason may be written if the
     *            value is not acceptable.
     * @return <CODE>true</CODE> if the value is acceptable, or
     *         <CODE>false</CODE> if it is not.
     */
    @Override
    public boolean valueIsAcceptable(final String valueString, final LocalizableMessageBuilder invalidReason) {
        for (final V allowedValue : allowedValues) {
            if (allowedValue.toString().equalsIgnoreCase(valueString)) {
                return true;
            }
        }
        invalidReason.append(ERR_MCARG_VALUE_NOT_ALLOWED.get(longIdentifier, valueString));
 
        return false;
    }
}