aboutsummaryrefslogtreecommitdiffstats
path: root/gwtquery-core
diff options
context:
space:
mode:
authorManolo Carrasco <manolo@apache.org>2010-05-29 07:32:55 +0000
committerManolo Carrasco <manolo@apache.org>2010-05-29 07:32:55 +0000
commit646f7d9f0a940fe23a9b0c986720536214e18e17 (patch)
tree8f905105dae582ae2d122f0b98b4966537e4bc2b /gwtquery-core
parent3e024f3cb9fad30904171029ea69cdbd7e273cb8 (diff)
downloadgwtquery-646f7d9f0a940fe23a9b0c986720536214e18e17.tar.gz
gwtquery-646f7d9f0a940fe23a9b0c986720536214e18e17.zip
use the same "unique()" method in SelectorEngine and GQuery, this eliminates the need of FastSet in favor of java.util.HashSet
Diffstat (limited to 'gwtquery-core')
-rw-r--r--gwtquery-core/src/main/java/com/google/gwt/query/client/GQuery.java52
-rw-r--r--gwtquery-core/src/main/java/com/google/gwt/query/client/impl/SelectorEngineImpl.java17
-rw-r--r--gwtquery-core/src/main/java/com/google/gwt/query/client/impl/SelectorEngineSizzle.java14
-rw-r--r--gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryCoreTest.java16
-rw-r--r--gwtquery-core/src/test/java/com/google/gwt/query/client/GQuerySelectorsTest.java18
5 files changed, 56 insertions, 61 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 fa339dbd..c310065a 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
@@ -20,8 +20,8 @@ import static com.google.gwt.query.client.Events.Events;
import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.JavaScriptObject;
+import com.google.gwt.core.client.JsArray;
import com.google.gwt.core.client.JsArrayString;
-import com.google.gwt.dom.client.BodyElement;
import com.google.gwt.dom.client.ButtonElement;
import com.google.gwt.dom.client.Document;
import com.google.gwt.dom.client.Element;
@@ -39,6 +39,7 @@ import com.google.gwt.query.client.css.Percentage;
import com.google.gwt.query.client.css.TakesLength;
import com.google.gwt.query.client.css.TakesPercentage;
import com.google.gwt.query.client.impl.DocumentStyleImpl;
+import com.google.gwt.query.client.impl.SelectorEngineImpl;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.Window;
@@ -135,41 +136,7 @@ public class GQuery implements Lazy<GQuery, LazyGQuery> {
}-*/;
}
- private static final class FastSet extends JavaScriptObject {
-
- public static FastSet create() {
- return JavaScriptObject.createObject().cast();
- }
-
- protected FastSet() {
- }
-
- public void add(Object o) {
- add0(o.hashCode());
- }
-
- public boolean contains(Object o) {
- return contains0(o.hashCode());
- }
-
- public void remove(Object o) {
- remove0(o.hashCode());
- }
-
- private native void add0(int hc) /*-{
- this[hc]=true;
- }-*/;
-
- private native boolean contains0(int hc) /*-{
- return this[hc] || false;
- }-*/;
-
- private native void remove0(int hc) /*-{
- delete this[hc];
- }-*/;
- }
-
- private static final class Queue<T> extends JavaScriptObject {
+ public static final class Queue<T> extends JavaScriptObject {
public static Queue newInstance() {
return createArray().cast();
@@ -2029,16 +1996,7 @@ public class GQuery implements Lazy<GQuery, LazyGQuery> {
* only works on arrays of DOM elements, not strings or numbers.
*/
public JSArray unique(JSArray result) {
- FastSet f = FastSet.create();
- JSArray ret = JSArray.create();
- for (int i = 0; i < result.getLength(); i++) {
- Element e = result.getElement(i);
- if (!f.contains(e)) {
- f.add(e);
- ret.addNode(e);
- }
- }
- return ret;
+ return SelectorEngineImpl.unique(result.<JsArray<Element>>cast()).cast();
}
/**
@@ -2396,7 +2354,7 @@ public class GQuery implements Lazy<GQuery, LazyGQuery> {
return n.getChildNodes().cast();
}
- private <S> Object data(Element item, String name, S value) {
+ protected <S> Object data(Element item, String name, S value) {
if (dataCache == null) {
windowData = JavaScriptObject.createObject().cast();
dataCache = JavaScriptObject.createObject().cast();
diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/impl/SelectorEngineImpl.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/impl/SelectorEngineImpl.java
index 49ca99ce..4e5a1a71 100644
--- a/gwtquery-core/src/main/java/com/google/gwt/query/client/impl/SelectorEngineImpl.java
+++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/impl/SelectorEngineImpl.java
@@ -15,6 +15,10 @@
*/
package com.google.gwt.query.client.impl;
+import java.util.HashSet;
+
+import com.google.gwt.core.client.JavaScriptObject;
+import com.google.gwt.core.client.JsArray;
import com.google.gwt.dom.client.Element;
import com.google.gwt.dom.client.Node;
import com.google.gwt.dom.client.NodeList;
@@ -131,4 +135,17 @@ public abstract class SelectorEngineImpl {
* @return a list of matched nodes
*/
public abstract NodeList<Element> select(String selector, Node ctx);
+
+ public static JsArray<Element> unique(JsArray<Element> a) {
+ JsArray<Element> ret = JavaScriptObject.createArray().cast();
+ HashSet<Integer> f = new HashSet<Integer>();
+ for (int i = 0; i < a.length(); i++) {
+ Element e = a.get(i);
+ if (!f.contains(e.hashCode())) {
+ f.add(e.hashCode());
+ ret.push(e);
+ }
+ }
+ return ret;
+ }
}
diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/impl/SelectorEngineSizzle.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/impl/SelectorEngineSizzle.java
index 3149f3be..57af8277 100644
--- a/gwtquery-core/src/main/java/com/google/gwt/query/client/impl/SelectorEngineSizzle.java
+++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/impl/SelectorEngineSizzle.java
@@ -15,7 +15,6 @@
*/
package com.google.gwt.query.client.impl;
-import java.util.HashSet;
import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.core.client.JsArray;
@@ -630,19 +629,6 @@ public class SelectorEngineSizzle extends SelectorEngineImpl {
return @com.google.gwt.query.client.impl.SelectorEngineSizzle::filter(Ljava/lang/String;Lcom/google/gwt/core/client/JsArray;ZLjava/lang/Object;)( later, tmpSet, false );
}-*/;
- public static JsArray<Element> unique(JsArray<Element> a) {
- JsArray<Element> ret = JavaScriptObject.createArray().cast();
- HashSet<Integer> f = new HashSet<Integer>();
- for (int i = 0; i < a.length(); i++) {
- Element e = a.get(i);
- if (!f.contains(e.hashCode())) {
- f.add(e.hashCode());
- ret.push(e);
- }
- }
- return ret;
- }
-
private static native JsArray<Element> select(String selector, Node context, JsArray<Element> results, JsArray<Element> seed) /*-{
results = results || [];
var origContext = context = context || document;
diff --git a/gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryCoreTest.java b/gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryCoreTest.java
index 4e275a85..82ec7694 100644
--- a/gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryCoreTest.java
+++ b/gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryCoreTest.java
@@ -20,6 +20,8 @@ import static com.google.gwt.query.client.GQuery.$$;
import com.google.gwt.dom.client.Element;
import com.google.gwt.junit.client.GWTTestCase;
+import com.google.gwt.query.client.impl.SelectorEngineImpl;
+import com.google.gwt.query.client.impl.SelectorEngineSizzle;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.RootPanel;
@@ -556,6 +558,20 @@ public class GQueryCoreTest extends GWTTestCase {
assertEquals(2, $("p", e).slice(0, -1).size());
assertEquals(0, $("p", e).slice(3, 2).size());
}
+
+ public void testUnique() {
+ SelectorEngineImpl selSizz = new SelectorEngineSizzle();
+ GQuery g = $(e).html("<div><p></p><p></p><span></span><p></p>");
+ JSArray a;
+ a = selSizz.select("p", e).cast();
+ assertEquals(3, a.getLength());
+ a.addNode(a.getNode(0));
+ a.addNode(a.getNode(3));
+ assertEquals(5 , a.getLength());
+ a = g.unique(a);
+ assertEquals(3, a.getLength());
+ }
+
public void testWrapMethod() {
String content = "<p>Test Paragraph.</p>";
String wrapper = "<div id=\"content\">Content</div>";
diff --git a/gwtquery-core/src/test/java/com/google/gwt/query/client/GQuerySelectorsTest.java b/gwtquery-core/src/test/java/com/google/gwt/query/client/GQuerySelectorsTest.java
index 17182d0c..057679e5 100644
--- a/gwtquery-core/src/test/java/com/google/gwt/query/client/GQuerySelectorsTest.java
+++ b/gwtquery-core/src/test/java/com/google/gwt/query/client/GQuerySelectorsTest.java
@@ -18,6 +18,7 @@ package com.google.gwt.query.client;
import static com.google.gwt.query.client.GQuery.$;
import com.google.gwt.core.client.GWT;
+import com.google.gwt.core.client.JsArray;
import com.google.gwt.dom.client.Document;
import com.google.gwt.dom.client.Element;
import com.google.gwt.dom.client.Node;
@@ -269,6 +270,23 @@ public class GQuerySelectorsTest extends GWTTestCase {
assertEquals("branchB target", selectors.target().text());
}
+ public void testUnique() {
+ SelectorEngineImpl selSizz = new SelectorEngineSizzle();
+ $(e).html(getTestContent());
+
+ JsArray<Element> a;
+ a = selSizz.select("p", e).cast();
+ int n = a.length();
+ assertTrue(n > 300);
+ for (int i=0; i<n; i++) {
+ a.push(a.get(i));
+ }
+ assertEquals(n * 2 , a.length());
+ a = SelectorEngineImpl.unique(a);
+ assertEquals(n, a.length());
+ }
+
+
private void assertArrayContains(Object result, Object... array) {
assertArrayContains("", result, array);
}