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.Collection;
021import java.util.List;
022
023import org.jivesoftware.smack.util.CollectionUtil;
024
025import org.jxmpp.jid.Jid;
026
027public class JidMultiFormField extends FormField {
028
029    private final List<Jid> values;
030
031    protected JidMultiFormField(Builder builder) {
032        super(builder);
033        values = CollectionUtil.cloneAndSeal(builder.values);
034    }
035
036    @Override
037    public List<Jid> getValues() {
038        return values;
039    }
040
041    public Builder asBuilder() {
042        return new Builder(this);
043    }
044
045    public static final class Builder extends FormField.Builder<JidMultiFormField, JidMultiFormField.Builder> {
046        private List<Jid> values;
047
048        private Builder(JidMultiFormField jidMultiFormField) {
049            super(jidMultiFormField);
050            values = CollectionUtil.newListWith(jidMultiFormField.getValues());
051        }
052
053        Builder(String fieldName) {
054            super(fieldName, FormField.Type.jid_multi);
055        }
056
057        private void ensureValuesAreInitialized() {
058            if (values == null) {
059                values = new ArrayList<>();
060            }
061        }
062
063        @Override
064        protected void resetInternal() {
065            values = null;
066        }
067
068        public Builder addValue(Jid jid) {
069            ensureValuesAreInitialized();
070
071            values.add(jid);
072            return this;
073        }
074
075        public Builder addValues(Collection<? extends Jid> jids) {
076            ensureValuesAreInitialized();
077
078            values.addAll(jids);
079            return this;
080        }
081
082        @Override
083        public JidMultiFormField build() {
084            return new JidMultiFormField(this);
085        }
086
087        @Override
088        public Builder getThis() {
089            return this;
090        }
091    }
092
093
094}