ListMultiFormField.java

  1. /**
  2.  *
  3.  * Copyright 2020 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.List;

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

  21. public class ListMultiFormField extends AbstractMultiFormField implements FormFieldWithOptions {

  22.     private final List<Option> options;

  23.     protected ListMultiFormField(Builder builder) {
  24.         super(builder);
  25.         options = CollectionUtil.cloneAndSeal(builder.options);
  26.     }

  27.     @Override
  28.     public List<Option> getOptions() {
  29.         return options;
  30.     }

  31.     @Override
  32.     protected void populateExtraXmlChildElements() {
  33.         super.populateExtraXmlChildElements();
  34.         extraXmlChildElements.addAll(options);
  35.     }

  36.     public Builder asBuilder() {
  37.         return new Builder(this);
  38.     }

  39.     public static final class Builder
  40.                     extends AbstractMultiFormField.Builder<ListMultiFormField, ListMultiFormField.Builder>
  41.                     implements FormFieldWithOptions.Builder<Builder> {

  42.         private List<Option> options;

  43.         private Builder(ListMultiFormField textMultiFormField) {
  44.             super(textMultiFormField);
  45.         }

  46.         Builder(String fieldName) {
  47.             super(fieldName, FormField.Type.list_multi);
  48.         }

  49.         @Override
  50.         public Builder addValue(CharSequence value) {
  51.             return super.addValueVerbatim(value);
  52.         }

  53.         @Override
  54.         public Builder addOption(Option option) {
  55.             if (options == null) {
  56.                 options = new ArrayList<>();
  57.             }
  58.             options.add(option);
  59.             return this;
  60.         }

  61.         @Override
  62.         public ListMultiFormField build() {
  63.             return new ListMultiFormField(this);
  64.         }

  65.         @Override
  66.         public Builder getThis() {
  67.             return this;
  68.         }

  69.     }

  70. }