From e0810c82d3e8be54e4b364dbf47dd782dfc259ea Mon Sep 17 00:00:00 2001 From: Bob Carroll Date: Mon, 14 Oct 2013 15:51:50 -0700 Subject: [PATCH] replaced the map for custom field values with a ValueTuple list --- README.md | 9 ++--- src/main/java/net/rcarz/jiraclient/Field.java | 38 ++++++++++++------- 2 files changed, 28 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index 76a1618..a0c2292 100644 --- a/README.md +++ b/README.md @@ -45,7 +45,6 @@ Patches are welcome and appreciated. Please try to follow existing styles, and s ```java import java.util.ArrayList; -import java.util.HashMap; import java.util.List; import net.rcarz.jiraclient.BasicCredentials; @@ -149,10 +148,10 @@ public class Example { /* Set two new values for customfield_5678. */ issue.update() - .field("customfield_5678", new HashMap() {{ - put("value", "foo"); - put("value", "bar"); - put("id", "1234"); /* you can also update using the value ID */ + .field("customfield_5678", new ArrayList() {{ + add("foo"); + add("bar"); + add(Field.valueById("1234")); /* you can also update using the value ID */ }}) .execute(); diff --git a/src/main/java/net/rcarz/jiraclient/Field.java b/src/main/java/net/rcarz/jiraclient/Field.java index 9ffe8c0..89d2a8e 100644 --- a/src/main/java/net/rcarz/jiraclient/Field.java +++ b/src/main/java/net/rcarz/jiraclient/Field.java @@ -73,11 +73,16 @@ public final class Field { * Allowed value types. */ public enum ValueType { - KEY("key"), NAME("name"), ID_NUMBER("id"); - private String value; + KEY("key"), NAME("name"), ID_NUMBER("id"), VALUE("value"); + private String typeName; - private ValueType(String value) { - this.value = value; + private ValueType(String typeName) { + this.typeName = typeName; + } + + @Override + public String toString() { + return typeName; } }; @@ -106,7 +111,7 @@ public final class Field { * @param value */ public ValueTuple(ValueType type, Object value) { - this(type.value, value); + this(type.toString(), value); } } @@ -470,7 +475,7 @@ public final class Field { ValueTuple tuple = (ValueTuple)val; json.put(tuple.type, tuple.value.toString()); } else - json.put(ValueType.NAME.value, val.toString()); + json.put(ValueType.NAME.toString(), val.toString()); result.add(json.toString()); } else if (type.equals("string")) @@ -542,7 +547,7 @@ public final class Field { ValueTuple tuple = (ValueTuple)value; json.put(tuple.type, tuple.value.toString()); } else - json.put(ValueType.NAME.value, value.toString()); + json.put(ValueType.NAME.toString(), value.toString()); return json.toString(); } else if (m.type.equals("project") || m.type.equals("issuelink")) { @@ -552,12 +557,12 @@ public final class Field { ValueTuple tuple = (ValueTuple)value; json.put(tuple.type, tuple.value.toString()); } else - json.put(ValueType.KEY.value, value.toString()); + json.put(ValueType.KEY.toString(), value.toString()); return json.toString(); } else if (m.type.equals("string")) { - if (value instanceof Map) - return toJsonMap((Map)value); + if (value instanceof List) + return toJsonMap((List)value); return value.toString(); } @@ -568,15 +573,20 @@ public final class Field { /** * Converts the given map to a JSON object. * - * @param map Map to be converted + * @param list List of values to be converted * * @return a JSON-encoded map */ - public static Object toJsonMap(Map map) { + public static Object toJsonMap(List list) { JSONObject json = new JSONObject(); - for (Object k : map.keySet()) - json.put(k, map.get(k)); + for (Object item : list) { + if (item instanceof ValueTuple) { + ValueTuple vt = (ValueTuple)item; + json.put(vt.type, vt.value.toString()); + } else + json.put(ValueType.VALUE.toString(), item.toString()); + } return json.toString(); }