001/**
002 *
003 * Copyright 2020 Florian Schmaus
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.jivesoftware.smackx.xdata;
018
019import java.util.ArrayList;
020import java.util.List;
021
022import org.jivesoftware.smack.util.CollectionUtil;
023
024public class ListMultiFormField extends AbstractMultiFormField implements FormFieldWithOptions {
025
026    private final List<Option> options;
027
028    protected ListMultiFormField(Builder builder) {
029        super(builder);
030        options = CollectionUtil.cloneAndSeal(builder.options);
031    }
032
033    @Override
034    public List<Option> getOptions() {
035        return options;
036    }
037
038    @Override
039    protected void populateExtraXmlChildElements() {
040        super.populateExtraXmlChildElements();
041        extraXmlChildElements.addAll(options);
042    }
043
044    public Builder asBuilder() {
045        return new Builder(this);
046    }
047
048    public static final class Builder
049                    extends AbstractMultiFormField.Builder<ListMultiFormField, ListMultiFormField.Builder>
050                    implements FormFieldWithOptions.Builder<Builder> {
051
052        private List<Option> options;
053
054        private Builder(ListMultiFormField textMultiFormField) {
055            super(textMultiFormField);
056        }
057
058        Builder(String fieldName) {
059            super(fieldName, FormField.Type.list_multi);
060        }
061
062        @Override
063        public Builder addValue(CharSequence value) {
064            return super.addValueVerbatim(value);
065        }
066
067        @Override
068        public Builder addOption(Option option) {
069            if (options == null) {
070                options = new ArrayList<>();
071            }
072            options.add(option);
073            return this;
074        }
075
076        @Override
077        public ListMultiFormField build() {
078            return new ListMultiFormField(this);
079        }
080
081        @Override
082        public Builder getThis() {
083            return this;
084        }
085
086    }
087
088}