aboutsummaryrefslogtreecommitdiffstats
path: root/main/src/cgeo/geocaching/network/Parameters.java
diff options
context:
space:
mode:
Diffstat (limited to 'main/src/cgeo/geocaching/network/Parameters.java')
-rw-r--r--main/src/cgeo/geocaching/network/Parameters.java46
1 files changed, 41 insertions, 5 deletions
diff --git a/main/src/cgeo/geocaching/network/Parameters.java b/main/src/cgeo/geocaching/network/Parameters.java
index e65bec2..5c0328a 100644
--- a/main/src/cgeo/geocaching/network/Parameters.java
+++ b/main/src/cgeo/geocaching/network/Parameters.java
@@ -12,7 +12,7 @@ import java.util.Comparator;
/**
* List of key/values pairs to be used in a GET or POST request.
- *
+ *
*/
public class Parameters extends ArrayList<NameValuePair> {
@@ -44,14 +44,16 @@ public class Parameters extends ArrayList<NameValuePair> {
* list of key/value pairs
* @throws InvalidParameterException
* if the number of key/values is unbalanced
+ * @return the object itself to facilitate chaining
*/
- public void put(final String... keyValues) {
+ public Parameters put(final String... keyValues) {
if (keyValues.length % 2 == 1) {
throw new InvalidParameterException("odd number of parameters");
}
for (int i = 0; i < keyValues.length; i += 2) {
add(new BasicNameValuePair(keyValues[i], keyValues[i + 1]));
}
+ return this;
}
/**
@@ -63,12 +65,46 @@ public class Parameters extends ArrayList<NameValuePair> {
Collections.sort(this, comparator);
}
- /**
- * @return the URL encoded string corresponding to those parameters
- */
@Override
public String toString() {
return URLEncodedUtils.format(this, HTTP.UTF_8);
}
+ /**
+ * Extend or create a Parameters object with new key/value pairs.
+ *
+ * @param params
+ * an existing object or null to create a new one
+ * @param keyValues
+ * list of key/value pair
+ * @throws InvalidParameterException
+ * if the number of key/values is unbalanced
+ * @return the object itself if it is non-null, a new one otherwise
+ */
+ public static Parameters extend(final Parameters params, final String... keyValues) {
+ if (params == null) {
+ return new Parameters(keyValues);
+ } else {
+ return params.put(keyValues);
+ }
+ }
+
+ /**
+ * Merge two (possibly null) Parameters object.
+ *
+ * @param params
+ * the object to merge into if non-null
+ * @param extra
+ * the object to merge from if non-null
+ * @return params with extra data if params was non-null, extra otherwise
+ */
+ public static Parameters merge(final Parameters params, final Parameters extra) {
+ if (params == null) {
+ return extra;
+ } else if (extra != null) {
+ params.addAll(extra);
+ }
+ return params;
+ }
+
}