From 35e0203a6d65bed6b08f55471a28dfc6488d2067 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Manuel=20Carrasco=20Mo=C3=B1ino?= Date: Thu, 4 Jul 2013 11:25:11 +0200 Subject: [PATCH] Better usage of generics in data() methods. Add removed static data method. Add tests for data methods --- .../com/google/gwt/query/client/GQuery.java | 44 +++++++++++++------ .../google/gwt/query/client/js/JsCache.java | 24 +++++----- .../gwt/query/client/GQueryCoreTestGwt.java | 31 +++++++++++++ 3 files changed, 74 insertions(+), 25 deletions(-) diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/GQuery.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/GQuery.java index 52feaf49..cac907f1 100644 --- a/gwtquery-core/src/main/java/com/google/gwt/query/client/GQuery.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/GQuery.java @@ -404,6 +404,13 @@ public class GQuery implements Lazy { return $(Arrays.asList(widgets)); } + /** + * Create an empty JSON object. + */ + public static Properties $$() { + return $$(null); + } + /** * Wrap a JSON object. */ @@ -477,17 +484,25 @@ public class GQuery implements Lazy { } /** - * We store data in js object which has this structure: + * Get the element data matching the key. + */ + public static T data(Element e, String key) { + return data(e, key, null); + } + + /** + * Store arbitrary data associated with the specified element. * - * datacache [element_hash] [key] = value + * We store this data in a global js object having the structure: + * datacache [element.hashCode()] [key] = value * * @return the value stored in the element with the given name */ - protected static Object data(Element element, String key, S value) { - return data(element, key, value, Object.class); + public static T data(Element element, String key, T value) { + return data(element, key, value, null); } - private static Object data(Element element, String key, S value, Class clz) { + private static T data(Element element, String key, T value, Class clz) { if (dataCache == null) { windowData = JavaScriptObject.createObject().cast(); dataCache = JavaScriptObject.createObject().cast(); @@ -1656,25 +1671,26 @@ public class GQuery implements Lazy { } /** - * Returns value at named data store for the element, as set by data(name, value). + * Return the value at the named data store for the first element in the set of matched + * elements. */ - public Object data(String name) { - return isEmpty() ? null : data(get(0), name, null); + @SuppressWarnings("unchecked") + public T data(String name) { + return isEmpty() ? null : (T)data(get(0), name, null); } /** - * Returns value at named data store for the element, as set by data(name, value) with desired - * return type. + * Return the value at the named data store for the first element in the set of matched + * elements, as set by data(name, value), with desired return type. * * @param clz return type class literal */ - @SuppressWarnings("unchecked") - public T data(String name, Class clz) { - return isEmpty() ? null : (T) data(get(0), name, null, clz); + public T data(String name, Class clz) { + return isEmpty() ? null : data(get(0), name, null, clz); } /** - * Stores the value in the named spot with desired return type. + * Store arbitrary data associated with the matched elements in the named data store. */ public GQuery data(String name, Object value) { for (Element e : elements()) { diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/js/JsCache.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/js/JsCache.java index b9a69905..fa604fe8 100644 --- a/gwtquery-core/src/main/java/com/google/gwt/query/client/js/JsCache.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/js/JsCache.java @@ -63,17 +63,19 @@ public class JsCache extends JavaScriptObject { @SuppressWarnings("unchecked") public final T get(Object id, Class clz) { Object o = get(id); - if (o instanceof Double) { - Double d = (Double)o; - if (clz == Float.class) o = d.floatValue(); - else if (clz == Integer.class) o = d.intValue(); - else if (clz == Long.class) o = d.longValue(); - else if (clz == Short.class) o = d.shortValue(); - else if (clz == Byte.class) o = d.byteValue(); - } else if (clz == Boolean.class && !(o instanceof Boolean)) { - o = Boolean.valueOf(String.valueOf(o)); - } else if (clz == String.class && !(o instanceof String)) { - o = String.valueOf(o); + if (clz != null) { + if (o instanceof Double) { + Double d = (Double)o; + if (clz == Float.class) o = d.floatValue(); + else if (clz == Integer.class) o = d.intValue(); + else if (clz == Long.class) o = d.longValue(); + else if (clz == Short.class) o = d.shortValue(); + else if (clz == Byte.class) o = d.byteValue(); + } else if (clz == Boolean.class && !(o instanceof Boolean)) { + o = Boolean.valueOf(String.valueOf(o)); + } else if (clz == String.class && !(o instanceof String)) { + o = String.valueOf(o); + } } return (T)o; } diff --git a/gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryCoreTestGwt.java b/gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryCoreTestGwt.java index 61f0f7e5..20809f2a 100644 --- a/gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryCoreTestGwt.java +++ b/gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryCoreTestGwt.java @@ -1546,6 +1546,37 @@ public class GQueryCoreTestGwt extends GWTTestCase { assertEquals("red", $(parent).css(CSS.BACKGROUND_COLOR, false)); } + public void testData() { + + assertEquals(null, $().data("whatever")); + + Object o = $$(); + + GQuery g = $(e) + .data("number", 3.5) + .data("bool", true) + .data("string", "foo") + .data("object", o); + + double d = g.data("number"); + assertEquals(3.5d, d); + int i = g.data("number", Integer.class); + assertEquals(3, i); + long l = g.data("number", Long.class); + assertEquals(3l, l); + + boolean b = g.data("bool"); + assertTrue(b); + + String s = g.data("string"); + assertEquals("foo", s); + s = g.data("bool", String.class); + assertEquals("true", s); + + Element n = GQuery.data(e, "object"); + assertEquals(o, n); + } + public void testDetachMethod(){ String html = "
parent
child
"; $(e).html(html); -- 2.39.5