Revision: 7606
Author: [email protected]
Date: Wed Feb 24 03:52:35 2010
Log: Make java.util.Collections.Empty{List,Map,Set} work with GWT RPC.
http://code.google.com/p/google-web-toolkit/source/detail?r=7606
Added:
/trunk/user/src/com/google/gwt/user/client/rpc/core/java/util/Collections.java
Modified:
/trunk/user/super/com/google/gwt/emul/java/util/Collections.java
/trunk/user/test/com/google/gwt/user/client/rpc/CollectionsTest.java
/trunk/user/test/com/google/gwt/user/client/rpc/CollectionsTestService.java
/trunk/user/test/com/google/gwt/user/client/rpc/CollectionsTestServiceAsync.java
/trunk/user/test/com/google/gwt/user/client/rpc/TestSetFactory.java
/trunk/user/test/com/google/gwt/user/client/rpc/TestSetValidator.java
/trunk/user/test/com/google/gwt/user/server/rpc/CollectionsTestServiceImpl.java
=======================================
--- /dev/null
+++
/trunk/user/src/com/google/gwt/user/client/rpc/core/java/util/Collections.java
Wed Feb 24 03:52:35 2010
@@ -0,0 +1,102 @@
+/*
+ * Copyright 2010 Google Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may
not
+ * use this file except in compliance with the License. You may obtain a
copy of
+ * the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations
under
+ * the License.
+ */
+package com.google.gwt.user.client.rpc.core.java.util;
+
+import com.google.gwt.user.client.rpc.SerializationException;
+import com.google.gwt.user.client.rpc.SerializationStreamReader;
+import com.google.gwt.user.client.rpc.SerializationStreamWriter;
+
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+/**
+ * Dummy class for nesting the custom serializer.
+ */
+public final class Collections {
+
+ /**
+ * Custom field serializer for {...@link java.util.Collections$EmptyList}.
+ */
+ public static final class EmptyList_CustomFieldSerializer {
+
+ @SuppressWarnings({"unused", "unchecked"})
+ public static void deserialize(SerializationStreamReader streamReader,
+ List instance) throws SerializationException {
+ // Handled in instantiate.
+ }
+
+ @SuppressWarnings({"unused", "unchecked"})
+ public static List instantiate(SerializationStreamReader streamReader)
+ throws SerializationException {
+ return java.util.Collections.emptyList();
+ }
+
+ @SuppressWarnings({"unused", "unchecked"})
+ public static void serialize(SerializationStreamWriter streamWriter,
+ List instance) throws SerializationException {
+ // Nothing to serialize -- instantiate always returns the same thing
+ }
+ }
+
+ /**
+ * Custom field serializer for {...@link java.util.Collections$EmptyMap}.
+ */
+ public static final class EmptyMap_CustomFieldSerializer {
+
+ @SuppressWarnings({"unused", "unchecked"})
+ public static void deserialize(SerializationStreamReader streamReader,
+ Map instance) throws SerializationException {
+ // Handled in instantiate.
+ }
+
+ @SuppressWarnings({"unused", "unchecked"})
+ public static Map instantiate(SerializationStreamReader streamReader)
+ throws SerializationException {
+ return java.util.Collections.emptyMap();
+ }
+
+ @SuppressWarnings({"unused", "unchecked"})
+ public static void serialize(SerializationStreamWriter streamWriter,
+ Map instance) throws SerializationException {
+ // Nothing to serialize -- instantiate always returns the same thing
+ }
+ }
+
+ /**
+ * Custom field serializer for {...@link java.util.Collections$EmptySet}.
+ */
+ public static final class EmptySet_CustomFieldSerializer {
+
+ @SuppressWarnings({"unused", "unchecked"})
+ public static void deserialize(SerializationStreamReader streamReader,
+ Set instance) throws SerializationException {
+ // Handled in instantiate.
+ }
+
+ @SuppressWarnings({"unused", "unchecked"})
+ public static Set instantiate(SerializationStreamReader streamReader)
+ throws SerializationException {
+ return java.util.Collections.emptySet();
+ }
+
+ @SuppressWarnings({"unused", "unchecked"})
+ public static void serialize(SerializationStreamWriter streamWriter,
+ Set instance) throws SerializationException {
+ // Nothing to serialize -- instantiate always returns the same thing
+ }
+ }
+}
=======================================
--- /trunk/user/super/com/google/gwt/emul/java/util/Collections.java Thu
Dec 17 20:58:44 2009
+++ /trunk/user/super/com/google/gwt/emul/java/util/Collections.java Wed
Feb 24 03:52:35 2010
@@ -15,12 +15,101 @@
*/
package java.util;
+import java.io.Serializable;
+
/**
* Utility methods that operate on collections. <a
*
href="http://java.sun.com/j2se/1.5.0/docs/api/java/util/Collections.html">[Sun
* docs]</a>
*/
public class Collections {
+
+ private static final class EmptyList extends AbstractList implements
+ RandomAccess, Serializable {
+ @Override
+ public boolean contains(Object object) {
+ return false;
+ }
+
+ @Override
+ public Object get(int location) {
+ throw new IndexOutOfBoundsException();
+ }
+
+ @Override
+ public int size() {
+ return 0;
+ }
+ }
+
+ private static final class EmptySet extends AbstractSet implements
+ Serializable {
+ @Override
+ public boolean contains(Object object) {
+ return false;
+ }
+
+ @Override
+ public Iterator iterator() {
+ return new Iterator() {
+ public boolean hasNext() {
+ return false;
+ }
+
+ public Object next() {
+ throw new NoSuchElementException();
+ }
+
+ public void remove() {
+ throw new UnsupportedOperationException();
+ }
+ };
+ }
+
+ @Override
+ public int size() {
+ return 0;
+ }
+}
+
+ private static final class EmptyMap extends AbstractMap implements
+ Serializable {
+ @Override
+ public boolean containsKey(Object key) {
+ return false;
+ }
+
+ @Override
+ public boolean containsValue(Object value) {
+ return false;
+ }
+
+ @Override
+ public Set entrySet() {
+ return EMPTY_SET;
+ }
+
+ @Override
+ public Object get(Object key) {
+ return null;
+ }
+
+ @Override
+ public Set keySet() {
+ return EMPTY_SET;
+ }
+
+ @Override
+ public int size() {
+ return 0;
+ }
+
+ @Override
+ public Collection values() {
+ return EMPTY_LIST;
+ }
+ }
+
/*
* TODO: make the unmodifiable collections serializable.
*/
@@ -510,13 +599,13 @@
}
@SuppressWarnings("unchecked")
- public static final List EMPTY_LIST = unmodifiableList(new ArrayList());
+ public static final List EMPTY_LIST = new EmptyList();
@SuppressWarnings("unchecked")
- public static final Map EMPTY_MAP = unmodifiableMap(new HashMap());
+ public static final Map EMPTY_MAP = new EmptyMap();
@SuppressWarnings("unchecked")
- public static final Set EMPTY_SET = unmodifiableSet(new HashSet());
+ public static final Set EMPTY_SET = new EmptySet();
private static Comparator<Comparable<Object>> reverseComparator = new
Comparator<Comparable<Object>>() {
public int compare(Comparable<Object> o1, Comparable<Object> o2) {
=======================================
--- /trunk/user/test/com/google/gwt/user/client/rpc/CollectionsTest.java
Wed Nov 4 13:03:23 2009
+++ /trunk/user/test/com/google/gwt/user/client/rpc/CollectionsTest.java
Wed Feb 24 03:52:35 2010
@@ -18,6 +18,7 @@
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.TestSetFactory.MarkerTypeArrayList;
import
com.google.gwt.user.client.rpc.TestSetFactory.MarkerTypeArraysAsList;
+import com.google.gwt.user.client.rpc.TestSetFactory.MarkerTypeEmpty;
import com.google.gwt.user.client.rpc.TestSetFactory.MarkerTypeHashMap;
import com.google.gwt.user.client.rpc.TestSetFactory.MarkerTypeHashSet;
import
com.google.gwt.user.client.rpc.TestSetFactory.MarkerTypeLinkedHashMap;
@@ -34,6 +35,8 @@
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
+import java.util.Map;
+import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.Vector;
@@ -190,6 +193,57 @@
}
});
}
+
+ public void testEmptyList() {
+ CollectionsTestServiceAsync service = getServiceAsync();
+ delayTestFinishForRpc();
+ service.echo(TestSetFactory.createEmptyList(),
+ new AsyncCallback<List<MarkerTypeEmpty>>() {
+ public void onFailure(Throwable caught) {
+ TestSetValidator.rethrowException(caught);
+ }
+
+ public void onSuccess(List<MarkerTypeEmpty> result) {
+ assertNotNull(result);
+ assertTrue(TestSetValidator.isValid(result));
+ finishTest();
+ }
+ });
+ }
+
+ public void testEmptyMap() {
+ CollectionsTestServiceAsync service = getServiceAsync();
+ delayTestFinishForRpc();
+ service.echo(TestSetFactory.createEmptyMap(),
+ new AsyncCallback<Map<MarkerTypeEmpty, MarkerTypeEmpty>>() {
+ public void onFailure(Throwable caught) {
+ TestSetValidator.rethrowException(caught);
+ }
+
+ public void onSuccess(Map<MarkerTypeEmpty, MarkerTypeEmpty>
result) {
+ assertNotNull(result);
+ assertTrue(TestSetValidator.isValid(result));
+ finishTest();
+ }
+ });
+ }
+
+ public void testEmptySet() {
+ CollectionsTestServiceAsync service = getServiceAsync();
+ delayTestFinishForRpc();
+ service.echo(TestSetFactory.createEmptySet(),
+ new AsyncCallback<Set<MarkerTypeEmpty>>() {
+ public void onFailure(Throwable caught) {
+ TestSetValidator.rethrowException(caught);
+ }
+
+ public void onSuccess(Set<MarkerTypeEmpty> result) {
+ assertNotNull(result);
+ assertTrue(TestSetValidator.isValid(result));
+ finishTest();
+ }
+ });
+ }
public void testEnumArray() {
CollectionsTestServiceAsync service = getServiceAsync();
=======================================
---
/trunk/user/test/com/google/gwt/user/client/rpc/CollectionsTestService.java
Mon Jul 6 16:17:17 2009
+++
/trunk/user/test/com/google/gwt/user/client/rpc/CollectionsTestService.java
Wed Feb 24 03:52:35 2010
@@ -17,6 +17,7 @@
import com.google.gwt.user.client.rpc.TestSetFactory.MarkerTypeArrayList;
import
com.google.gwt.user.client.rpc.TestSetFactory.MarkerTypeArraysAsList;
+import com.google.gwt.user.client.rpc.TestSetFactory.MarkerTypeEmpty;
import com.google.gwt.user.client.rpc.TestSetFactory.MarkerTypeHashMap;
import com.google.gwt.user.client.rpc.TestSetFactory.MarkerTypeHashSet;
import
com.google.gwt.user.client.rpc.TestSetFactory.MarkerTypeLinkedHashMap;
@@ -34,6 +35,8 @@
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
+import java.util.Map;
+import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.Vector;
@@ -58,6 +61,18 @@
ArrayList<MarkerTypeArrayList> echo(ArrayList<MarkerTypeArrayList> value)
throws CollectionsTestServiceException;
+ // For Collections.emptyList()
+ List<MarkerTypeEmpty> echo(List<MarkerTypeEmpty> value)
+ throws CollectionsTestServiceException;
+
+ // For Collections.emptyMap()
+ Map<MarkerTypeEmpty, MarkerTypeEmpty> echo(Map<MarkerTypeEmpty,
+ MarkerTypeEmpty> value) throws CollectionsTestServiceException;
+
+ // For Collections.emptySet()
+ Set<MarkerTypeEmpty> echo(Set<MarkerTypeEmpty> value)
+ throws CollectionsTestServiceException;
+
boolean[] echo(boolean[] value) throws CollectionsTestServiceException;
Boolean[] echo(Boolean[] value) throws CollectionsTestServiceException;
=======================================
---
/trunk/user/test/com/google/gwt/user/client/rpc/CollectionsTestServiceAsync.java
Mon Jul 6 16:17:17 2009
+++
/trunk/user/test/com/google/gwt/user/client/rpc/CollectionsTestServiceAsync.java
Wed Feb 24 03:52:35 2010
@@ -17,6 +17,7 @@
import com.google.gwt.user.client.rpc.TestSetFactory.MarkerTypeArrayList;
import
com.google.gwt.user.client.rpc.TestSetFactory.MarkerTypeArraysAsList;
+import com.google.gwt.user.client.rpc.TestSetFactory.MarkerTypeEmpty;
import com.google.gwt.user.client.rpc.TestSetFactory.MarkerTypeHashMap;
import com.google.gwt.user.client.rpc.TestSetFactory.MarkerTypeHashSet;
import
com.google.gwt.user.client.rpc.TestSetFactory.MarkerTypeLinkedHashMap;
@@ -34,6 +35,8 @@
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
+import java.util.Map;
+import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.Vector;
@@ -45,6 +48,18 @@
void echo(ArrayList<MarkerTypeArrayList> value,
AsyncCallback<ArrayList<MarkerTypeArrayList>> callback);
+ // For Collections.emptyList()
+ void echo(List<MarkerTypeEmpty> value,
+ AsyncCallback<List<MarkerTypeEmpty>> callback);
+
+ // For Collections.emptyMap()
+ void echo(Map<MarkerTypeEmpty, MarkerTypeEmpty> value,
+ AsyncCallback<Map<MarkerTypeEmpty, MarkerTypeEmpty>> callback);
+
+ // For Collections.emptySet()
+ void echo(Set<MarkerTypeEmpty> value,
+ AsyncCallback<Set<MarkerTypeEmpty>> callback);
+
void echo(boolean[] value, AsyncCallback<boolean[]> callback);
void echo(Boolean[] value, AsyncCallback<Boolean[]> callback);
=======================================
--- /trunk/user/test/com/google/gwt/user/client/rpc/TestSetFactory.java Tue
Nov 3 08:52:16 2009
+++ /trunk/user/test/com/google/gwt/user/client/rpc/TestSetFactory.java Wed
Feb 24 03:52:35 2010
@@ -27,6 +27,8 @@
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
+import java.util.Map;
+import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.Vector;
@@ -100,6 +102,16 @@
super(null);
}
}
+
+ /**
+ * A single-use marker type to independently check type parameter
exposure in
+ * various empty collections.
+ */
+ public static final class MarkerTypeEmpty extends MarkerBase {
+ MarkerTypeEmpty() {
+ super(null);
+ }
+ }
/**
* A single-use marker type to independently check type parameter
exposure in
@@ -393,6 +405,18 @@
new Double(Double.MAX_VALUE), new Double(Double.MIN_VALUE),
new Double(Double.MAX_VALUE), new Double(Double.MIN_VALUE)};
}
+
+ public static List<MarkerTypeEmpty> createEmptyList() {
+ return java.util.Collections.emptyList();
+ }
+
+ public static Map<MarkerTypeEmpty, MarkerTypeEmpty> createEmptyMap() {
+ return java.util.Collections.emptyMap();
+ }
+
+ public static Set<MarkerTypeEmpty> createEmptySet() {
+ return java.util.Collections.emptySet();
+ }
public static Enum<?>[] createEnumArray() {
return new Enum<?>[] {
=======================================
--- /trunk/user/test/com/google/gwt/user/client/rpc/TestSetValidator.java
Tue Nov 3 13:04:24 2009
+++ /trunk/user/test/com/google/gwt/user/client/rpc/TestSetValidator.java
Wed Feb 24 03:52:35 2010
@@ -15,6 +15,7 @@
*/
package com.google.gwt.user.client.rpc;
+import com.google.gwt.user.client.rpc.TestSetFactory.MarkerTypeEmpty;
import com.google.gwt.user.client.rpc.TestSetFactory.MarkerTypeTreeMap;
import com.google.gwt.user.client.rpc.TestSetFactory.MarkerTypeTreeSet;
import
com.google.gwt.user.client.rpc.TestSetFactory.SerializableDoublyLinkedNode;
@@ -247,6 +248,18 @@
return reference.equals(list);
}
+
+ public static boolean isValid(List<MarkerTypeEmpty> list) {
+ return list != null && list.size() == 0;
+ }
+
+ public static boolean isValid(Map<MarkerTypeEmpty, MarkerTypeEmpty> map)
{
+ return map != null && map.size() == 0;
+ }
+
+ public static boolean isValid(Set<MarkerTypeEmpty> set) {
+ return set != null && set.size() == 0;
+ }
public static boolean isValid(HashMap<?, ?> expected, HashMap<?, ?> map)
{
if (map == null) {
=======================================
---
/trunk/user/test/com/google/gwt/user/server/rpc/CollectionsTestServiceImpl.java
Mon Jul 6 16:17:17 2009
+++
/trunk/user/test/com/google/gwt/user/server/rpc/CollectionsTestServiceImpl.java
Wed Feb 24 03:52:35 2010
@@ -20,6 +20,7 @@
import com.google.gwt.user.client.rpc.TestSetValidator;
import com.google.gwt.user.client.rpc.TestSetFactory.MarkerTypeArrayList;
import
com.google.gwt.user.client.rpc.TestSetFactory.MarkerTypeArraysAsList;
+import com.google.gwt.user.client.rpc.TestSetFactory.MarkerTypeEmpty;
import com.google.gwt.user.client.rpc.TestSetFactory.MarkerTypeHashMap;
import com.google.gwt.user.client.rpc.TestSetFactory.MarkerTypeHashSet;
import
com.google.gwt.user.client.rpc.TestSetFactory.MarkerTypeLinkedHashMap;
@@ -38,6 +39,8 @@
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
+import java.util.Map;
+import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.Vector;
@@ -266,6 +269,15 @@
}
return actual;
}
+
+ public List<MarkerTypeEmpty> echo(List<MarkerTypeEmpty> list)
+ throws CollectionsTestServiceException {
+ if (!TestSetValidator.isValid(list)) {
+ throw new CollectionsTestServiceException();
+ }
+
+ return list;
+ }
public long[] echo(long[] actual) throws CollectionsTestServiceException
{
long[] expected = TestSetFactory.createPrimitiveLongArray();
@@ -286,6 +298,25 @@
return actual;
}
+
+ public Map<MarkerTypeEmpty, MarkerTypeEmpty> echo(
+ Map<MarkerTypeEmpty, MarkerTypeEmpty> map)
+ throws CollectionsTestServiceException {
+ if (!TestSetValidator.isValid(map)) {
+ throw new CollectionsTestServiceException();
+ }
+
+ return map;
+ }
+
+ public Set<MarkerTypeEmpty> echo(Set<MarkerTypeEmpty> set)
+ throws CollectionsTestServiceException {
+ if (!TestSetValidator.isValid(set)) {
+ throw new CollectionsTestServiceException();
+ }
+
+ return set;
+ }
public short[] echo(short[] actual) throws
CollectionsTestServiceException {
short[] expected = TestSetFactory.createPrimitiveShortArray();
--
http://groups.google.com/group/Google-Web-Toolkit-Contributors