JidMultiFormField.java

  1. /**
  2.  *
  3.  * Copyright 2020-2021 Florian Schmaus.
  4.  *
  5.  * Licensed under the Apache License, Version 2.0 (the "License");
  6.  * you may not use this file except in compliance with the License.
  7.  * You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.jivesoftware.smackx.xdata;

  18. import java.util.ArrayList;
  19. import java.util.Collection;
  20. import java.util.List;

  21. import org.jivesoftware.smack.util.CollectionUtil;

  22. import org.jxmpp.jid.Jid;
  23. import org.jxmpp.jid.impl.JidCreate;
  24. import org.jxmpp.stringprep.XmppStringprepException;

  25. public final class JidMultiFormField extends FormField {

  26.     private final List<Jid> values;

  27.     private final List<Value> rawValues;

  28.     JidMultiFormField(Builder builder) {
  29.         super(builder);
  30.         values = CollectionUtil.cloneAndSeal(builder.values);
  31.         rawValues = CollectionUtil.cloneAndSeal(builder.rawValues);
  32.     }

  33.     @Override
  34.     public List<Jid> getValues() {
  35.         return values;
  36.     }

  37.     @Override
  38.     public List<Value> getRawValues() {
  39.         return rawValues;
  40.     }

  41.     public Builder asBuilder() {
  42.         return new Builder(this);
  43.     }

  44.     public static final class Builder extends FormField.Builder<JidMultiFormField, JidMultiFormField.Builder> {
  45.         private List<Jid> values;

  46.         private List<Value> rawValues;

  47.         private Builder(JidMultiFormField jidMultiFormField) {
  48.             super(jidMultiFormField);
  49.             values = CollectionUtil.newListWith(jidMultiFormField.getValues());
  50.         }

  51.         Builder(String fieldName) {
  52.             super(fieldName, FormField.Type.jid_multi);
  53.         }

  54.         private void ensureValuesAreInitialized() {
  55.             if (values == null) {
  56.                 values = new ArrayList<>();
  57.                 rawValues = new ArrayList<>();
  58.             }
  59.         }

  60.         @Override
  61.         protected void resetInternal() {
  62.             values = null;
  63.             rawValues = null;
  64.         }

  65.         public Builder addValue(Jid jid) {
  66.             Value value = new Value(jid);

  67.             ensureValuesAreInitialized();
  68.             values.add(jid);
  69.             rawValues.add(value);

  70.             return getThis();
  71.         }

  72.         public Builder addValue(Value value) throws XmppStringprepException {
  73.             Jid jid = JidCreate.from(value.getValue());

  74.             ensureValuesAreInitialized();
  75.             values.add(jid);
  76.             rawValues.add(value);

  77.             return this;
  78.         }

  79.         public Builder addValues(Collection<? extends Jid> jids) {
  80.             for (Jid jid : jids) {
  81.                 addValue(jid);
  82.             }
  83.             return this;
  84.         }

  85.         @Override
  86.         public JidMultiFormField build() {
  87.             return new JidMultiFormField(this);
  88.         }

  89.         @Override
  90.         public Builder getThis() {
  91.             return this;
  92.         }
  93.     }


  94. }