diff options
| author | Samuel Tardieu <sam@rfc1149.net> | 2011-10-03 12:58:16 +0200 |
|---|---|---|
| committer | Samuel Tardieu <sam@rfc1149.net> | 2011-10-03 14:30:28 +0200 |
| commit | dc14abe4b3da16f1cb085ed5c117485955df2551 (patch) | |
| tree | 94e9a2396ca9a6808ef8b2302ada41a4595d4c3a /main/src/cgeo/geocaching/Parameters.java | |
| parent | a9869b7f63a5954133198d2fa75fde168f649d85 (diff) | |
| download | cgeo-dc14abe4b3da16f1cb085ed5c117485955df2551.zip cgeo-dc14abe4b3da16f1cb085ed5c117485955df2551.tar.gz cgeo-dc14abe4b3da16f1cb085ed5c117485955df2551.tar.bz2 | |
Use more Parameters() with variable arguments
Diffstat (limited to 'main/src/cgeo/geocaching/Parameters.java')
| -rw-r--r-- | main/src/cgeo/geocaching/Parameters.java | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/main/src/cgeo/geocaching/Parameters.java b/main/src/cgeo/geocaching/Parameters.java index 5411850..fdbbe3b 100644 --- a/main/src/cgeo/geocaching/Parameters.java +++ b/main/src/cgeo/geocaching/Parameters.java @@ -1,16 +1,29 @@ package cgeo.geocaching; import org.apache.http.NameValuePair; +import org.apache.http.client.utils.URLEncodedUtils; import org.apache.http.message.BasicNameValuePair; +import org.apache.http.protocol.HTTP; +import java.security.InvalidParameterException; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; +/** + * List of key/values pairs to be used in a GET or POST request. + * + */ public class Parameters extends ArrayList<NameValuePair> { private static final long serialVersionUID = 1L; + /** + * @param keyValues + * list of initial key/value pairs + * @throws InvalidParameterException + * if the number of key/values is unbalanced + */ public Parameters(final String... keyValues) { super(); put(keyValues); @@ -24,14 +37,38 @@ public class Parameters extends ArrayList<NameValuePair> { } }; + /** + * Add new key/value pairs to the current parameters. + * + * @param keyValues + * list of key/value pairs + * @throws InvalidParameterException + * if the number of key/values is unbalanced + */ public void 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])); } } + /** + * Lexically sort key/value pairs first by key, then by value. + * + * Some signing algorithms need the values to be ordered before issuing the signature. + */ public void sort() { Collections.sort(this, comparator); } + /** + * @return the URL encoded string corresponding to those parameters + */ + @Override + public String toString() { + return URLEncodedUtils.format(this, HTTP.UTF_8); + } + } |
