1
0
Fork 0
mirror of https://codeberg.org/Mercury-IM/Smack synced 2024-12-26 06:07:59 +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

@ -157,13 +157,13 @@ public final class HttpFileUploadManager extends Manager {
return new UploadService(address, version);
}
List<String> values = field.getValues();
if (values.isEmpty()) {
String maxFileSizeValue = field.getFirstValue();
if (maxFileSizeValue == null) {
return new UploadService(address, version);
}
Long maxFileSize = Long.valueOf(values.get(0));
Long maxFileSize = Long.valueOf(maxFileSizeValue);
return new UploadService(address, version, maxFileSize);
}

View file

@ -723,10 +723,10 @@ public final class EntityCapsManager extends Manager {
return new CapsVersionAndHash(version, hash);
}
private static void formFieldValuesToCaps(List<String> i, StringBuilder sb) {
SortedSet<String> fvs = new TreeSet<>();
private static void formFieldValuesToCaps(List<CharSequence> i, StringBuilder sb) {
SortedSet<CharSequence> fvs = new TreeSet<>();
fvs.addAll(i);
for (String fv : fvs) {
for (CharSequence fv : fvs) {
sb.append(fv);
sb.append('<');
}

View file

@ -341,7 +341,7 @@ public final class FileTransferNegotiator extends Manager {
private StreamNegotiator getOutgoingNegotiator(final FormField field) throws NoAcceptableTransferMechanisms {
boolean isByteStream = false;
boolean isIBB = false;
for (String variable : field.getValues()) {
for (CharSequence variable : field.getValues()) {
if (variable.equals(Bytestream.NAMESPACE) && !IBB_ONLY) {
isByteStream = true;
}

View file

@ -107,7 +107,7 @@ public class MucConfigFormManager {
// Set the local variables according to the fields found in the answer form
if (answerForm.hasField(MUC_ROOMCONFIG_ROOMOWNERS)) {
// Set 'owners' to the currently configured owners
List<String> ownerStrings = answerForm.getField(MUC_ROOMCONFIG_ROOMOWNERS).getValues();
List<CharSequence> ownerStrings = answerForm.getField(MUC_ROOMCONFIG_ROOMOWNERS).getValues();
owners = new ArrayList<>(ownerStrings.size());
JidUtil.jidsFrom(ownerStrings, owners, null);
}

View file

@ -19,6 +19,7 @@ package org.jivesoftware.smackx.muc;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Collections;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
@ -29,6 +30,7 @@ import org.jivesoftware.smackx.xdata.FormField;
import org.jxmpp.jid.EntityBareJid;
import org.jxmpp.jid.Jid;
import org.jxmpp.jid.util.JidUtil;
/**
* Represents the room information that was discovered using Service Discovery. It's possible to
@ -97,7 +99,7 @@ public class RoomInfo {
/**
* Contact Address
*/
private final List<String> contactJid;
private final List<EntityBareJid> contactJid;
/**
* Natural Language for Room Discussions
@ -157,7 +159,7 @@ public class RoomInfo {
int occupantsCount = -1;
String description = "";
int maxhistoryfetch = -1;
List<String> contactJid = null;
List<EntityBareJid> contactJid = null;
String lang = null;
String ldapgroup = null;
Boolean subjectmod = null;
@ -169,49 +171,48 @@ public class RoomInfo {
FormField descField = form.getField("muc#roominfo_description");
if (descField != null && !descField.getValues().isEmpty()) {
// Prefer the extended result description
description = descField.getValues().get(0);
description = descField.getFirstValue();
}
FormField subjField = form.getField("muc#roominfo_subject");
if (subjField != null && !subjField.getValues().isEmpty()) {
subject = subjField.getValues().get(0);
subject = subjField.getFirstValue();
}
FormField occCountField = form.getField("muc#roominfo_occupants");
if (occCountField != null && !occCountField.getValues().isEmpty()) {
occupantsCount = Integer.parseInt(occCountField.getValues().get(
0));
occupantsCount = Integer.parseInt(occCountField.getFirstValue());
}
FormField maxhistoryfetchField = form.getField("muc#maxhistoryfetch");
if (maxhistoryfetchField != null && !maxhistoryfetchField.getValues().isEmpty()) {
maxhistoryfetch = Integer.parseInt(maxhistoryfetchField.getValues().get(
0));
maxhistoryfetch = Integer.parseInt(maxhistoryfetchField.getFirstValue());
}
FormField contactJidField = form.getField("muc#roominfo_contactjid");
if (contactJidField != null && !contactJidField.getValues().isEmpty()) {
contactJid = contactJidField.getValues();
List<CharSequence> contactJidValues = contactJidField.getValues();
contactJid = JidUtil.filterEntityBareJidList(JidUtil.jidSetFrom(contactJidValues));
}
FormField langField = form.getField("muc#roominfo_lang");
if (langField != null && !langField.getValues().isEmpty()) {
lang = langField.getValues().get(0);
lang = langField.getFirstValue();
}
FormField ldapgroupField = form.getField("muc#roominfo_ldapgroup");
if (ldapgroupField != null && !ldapgroupField.getValues().isEmpty()) {
ldapgroup = ldapgroupField.getValues().get(0);
ldapgroup = ldapgroupField.getFirstValue();
}
FormField subjectmodField = form.getField("muc#roominfo_subjectmod");
if (subjectmodField != null && !subjectmodField.getValues().isEmpty()) {
subjectmod = Boolean.valueOf(subjectmodField.getValues().get(0));
subjectmod = Boolean.valueOf(subjectmodField.getFirstValue());
}
FormField urlField = form.getField("muc#roominfo_logs");
if (urlField != null && !urlField.getValues().isEmpty()) {
String urlString = urlField.getValues().get(0);
String urlString = urlField.getFirstValue();
try {
logs = new URL(urlString);
} catch (MalformedURLException e) {
@ -221,7 +222,7 @@ public class RoomInfo {
FormField pubsubField = form.getField("muc#roominfo_pubsub");
if (pubsubField != null && !pubsubField.getValues().isEmpty()) {
pubsub = pubsubField.getValues().get(0);
pubsub = pubsubField.getFirstValue();
}
}
this.description = description;
@ -354,8 +355,8 @@ public class RoomInfo {
*
* @return a list of contact addresses for this room.
*/
public List<String> getContactJids() {
return contactJid;
public List<EntityBareJid> getContactJids() {
return Collections.unmodifiableList(contactJid);
}
/**

View file

@ -103,7 +103,7 @@ public class OfflineMessageManager {
namespace);
Form extendedInfo = Form.getFormFrom(info);
if (extendedInfo != null) {
String value = extendedInfo.getField("number_of_messages").getValues().get(0);
String value = extendedInfo.getField("number_of_messages").getFirstValue();
return Integer.parseInt(value);
}
return 0;

View file

@ -606,7 +606,7 @@ public class ConfigureForm extends Form {
StringBuilder valuesBuilder = new StringBuilder();
for (String value : formField.getValues()) {
for (CharSequence value : formField.getValues()) {
if (valuesBuilder.length() > 0)
result.append(',');
valuesBuilder.append(value);
@ -628,13 +628,13 @@ public class ConfigureForm extends Form {
private String getFieldValue(ConfigureNodeFields field) {
FormField formField = getField(field.getFieldName());
return (formField.getValues().isEmpty()) ? null : formField.getValues().get(0);
return formField.getFirstValue();
}
private List<String> getFieldValues(ConfigureNodeFields field) {
FormField formField = getField(field.getFieldName());
return formField.getValues();
return formField.getValuesAsString();
}
private void addField(ConfigureNodeFields nodeField, FormField.Type type) {

View file

@ -196,13 +196,13 @@ public class SubscribeForm extends Form {
private String getFieldValue(SubscribeOptionFields field) {
FormField formField = getField(field.getFieldName());
return formField.getValues().get(0);
return formField.getFirstValue();
}
private List<String> getFieldValues(SubscribeOptionFields field) {
FormField formField = getField(field.getFieldName());
return formField.getValues();
return formField.getValuesAsString();
}
private void addField(SubscribeOptionFields nodeField, FormField.Type type) {

View file

@ -74,7 +74,7 @@ public class ReportedData {
List<Field> fieldList = new ArrayList<>(columns.size());
for (FormField field : item.getFields()) {
// The field is created with all the values of the data form's field
List<String> values = new ArrayList<>();
List<CharSequence> values = new ArrayList<>();
values.addAll(field.getValues());
fieldList.add(new Field(field.getVariable(), values));
}
@ -205,7 +205,7 @@ public class ReportedData {
* @param variable the variable to match.
* @return the values of the field whose variable matches the requested variable.
*/
public List<String> getValues(String variable) {
public List<CharSequence> getValues(String variable) {
for (Field field : getFields()) {
if (variable.equalsIgnoreCase(field.getVariable())) {
return field.getValues();
@ -226,9 +226,9 @@ public class ReportedData {
public static class Field {
private final String variable;
private final List<String> values;
private final List<? extends CharSequence> values;
public Field(String variable, List<String> values) {
public Field(String variable, List<? extends CharSequence> values) {
this.variable = variable;
this.values = values;
}
@ -247,7 +247,7 @@ public class ReportedData {
*
* @return the returned values of the search.
*/
public List<String> getValues() {
public List<CharSequence> getValues() {
return Collections.unmodifiableList(values);
}
}

View file

@ -85,7 +85,7 @@ class SimpleUserSearch extends IQ {
}
private static String getSingleValue(FormField formField) {
List<String> values = formField.getValues();
List<String> values = formField.getValuesAsString();
if (values.isEmpty()) {
return "";
} else {

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