001/** 002 * 003 * Copyright 2020-2021 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.Collections; 020import java.util.List; 021 022import org.jivesoftware.smack.util.CollectionUtil; 023 024public abstract class SingleValueFormField extends FormField { 025 026 private final Value rawValue; 027 028 protected SingleValueFormField(Builder<?, ?> builder) { 029 super(builder); 030 rawValue = builder.rawValue; 031 } 032 033 @Override 034 public final List<CharSequence> getValues() { 035 CharSequence value = getValue(); 036 return CollectionUtil.emptyOrSingletonListFrom(value); 037 } 038 039 public abstract CharSequence getValue(); 040 041 public final Value getRawValue() { 042 return rawValue; 043 } 044 045 @Override 046 public final List<Value> getRawValues() { 047 Value rawValue = getRawValue(); 048 return CollectionUtil.emptyOrSingletonListFrom(rawValue); 049 } 050 051 @Override 052 protected void populateExtraXmlChildElements() { 053 if (rawValue == null) { 054 return; 055 } 056 057 extraXmlChildElements = Collections.singletonList(rawValue); 058 } 059 060 public abstract static class Builder<F extends SingleValueFormField, B extends Builder<F, B>> 061 extends FormField.Builder<F, B> { 062 063 protected Builder(String fieldName, Type type) { 064 super(fieldName, type); 065 } 066 067 protected Builder(SingleValueFormField formField) { 068 super(formField); 069 rawValue = formField.getRawValue(); 070 } 071 072 protected Value rawValue; 073 074 @Override 075 protected void resetInternal() { 076 rawValue = null; 077 }; 078 079 } 080}