soc.2012.android: 257607fe: Added a String peer generator and GList ...

michael at soc.pidgin.im michael at soc.pidgin.im
Mon May 28 11:36:46 EDT 2012


----------------------------------------------------------------------
Revision: 257607fea4c29923037534e43b8a48acab44491e
Parent:   63a95e6b14caf6640ed95c06e144d41e87254adf
Author:   michael at soc.pidgin.im
Date:     05/28/12 11:29:36
Branch:   im.pidgin.soc.2012.android
URL: http://d.pidgin.im/viewmtn/revision/info/257607fea4c29923037534e43b8a48acab44491e

Changelog: 

Added a String peer generator and GList interface.

Changes against parent 63a95e6b14caf6640ed95c06e144d41e87254adf

  added    android/workspace/im.pidgin.libpurple/native/GListReference.c
  added    android/workspace/im.pidgin.libpurple/native/StringPeerGenerator.c
  added    android/workspace/im.pidgin.libpurple/src/im/pidgin/libpurple/glib/GHashTableIterator.java
  added    android/workspace/im.pidgin.libpurple/src/im/pidgin/libpurple/glib/GListReference.java
  added    android/workspace/im.pidgin.libpurple/src/im/pidgin/libpurple/peering/StringPeerGenerator.java
  patched  android/workspace/im.pidgin.libpurple/native/build.ant
  patched  android/workspace/im.pidgin.libpurple/src/im/pidgin/libpurple/glib/GList.java
  patched  android/workspace/im.pidgin.libpurple/src/im/pidgin/libpurple/glib/GListIterator.java

-------------- next part --------------
============================================================
--- android/workspace/im.pidgin.libpurple/native/build.ant	15c9020c6d45647c25cd3870e1ab2a5107011b03
+++ android/workspace/im.pidgin.libpurple/native/build.ant	27927631a7ff925c42a53d18d97b36fa4aa3a29f
@@ -50,9 +50,9 @@
 		<javah classdefinition="im.pidgin.libpurple.core.dns.DnsQueryResolvedCallback" to="${javah.out}" />
 
 		<javah classdefinition="im.pidgin.libpurple.glib.GHashTableReference" to="${javah.out}" />
-		<javah classdefinition="im.pidgin.libpurple.glib.GList" to="${javah.out}" />
-		<javah classdefinition="im.pidgin.libpurple.glib.GListIterator" to="${javah.out}" />
+		<javah classdefinition="im.pidgin.libpurple.glib.GListReference" to="${javah.out}" />
 		<javah classdefinition="im.pidgin.libpurple.glib.GSourceFunctionCall" to="${javah.out}" />
+		<javah classdefinition="im.pidgin.libpurple.peering.StringPeerGenerator" to="${javah.out}" />
 
 		<javah classdefinition="im.pidgin.libpurple.plugin.PurplePlugin" to="${javah.out}" />
 		<javah classdefinition="im.pidgin.libpurple.plugin.PurpleProtocolPlugin" to="${javah.out}" />
============================================================
--- android/workspace/im.pidgin.libpurple/src/im/pidgin/libpurple/glib/GList.java	fc3922a5ed3ee7d818c8beb4a907e35c3c4fbe21
+++ android/workspace/im.pidgin.libpurple/src/im/pidgin/libpurple/glib/GList.java	3990da3f76ed7cea08df68c031097c6018da5d1f
@@ -4,16 +4,21 @@ public class GList<E> extends AbstractSe
 import java.util.ListIterator;
 
 public class GList<E> extends AbstractSequentialList<E>{
+	
+	private final GListReference<E> reference;
+
+	protected GList(GListReference<E> reference) {
+		this.reference = reference;
+	}
+	
 	@Override
 	public ListIterator<E> listIterator(int location) {
-		// TODO Auto-generated method stub
-		return null;
+		return new GListIterator<E>(reference, location);
 	}
 
 	@Override
 	public int size() {
-		// TODO Auto-generated method stub
-		return 0;
+		return reference.length();
 	}
 
 }
============================================================
--- android/workspace/im.pidgin.libpurple/src/im/pidgin/libpurple/glib/GListIterator.java	ef963c908966e064d91c85358d1c0bb4267702cb
+++ android/workspace/im.pidgin.libpurple/src/im/pidgin/libpurple/glib/GListIterator.java	d6b75c557a534f4503f0fa9639ea209570c45e3e
@@ -1,68 +1,117 @@ import java.util.ListIterator;
 package im.pidgin.libpurple.glib;
 
 import java.util.ListIterator;
+import java.util.NoSuchElementException;
 
 /**
  * This is an iterator over a glist.
  * 
  * @author michaelz
  * 
- * @param <E> The list elemt type.
+ * @param <E> The list element type.
  */
 public class GListIterator<E> implements ListIterator<E> {
 
-	@Override
-	public void add(E arg0) {
-		// TODO Auto-generated method stub
+	private final GListReference<E> reference;
+	/**
+	 * A handle to the list item that should be used for a {@link #next()}-Operation.
+	 * <p>
+	 */
+	long nextGlistItem = 0;
+	/**
+	 * A handle to the list item that should be used for a {@link #previous()}-Operation.
+	 * <p>
+	 * 0: no previous item
+	 */
+	long previousGlistItem = 0;
 
+	/**
+	 * The index of the item that is returned in a {@link #next()} Operation.
+	 */
+	int nextIndex;
+
+
+	public GListIterator(GListReference<E> reference, int location) {
+		this.reference = reference;
+		if (location < 0) {
+			throw new IllegalArgumentException(
+			        "location cannot be smaller than 0");
+		}
+
+		if (location == 0) {
+			nextGlistItem = reference.getItemHandle(location);
+			previousGlistItem = 0;
+		} else {
+			previousGlistItem = reference.getItemHandle(location - 1);
+			if (previousGlistItem == 0) {
+				throw new IllegalArgumentException(
+				        "location is too big for this list.");
+			}
+			nextGlistItem = reference.next(previousGlistItem);
+		}
+		nextIndex = location;
 	}
 
 	@Override
+	public void add(E object) {
+		throw new UnsupportedOperationException();
+	}
+
+	@Override
 	public boolean hasNext() {
-		// TODO Auto-generated method stub
-		return false;
+		return nextGlistItem != 0;
 	}
 
 	@Override
 	public boolean hasPrevious() {
-		// TODO Auto-generated method stub
-		return false;
+		return previousGlistItem != 0;
 	}
 
 	@Override
 	public E next() {
-		// TODO Auto-generated method stub
-		return null;
+		if (nextGlistItem == 0) {
+			throw new NoSuchElementException();
+		}
+
+		
+		E item = reference.getData(nextGlistItem);
+		previousGlistItem = nextGlistItem;
+		nextGlistItem = reference.next(previousGlistItem);
+		nextIndex++;
+		return item;
 	}
 
 	@Override
 	public int nextIndex() {
-		// TODO Auto-generated method stub
-		return 0;
+		return nextIndex;
 	}
 
 	@Override
 	public E previous() {
-		// TODO Auto-generated method stub
-		return null;
+		if (previousGlistItem == 0) {
+			throw new NoSuchElementException();
+		}
+
+		E item = reference.getData(previousGlistItem);
+		nextGlistItem = previousGlistItem;
+		previousGlistItem = reference.previous(nextGlistItem);
+		nextIndex--;
+		return item;
 	}
 
 	@Override
 	public int previousIndex() {
-		// TODO Auto-generated method stub
-		return 0;
+		return nextIndex - 1;
 	}
 
 	@Override
 	public void remove() {
-		// TODO Auto-generated method stub
-
+		throw new UnsupportedOperationException();
 	}
 
 	@Override
-	public void set(E arg0) {
-		// TODO Auto-generated method stub
-
+	public void set(E object) {
+		throw new UnsupportedOperationException();
 	}
 
 }
============================================================
--- /dev/null	
+++ android/workspace/im.pidgin.libpurple/native/GListReference.c	6c636f038764194106f52ac508f01446de2a8b65
@@ -0,0 +1,53 @@
+/* DO NOT EDIT THIS FILE - it is machine generated */
+#include <jni.h>
+/* Header for class im_pidgin_libpurple_glib_GListReference */
+
+#ifndef _Included_im_pidgin_libpurple_glib_GListReference
+#define _Included_im_pidgin_libpurple_glib_GListReference
+#ifdef __cplusplus
+extern "C" {
+#endif
+/*
+ * Class:     im_pidgin_libpurple_glib_GListReference
+ * Method:    length_native
+ * Signature: ()I
+ */
+JNIEXPORT jint JNICALL Java_im_pidgin_libpurple_glib_GListReference_length_1native
+  (JNIEnv *, jobject);
+
+/*
+ * Class:     im_pidgin_libpurple_glib_GListReference
+ * Method:    nth_native
+ * Signature: (I)J
+ */
+JNIEXPORT jlong JNICALL Java_im_pidgin_libpurple_glib_GListReference_nth_1native
+  (JNIEnv *, jobject, jint);
+
+/*
+ * Class:     im_pidgin_libpurple_glib_GListReference
+ * Method:    data_native
+ * Signature: (J)J
+ */
+JNIEXPORT jlong JNICALL Java_im_pidgin_libpurple_glib_GListReference_data_1native
+  (JNIEnv *, jobject, jlong);
+
+/*
+ * Class:     im_pidgin_libpurple_glib_GListReference
+ * Method:    next_native
+ * Signature: (J)J
+ */
+JNIEXPORT jlong JNICALL Java_im_pidgin_libpurple_glib_GListReference_next_1native
+  (JNIEnv *, jobject, jlong);
+
+/*
+ * Class:     im_pidgin_libpurple_glib_GListReference
+ * Method:    previous_native
+ * Signature: (J)J
+ */
+JNIEXPORT jlong JNICALL Java_im_pidgin_libpurple_glib_GListReference_previous_1native
+  (JNIEnv *, jobject, jlong);
+
+#ifdef __cplusplus
+}
+#endif
+#endif
============================================================
--- /dev/null	
+++ android/workspace/im.pidgin.libpurple/native/StringPeerGenerator.c	67da2b6ad6db8fc91f0cca0ba144cfd891d27504
@@ -0,0 +1,33 @@
+
+#include "StringPeerGenerator.h"
+#include "helpers.h"
+
+/*
+ * Class:     im_pidgin_libpurple_peering_StringPeerGenerator
+ * Method:    toString_native
+ * Signature: (J)Ljava/lang/String;
+ */
+JNIEXPORT jstring JNICALL Java_im_pidgin_libpurple_peering_StringPeerGenerator_toString_1native
+  (JNIEnv *env, jobject obj, jlong ptr) {
+	return (*env)->NewStringUTF(env, (const char*) longToP(ptr));
+}
+
+/*
+ * Class:     im_pidgin_libpurple_peering_StringPeerGenerator
+ * Method:    fromString_native
+ * Signature: (Ljava/lang/String;)J
+ */
+JNIEXPORT jlong JNICALL Java_im_pidgin_libpurple_peering_StringPeerGenerator_fromString_1native
+  (JNIEnv *env, jobject obj, jstring str) {
+	return pToLong((*env)->GetStringUTFChars(env, str, NULL));
+}
+
+/*
+ * Class:     im_pidgin_libpurple_peering_StringPeerGenerator
+ * Method:    free_native
+ * Signature: (Ljava/lang/String;Ljava/lang/Long;)V
+ */
+JNIEXPORT void JNICALL Java_im_pidgin_libpurple_peering_StringPeerGenerator_free_1native
+  (JNIEnv *env, jobject obj, jstring str, jlong ptr) {
+	(*env)->ReleaseStringUTFChars(env, str, (const char*) longToP(ptr));
+}
============================================================
--- /dev/null	
+++ android/workspace/im.pidgin.libpurple/src/im/pidgin/libpurple/glib/GHashTableIterator.java	5e164575a8dca7bff7f38a34e72fd73e759fd9ba
@@ -0,0 +1,49 @@
+package im.pidgin.libpurple.glib;
+
+import java.util.Iterator;
+import java.util.Map.Entry;
+
+public class GHashTableIterator<E> implements
+		Iterator<java.util.Map.Entry<java.lang.String, E>> {
+
+	private final GHashTableReference<E> reference;
+	private final long iterator;
+	private Entry<String, E> next;
+	private Entry<String, E> current;
+
+	public GHashTableIterator(GHashTableReference<E> reference) {
+		this.reference = reference;
+		iterator = reference.getIterator();
+		next = reference.next(iterator);
+	}
+
+	@Override
+	public boolean hasNext() {
+		return next != null;
+	}
+
+	@Override
+	public Entry<String, E> next() {
+		if (!hasNext()) {
+			throw new IllegalStateException("Iterator at end of table.");
+		}
+		current = next;
+		next = reference.next(iterator);
+		return current;
+	}
+
+	@Override
+	public void remove() {
+		if (current == null) {
+			throw new IllegalStateException("next() must be called before remove()");
+		}
+		reference.remove(current.getKey());
+		current = null;
+	}
+	
+	@Override
+	protected void finalize() throws Throwable {
+		reference.freeIterator(iterator);
+	}
+
+}
============================================================
--- /dev/null	
+++ android/workspace/im.pidgin.libpurple/src/im/pidgin/libpurple/glib/GListReference.java	d4cdf0ae2453fa327fe531e7ff8d8b7150927620
@@ -0,0 +1,82 @@
+package im.pidgin.libpurple.glib;
+
+import im.pidgin.libpurple.peering.PeerGenerator;
+import im.pidgin.libpurple.peering.Peered;
+
+public final class GListReference<E> extends Peered {
+
+	private final boolean modifyable;
+	private final PeerGenerator<E> peerGenerator;
+
+	public GListReference(long list, PeerGenerator<E> peerGenerator,
+			boolean modifyable) {
+		super(list);
+		this.peerGenerator = peerGenerator;
+		this.modifyable = modifyable;
+	}
+
+	protected int length() {
+		ensureNotDestroyed();
+		return length_native();
+	}
+
+	private native int length_native();
+
+	/**
+	 * Gets the item handle at the given position
+	 * 
+	 * @param position
+	 *            The index in the list.
+	 * @return The glist item, or 0 if it is outside the list.
+	 * @see GListReference#getData(long)
+	 */
+	protected long getItemHandle(int position) {
+		ensureNotDestroyed();
+		return nth_native(position);
+	}
+
+	private native long nth_native(int position);
+
+	/**
+	 * Gets the data pointer for a list handle.
+	 * 
+	 * @param handle
+	 *            The list node handle
+	 * @return The pointer contained in the list.
+	 */
+	protected E getData(long handle) {
+		ensureNotDestroyed();
+		long data = data_native(handle);
+		return peerGenerator.getPeer(data);
+	}
+
+	private native long data_native(long handle);
+
+	/**
+	 * Gets the handle of the next list item.
+	 * 
+	 * @param handle
+	 *            The next item handle.
+	 * @return
+	 */
+	protected long next(long handle) {
+		ensureNotDestroyed();
+		return next_native(handle);
+	}
+
+	private native long next_native(long handle);
+
+	/**
+	 * Gets the handle of the previous list item.
+	 * 
+	 * @param handle
+	 *            The current item handle.
+	 * @return
+	 */
+	protected long previous(long handle) {
+		ensureNotDestroyed();
+		return previous_native(handle);
+	}
+
+	private native long previous_native(long handle);
+}
============================================================
--- /dev/null	
+++ android/workspace/im.pidgin.libpurple/src/im/pidgin/libpurple/peering/StringPeerGenerator.java	69cd9475efb5d7c5de6df77e8b6815d95ee5607a
@@ -0,0 +1,47 @@
+package im.pidgin.libpurple.peering;
+
+import java.util.Hashtable;
+import java.util.Map.Entry;
+
+/**
+ * This is a string peer generator.
+ * <p>
+ * When it is garbage collected, it releases all strings, so you should hold a
+ * reference to it until you are finished, and delete the reference afterwards.
+ * 
+ * @author michaelz
+ * 
+ */
+public class StringPeerGenerator implements PeerGenerator<String> {
+	private final Hashtable<String, Long> stringReferences = new Hashtable<String, Long>();
+
+	@Override
+	public String getPeer(long pointer) {
+		String string = toString_native(pointer);
+		return string;
+	}
+	
+	private native String toString_native(long charPointer);
+
+	@Override
+	public long getPeer(String string) {
+		Long nativeStr = stringReferences.get(string);
+		if (nativeStr == null) {
+			nativeStr = fromString_native(string);
+			stringReferences.put(string, nativeStr);
+		}
+		return nativeStr;
+	}
+	
+	private native long fromString_native(String string);
+
+	
+	@Override
+	protected void finalize() throws Throwable {
+		for (Entry<String, Long> refEntry : stringReferences.entrySet()) {
+			free_native(refEntry.getKey(), refEntry.getValue());
+		}
+	}
+
+	private native void free_native(String javaString, long nativeString);
+}


More information about the Commits mailing list