1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2025-12-15 15:31:08 +01:00

Change FormField value(s) type from String to CharSequence

This commit is contained in:
Florian Schmaus 2018-04-06 13:24:54 +02:00
parent 9b5dafe541
commit 1d88c857b5
12 changed files with 79 additions and 44 deletions

View file

@ -277,7 +277,7 @@ public class Form {
* @throws IllegalStateException if the form is not of type "submit".
* @throws IllegalArgumentException if the form does not include the specified variable.
*/
public void setAnswer(String variable, List<String> values) {
public void setAnswer(String variable, List<? extends CharSequence> values) {
if (!isSubmitType()) {
throw new IllegalStateException("Cannot set an answer if the form is not of type " +
"\"submit\"");
@ -324,7 +324,7 @@ public class Form {
// Clear the old values
field.resetValues();
// Set the default value
for (String value : field.getValues()) {
for (CharSequence value : field.getValues()) {
field.addValue(value);
}
}
@ -504,7 +504,7 @@ public class Form {
if (field.getType() == FormField.Type.hidden) {
// Since a hidden field could have many values we need to collect them
// in a list
List<String> values = new ArrayList<>();
List<CharSequence> values = new ArrayList<>();
values.addAll(field.getValues());
form.setAnswer(field.getVariable(), values);
}

View file

@ -142,7 +142,7 @@ public class FormField implements NamedElement {
private String label;
private Type type;
private final List<Option> options = new ArrayList<>();
private final List<String> values = new ArrayList<>();
private final List<CharSequence> values = new ArrayList<>();
private ValidateElement validateElement;
/**
@ -226,12 +226,46 @@ public class FormField implements NamedElement {
*
* @return a List of the default values or answered values of the question.
*/
public List<String> getValues() {
public List<CharSequence> getValues() {
synchronized (values) {
return Collections.unmodifiableList(new ArrayList<>(values));
}
}
/**
* Returns the values a String. Note that you should use {@link #getValues()} whenever possible instead of this
* method.
*
* @return a list of Strings representing the values
* @see #getValues()
* @since 4.3
*/
public List<String> getValuesAsString() {
List<CharSequence> valuesAsCharSequence = getValues();
List<String> res = new ArrayList<>(valuesAsCharSequence.size());
for (CharSequence value : valuesAsCharSequence) {
res.add(value.toString());
}
return res;
}
/**
* Returns the first value of this form fold or {@code null}.
*
* @return the first value or {@code null}
* @since 4.3
*/
public String getFirstValue() {
CharSequence firstValue;
synchronized (values) {
firstValue = values.get(0);
}
if (firstValue == null) {
return null;
}
return firstValue.toString();
}
/**
* Returns the variable name that the question is filling out.
* <p>
@ -321,7 +355,7 @@ public class FormField implements NamedElement {
*
* @param value a default value or an answered value of the question.
*/
public void addValue(String value) {
public void addValue(CharSequence value) {
synchronized (values) {
values.add(value);
}
@ -333,7 +367,7 @@ public class FormField implements NamedElement {
*
* @param newValues default values or an answered values of the question.
*/
public void addValues(List<String> newValues) {
public void addValues(List<? extends CharSequence> newValues) {
synchronized (values) {
values.addAll(newValues);
}
@ -377,7 +411,7 @@ public class FormField implements NamedElement {
buf.optElement("desc", getDescription());
buf.condEmptyElement(isRequired(), "required");
// Loop through all the values and append them to the string buffer
for (String value : getValues()) {
for (CharSequence value : getValues()) {
buf.element("value", value);
}
// Loop through all the values and append them to the string buffer