mirror of
https://github.com/vanitasvitae/Smack.git
synced 2024-11-22 20:12:07 +01:00
Use LinkedHashMap for form fields
for efficient lookup.
This commit is contained in:
parent
189f524195
commit
265e5c69d5
4 changed files with 25 additions and 28 deletions
|
@ -215,12 +215,7 @@ public final class FileTransferNegotiator extends Manager {
|
||||||
}
|
}
|
||||||
|
|
||||||
private static FormField getStreamMethodField(DataForm form) {
|
private static FormField getStreamMethodField(DataForm form) {
|
||||||
for (FormField field : form.getFields()) {
|
return form.getField(STREAM_DATA_FIELD_NAME);
|
||||||
if (field.getVariable().equals(STREAM_DATA_FIELD_NAME)) {
|
|
||||||
return field;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private StreamNegotiator getNegotiator(final FormField field)
|
private StreamNegotiator getNegotiator(final FormField field)
|
||||||
|
|
|
@ -350,16 +350,7 @@ public class Form {
|
||||||
* @return the field of the form whose variable matches the specified variable.
|
* @return the field of the form whose variable matches the specified variable.
|
||||||
*/
|
*/
|
||||||
public FormField getField(String variable) {
|
public FormField getField(String variable) {
|
||||||
if (variable == null || variable.equals("")) {
|
return dataForm.getField(variable);
|
||||||
throw new IllegalArgumentException("Variable must not be null or blank.");
|
|
||||||
}
|
|
||||||
// Look for the field whose variable matches the requested variable
|
|
||||||
for (FormField field : getFields()) {
|
|
||||||
if (variable.equals(field.getVariable())) {
|
|
||||||
return field;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -22,6 +22,7 @@ import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import org.jivesoftware.smack.packet.NamedElement;
|
import org.jivesoftware.smack.packet.NamedElement;
|
||||||
|
import org.jivesoftware.smack.util.StringUtils;
|
||||||
import org.jivesoftware.smack.util.XmlStringBuilder;
|
import org.jivesoftware.smack.util.XmlStringBuilder;
|
||||||
import org.jivesoftware.smackx.xdatavalidation.packet.ValidateElement;
|
import org.jivesoftware.smackx.xdatavalidation.packet.ValidateElement;
|
||||||
|
|
||||||
|
@ -150,7 +151,7 @@ public class FormField implements NamedElement {
|
||||||
* @param variable the variable name of the question.
|
* @param variable the variable name of the question.
|
||||||
*/
|
*/
|
||||||
public FormField(String variable) {
|
public FormField(String variable) {
|
||||||
this.variable = variable;
|
this.variable = StringUtils.requireNotNullOrEmpty(variable, "Variable must not be null or empty");
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -158,7 +159,7 @@ public class FormField implements NamedElement {
|
||||||
* name.
|
* name.
|
||||||
*/
|
*/
|
||||||
public FormField() {
|
public FormField() {
|
||||||
this(null);
|
this.variable = null;
|
||||||
this.type = Type.fixed;
|
this.type = Type.fixed;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -25,8 +25,10 @@ import org.jivesoftware.smackx.xdata.FormField;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
|
import java.util.LinkedHashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Represents a form that could be use for gathering data as well as for reporting data
|
* Represents a form that could be use for gathering data as well as for reporting data
|
||||||
|
@ -71,7 +73,7 @@ public class DataForm implements ExtensionElement {
|
||||||
private List<String> instructions = new ArrayList<String>();
|
private List<String> instructions = new ArrayList<String>();
|
||||||
private ReportedData reportedData;
|
private ReportedData reportedData;
|
||||||
private final List<Item> items = new ArrayList<Item>();
|
private final List<Item> items = new ArrayList<Item>();
|
||||||
private final List<FormField> fields = new ArrayList<FormField>();
|
private final Map<String, FormField> fields = new LinkedHashMap<>();
|
||||||
private final List<Element> extensionElements = new ArrayList<Element>();
|
private final List<Element> extensionElements = new ArrayList<Element>();
|
||||||
|
|
||||||
public DataForm(Type type) {
|
public DataForm(Type type) {
|
||||||
|
@ -139,7 +141,7 @@ public class DataForm implements ExtensionElement {
|
||||||
*/
|
*/
|
||||||
public List<FormField> getFields() {
|
public List<FormField> getFields() {
|
||||||
synchronized (fields) {
|
synchronized (fields) {
|
||||||
return Collections.unmodifiableList(new ArrayList<FormField>(fields));
|
return new ArrayList<>(fields.values());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -152,13 +154,21 @@ public class DataForm implements ExtensionElement {
|
||||||
*/
|
*/
|
||||||
public FormField getField(String variableName) {
|
public FormField getField(String variableName) {
|
||||||
synchronized (fields) {
|
synchronized (fields) {
|
||||||
for (FormField field : fields) {
|
return fields.get(variableName);
|
||||||
if (variableName.equals(field.getVariable())) {
|
|
||||||
return field;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if a form field with the given variable name exists.
|
||||||
|
*
|
||||||
|
* @param variableName
|
||||||
|
* @return true if a form field with the variable name exists, false otherwise.
|
||||||
|
* @since 4.2
|
||||||
|
*/
|
||||||
|
public boolean hasField(String variableName) {
|
||||||
|
synchronized (fields) {
|
||||||
|
return fields.containsKey(variableName);
|
||||||
}
|
}
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getElementName() {
|
public String getElementName() {
|
||||||
|
@ -206,12 +216,12 @@ public class DataForm implements ExtensionElement {
|
||||||
*/
|
*/
|
||||||
public void addField(FormField field) {
|
public void addField(FormField field) {
|
||||||
String fieldVariableName = field.getVariable();
|
String fieldVariableName = field.getVariable();
|
||||||
if (fieldVariableName != null && getField(fieldVariableName) != null) {
|
if (hasField(fieldVariableName)) {
|
||||||
throw new IllegalArgumentException("This data form already contains a form field with the variable name '"
|
throw new IllegalArgumentException("This data form already contains a form field with the variable name '"
|
||||||
+ fieldVariableName + "'");
|
+ fieldVariableName + "'");
|
||||||
}
|
}
|
||||||
synchronized (fields) {
|
synchronized (fields) {
|
||||||
fields.add(field);
|
fields.put(fieldVariableName, field);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue