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 ListSingleFormField extends AbstractSingleStringValueFormField implements FormFieldWithOptions { 025 026 private final List<Option> options; 027 028 protected ListSingleFormField(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 public Builder asBuilder() { 039 return new Builder(this); 040 } 041 042 @Override 043 protected void populateExtraXmlChildElements() { 044 extraXmlChildElements = new ArrayList<>(1 + options.size()); 045 CharSequence value = getValue(); 046 if (value != null) { 047 extraXmlChildElements.add(new FormField.Value(value)); 048 } 049 050 extraXmlChildElements.addAll(options); 051 } 052 053 public static final class Builder 054 extends AbstractSingleStringValueFormField.Builder<ListSingleFormField, ListSingleFormField.Builder> 055 implements FormFieldWithOptions.Builder<Builder> { 056 057 private List<Option> options; 058 059 private Builder(ListSingleFormField textSingleFormField) { 060 super(textSingleFormField); 061 } 062 063 Builder(String fieldName) { 064 super(fieldName, FormField.Type.list_single); 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 ListSingleFormField build() { 078 return new ListSingleFormField(this); 079 } 080 081 @Override 082 public Builder getThis() { 083 return this; 084 } 085 } 086 087}