ListSingleFormField.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 ListSingleFormField  extends AbstractSingleStringValueFormField implements FormFieldWithOptions {

  22.     private final List<Option> options;

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

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

  31.     public Builder asBuilder() {
  32.         return new Builder(this);
  33.     }

  34.     @Override
  35.     protected void populateExtraXmlChildElements() {
  36.         extraXmlChildElements = new ArrayList<>(1 + options.size());
  37.         CharSequence value = getValue();
  38.         if (value != null) {
  39.             extraXmlChildElements.add(new FormField.Value(value));
  40.         }

  41.         extraXmlChildElements.addAll(options);
  42.     }

  43.     public static final class Builder
  44.                     extends AbstractSingleStringValueFormField.Builder<ListSingleFormField, ListSingleFormField.Builder>
  45.                     implements FormFieldWithOptions.Builder<Builder> {

  46.         private List<Option> options;

  47.         private Builder(ListSingleFormField textSingleFormField) {
  48.             super(textSingleFormField);
  49.         }

  50.         Builder(String fieldName) {
  51.             super(fieldName, FormField.Type.list_single);
  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 ListSingleFormField build() {
  63.             return new ListSingleFormField(this);
  64.         }

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

  70. }