1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2024-06-11 14:17:08 +02:00
Smack/smack-extensions/src/main/java/org/jivesoftware/smackx/xdata/AbstractMultiFormField.java
Florian Schmaus 77e26fc575 Re-work data form API
Apply builder pattern to form fields and replace getVariable() with
getFieldName(). Refer to the field name as "field name" instead of
"variable" everyone, just as XEP-0004 does.

Improve the high-level form API: introduce FilledForm and FillableForm
which perform stronger validation and consistency checks.

Also add FormFieldRegistry to enable processing of 'submit' forms
where the form field types are omitted.

Smack also now does omit the form field type declaration on 'submit'
type forms, as it is allowed by XEP-0004.
2020-05-13 20:14:41 +02:00

93 lines
2.5 KiB
Java

/**
*
* Copyright 2020 Florian Schmaus
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx.xdata;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import org.jivesoftware.smack.util.CollectionUtil;
import org.jxmpp.util.XmppDateTime;
public class AbstractMultiFormField extends FormField {
private final List<String> values;
protected AbstractMultiFormField(Builder<?, ?> builder) {
super(builder);
values = CollectionUtil.cloneAndSeal(builder.values);
}
@Override
public final List<String> getValues() {
return values;
}
public abstract static class Builder<F extends FormField, B extends FormField.Builder<F, B>>
extends FormField.Builder<F, B> {
private List<String> values;
protected Builder(AbstractMultiFormField formField) {
super(formField);
values = CollectionUtil.newListWith(formField.getValues());
}
protected Builder(String fieldName, FormField.Type type) {
super(fieldName, type);
}
private void ensureValuesAreInitialized() {
if (values == null) {
values = new ArrayList<>();
}
}
@Override
protected void resetInternal() {
values = null;
}
public abstract B addValue(CharSequence value);
public B addValueVerbatim(CharSequence value) {
ensureValuesAreInitialized();
values.add(value.toString());
return getThis();
}
public final B addValue(Date date) {
String dateString = XmppDateTime.formatXEP0082Date(date);
return addValueVerbatim(dateString);
}
public final B addValues(Collection<? extends CharSequence> values) {
ensureValuesAreInitialized();
for (CharSequence value : values) {
this.values.add(value.toString());
}
return getThis();
}
}
}