[mapguide-commits] r7222 - sandbox/jng/swig-java/Web/src/JavaApiEx

svn_mapguide at osgeo.org svn_mapguide at osgeo.org
Fri Nov 23 07:55:15 PST 2012


Author: jng
Date: 2012-11-23 07:55:14 -0800 (Fri, 23 Nov 2012)
New Revision: 7222

Modified:
   sandbox/jng/swig-java/Web/src/JavaApiEx/DIFFERENCES.txt
   sandbox/jng/swig-java/Web/src/JavaApiEx/javaextensions.i
Log:
#9: Add Iterable support for MgReadOnlyLayerCollection

Modified: sandbox/jng/swig-java/Web/src/JavaApiEx/DIFFERENCES.txt
===================================================================
--- sandbox/jng/swig-java/Web/src/JavaApiEx/DIFFERENCES.txt	2012-11-23 14:29:55 UTC (rev 7221)
+++ sandbox/jng/swig-java/Web/src/JavaApiEx/DIFFERENCES.txt	2012-11-23 15:55:14 UTC (rev 7222)
@@ -32,9 +32,13 @@
  - MgFeatureSchemaCollection (T is MgFeatureSchema)
  - MgPropertyCollection (T is MgProperty)
  - MgStringCollection (T is String)
+ 
+4. The following classes now implement java.util.Iterable<T> allowing them to be used in an enhanced for-loop
 
-4. To avoid naming conflicts with SWIG generated code and methods from inherited java classes or interfaces as a result of the above changes, the following class methods have been renamed in the Java MapGuide API:
+ - MgReadOnlyLayerCollection (T is MgLayerBase)
 
+5. To avoid naming conflicts with SWIG generated code and methods from inherited java classes or interfaces as a result of the above changes, the following class methods have been renamed in the Java MapGuide API:
+
  - MgPropertyDefinition.Delete      is now MgPropertyDefinition.markAsDeleted
  - MgClassDefinition.Delete         is now MgClassDefinition.markAsDeleted
  - MgFeatureSchema.Delete           is now MgFeatureSchema.markAsDeleted

Modified: sandbox/jng/swig-java/Web/src/JavaApiEx/javaextensions.i
===================================================================
--- sandbox/jng/swig-java/Web/src/JavaApiEx/javaextensions.i	2012-11-23 14:29:55 UTC (rev 7221)
+++ sandbox/jng/swig-java/Web/src/JavaApiEx/javaextensions.i	2012-11-23 15:55:14 UTC (rev 7222)
@@ -53,6 +53,9 @@
 //that needs one
 //
 //Kinda like C++ templates for Java without the suck. And we all know how much Java generics suck :D
+//
+//TODO: Not sure if a SWIG macro can call into another SWIG macro, but if it can it should call into
+//the IMPLEMENT_MG_JAVA_ITERABLE_API
 %define IMPLEMENT_MG_JAVA_COLLECTION_API(collection_type, item_type)
 //Necessary imports
 %typemap(javaimports) collection_type %{
@@ -90,6 +93,8 @@
             throw new UnsupportedOperationException();
         }
     }
+    
+    public Iterator<item_type> iterator() { return new ItemIterator(this); }
 
     public boolean add(item_type item) {
         this.addItem(item);
@@ -122,8 +127,6 @@
 
     public boolean isEmpty() { return this.getCount() == 0; }
 
-    public Iterator<item_type> iterator() { return new ItemIterator(this); }
-
     public boolean remove(Object o) {
         if (o instanceof item_type) {
             return this.remove((item_type)o);
@@ -184,10 +187,53 @@
 %}
 %enddef
 
+//This helper macro implements the java.lang.Iterable API allowing the instance of the
+//java proxy to be used in an enhanced for loop (akin to IEnumerable/foreach in C#)
+%define IMPLEMENT_MG_JAVA_ITERABLE_API(collection_type, item_type)
+//Necessary imports
+%typemap(javaimports) collection_type %{
+import java.util.Iterator;
+import java.util.NoSuchElementException;
+%}
+//Java Interfaces implemented by the java proxy class
+%typemap(javainterfaces) collection_type "java.lang.Iterable<item_type>"
+//This is the java.lang.Iterable<T> implementation that is injected into each java proxy class
+%typemap(javacode) collection_type %{
+
+    class ItemIterator implements Iterator<item_type> {
+        private collection_type _collection;
+        private int _pos;
+        
+        public ItemIterator(collection_type c) { 
+            _collection = c; 
+            _pos = -1;
+        }
+        
+        public boolean hasNext() {
+            return _pos + 1 <= _collection.getCount();
+        }
+        
+        public item_type next() {
+            _pos++;
+            if (_pos >= _collection.getCount())
+                throw new NoSuchElementException();
+            return _collection.getItem(_pos);
+        }
+        
+        public void remove() {
+            throw new UnsupportedOperationException();
+        }
+    }
+    
+    public Iterator<item_type> iterator() { return new ItemIterator(this); }
+    
+%}
+%enddef
+
 //Plug the stock implementation for our MapGuide collection classes
 IMPLEMENT_MG_JAVA_COLLECTION_API(MgBatchPropertyCollection, MgPropertyCollection)
 IMPLEMENT_MG_JAVA_COLLECTION_API(MgClassDefinitionCollection, MgClassDefinition)
 IMPLEMENT_MG_JAVA_COLLECTION_API(MgFeatureSchemaCollection, MgFeatureSchema)
-//IMPLEMENT_MG_JAVA_COLLECTION_API(MgIntCollection, Integer)
 IMPLEMENT_MG_JAVA_COLLECTION_API(MgPropertyCollection, MgProperty)
-IMPLEMENT_MG_JAVA_COLLECTION_API(MgStringCollection, String)
\ No newline at end of file
+IMPLEMENT_MG_JAVA_COLLECTION_API(MgStringCollection, String)
+IMPLEMENT_MG_JAVA_ITERABLE_API(MgReadOnlyLayerCollection, MgLayerBase)
\ No newline at end of file



More information about the mapguide-commits mailing list