☠ patch of doom



Hi,

So Jonathan and I were working on refactoring the code to support
multiple chats and introducing the concept of a "Discussion" which
groups a chat with multiple live comments.

We realized that we needed to generate unique identifiers for the
signalChanged bit, and this also tied in with the magic XML-RPC
demarshalling where you need to generate a unique ID for the javascript
to use.

So we created a ReferenceableObject superclass which holds an id and
also handles the magic RPC bits.  It uses a ReferencableObjectRegistry
to assign each object an id and look them up later.  We sort of expect
Hibernate to take over ReferencableObjectRegistry.  However the
ReferencableObjectInterface interface may remain.

This does make the XML-RPC interface even cooler in that now for example
updateLiveComment just takes a LiveComment parameter instead of an
ActiveTopic and an integer id.

This doomed the javascript which is not at all fixed.  But here's the
direction we are planning to go, will work on this more next week.

Comments on stuff appreciated.
  
Index: com/keypoint/PngEncoderB.java
===================================================================
RCS file: /cvs/gnome/yarrr/src/com/keypoint/PngEncoderB.java,v
retrieving revision 1.1
diff -u -r1.1 PngEncoderB.java
--- com/keypoint/PngEncoderB.java	23 Mar 2005 05:56:00 -0000	1.1
+++ com/keypoint/PngEncoderB.java	25 Mar 2005 00:16:50 -0000
@@ -1,15 +1,11 @@
 package com.keypoint;
 
-import java.awt.Image;
 import java.awt.image.BufferedImage;
 import java.awt.image.DataBuffer;
 import java.awt.image.IndexColorModel;
-import java.awt.image.ImageObserver;
-import java.awt.image.PixelGrabber;
 import java.awt.image.WritableRaster;
 import java.io.ByteArrayOutputStream;
 import java.io.IOException;
-import java.util.zip.CRC32;
 import java.util.zip.Deflater;
 import java.util.zip.DeflaterOutputStream;
 
@@ -161,7 +157,6 @@
         }
         width = image.getWidth( null );
         height = image.getHeight( null );
-        this.image = image;
 
         if (!establishStorageInfo())
         {
Index: org/gnome/yarrr/ActiveTopic.java
===================================================================
RCS file: /cvs/gnome/yarrr/src/org/gnome/yarrr/ActiveTopic.java,v
retrieving revision 1.6
diff -u -r1.6 ActiveTopic.java
--- org/gnome/yarrr/ActiveTopic.java	24 Mar 2005 20:15:29 -0000	1.6
+++ org/gnome/yarrr/ActiveTopic.java	25 Mar 2005 00:16:50 -0000
@@ -3,15 +3,13 @@
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Iterator;
+import java.util.List;
 import java.util.Vector;
 
-import org.gnome.yarrr.xmlrpc.XmlRpcDemarshaller;
-import org.gnome.yarrr.xmlrpc.XmlRpcHandler;
-
 /*
  * Created on Mar 16, 2005
  */
-public class ActiveTopic implements XmlRpcDemarshaller {
+public class ActiveTopic implements ReferencableObjectInterface {
 	
 	private class WaitObject {
 		String name;
@@ -26,46 +24,36 @@
 	Topic topic;
 	Yarrr yarrr;
 	
-	ArrayList/*LiveComment*/ liveComments;
-	Chat thechat;
+	ArrayList /* LiveComment */ closedComments;
+	ArrayList /* Discussion*/ discussions;
     Whiteboard theWhiteboard;
 	
 	ArrayList/*PollWaiter*/ waitObjects;
 	
-	public synchronized LiveComment openComment() {
-		LiveComment comment = new LiveComment(this);
-		liveComments.add(comment);
-		return comment;
-	}
-	
 	public ActiveTopic(Yarrr yarrr, Topic topic) {
 		this.yarrr = yarrr;
 		this.topic = topic;
-		liveComments = new ArrayList();
+		closedComments = new ArrayList();
+		discussions = new ArrayList();
 		waitObjects = new ArrayList();
-		
-		this.thechat = new Chat(this);
+
         this.theWhiteboard = new Whiteboard(this);
 	}
 	
-	public synchronized LiveComment getComment(int id) {
-		for (Iterator i = liveComments.iterator(); i.hasNext();) {
-			LiveComment comment = (LiveComment) i.next();
-			if (comment.getId() == id)
-				return comment;
-		}
-		return null;
+	public synchronized Discussion openDiscussion() {
+		Discussion d = new Discussion(this);
+		this.discussions.add(d);
+		return d;
 	}
 	
-	public synchronized Chat getChat() {
-		return thechat;
+	public synchronized List getDiscussions() {
+		return new Vector(this.discussions);
 	}
-
+	
     public synchronized Whiteboard getWhiteboard() {
         return theWhiteboard;
     }
 
-
 	private WaitObject findWaitObject(String name) {
 		for (Iterator i = waitObjects.iterator(); i.hasNext(); ) {
 			WaitObject obj = (WaitObject) i.next();
@@ -76,11 +64,12 @@
 		return null;
 	}
 	
-	public synchronized void signalChanged(String changed) {
+	public synchronized void signalChanged(ReferencableObjectInterface obj) {
 		ArrayList l = new ArrayList();
-		l.add(changed);
+		l.add(this.getRegistry().getReference(obj));
 		signalChanged(l);
 	}
+	
 	public synchronized void signalChanged(Collection changed) {
 		System.out.println("signalChanged: "+changed);
 		for (Iterator i = changed.iterator(); i.hasNext(); ) {
@@ -130,17 +119,20 @@
 		} while (changed.size() == 0);
 		return changed;
 	}
-	
-	static public Object xmlRpcDemarshal(Object xmlRpcObject, XmlRpcHandler handler) throws DemarshallingException 
-	{
-		try {
-			YarrrXmlRpcMethods methods = (YarrrXmlRpcMethods) handler;
-			Long id = new Long((String)xmlRpcObject);
-			return methods.getYarrr().lookupActiveTopic(id.longValue());
-		} catch (NumberFormatException e) {
-			throw new DemarshallingException(e);
-		}
+
+	public List getClosedComments() {
+		return closedComments;
 	}
-	static public Class xmlRpcMarshalledType = String.class;
 
+	public ReferencableObjectRegistry getRegistry() {
+		return this.yarrr.getRegistry();
+	}
+
+	public long getReferenceId() {
+		return this.topic.getReferenceId();
+	}
+
+	public void setReferenceId(long id) {
+		this.topic.setReferenceId(id);
+	}
 }
Index: org/gnome/yarrr/Chat.java
===================================================================
RCS file: /cvs/gnome/yarrr/src/org/gnome/yarrr/Chat.java,v
retrieving revision 1.4
diff -u -r1.4 Chat.java
--- org/gnome/yarrr/Chat.java	24 Mar 2005 20:15:29 -0000	1.4
+++ org/gnome/yarrr/Chat.java	25 Mar 2005 00:16:50 -0000
@@ -18,7 +18,7 @@
  * @author walters
  *
  */
-public class Chat {
+public class Chat extends ReferencableObject {
 	
 	static Log logger = LogFactory.getLog(Chat.class);
 	
@@ -74,14 +74,15 @@
 	
 	public Chat(ActiveTopic topic) {
 		this.topic = topic;
-		this.messages = new ArrayList();	
+		this.messages = new ArrayList();
+		this.topic.getRegistry().register(this);
 	}
 	
 	synchronized public void addMessage(String author, String contents) {
 		Message msg = new Message(author, contents);
 		this.messages.add(msg);
 		this.version++;
-		this.topic.signalChanged("chat");
+		this.topic.signalChanged(this);
 	}
 	
 	synchronized public int getVersion() {
Index: org/gnome/yarrr/LiveComment.java
===================================================================
RCS file: /cvs/gnome/yarrr/src/org/gnome/yarrr/LiveComment.java,v
retrieving revision 1.4
diff -u -r1.4 LiveComment.java
--- org/gnome/yarrr/LiveComment.java	24 Mar 2005 20:15:29 -0000	1.4
+++ org/gnome/yarrr/LiveComment.java	25 Mar 2005 00:16:50 -0000
@@ -8,7 +8,6 @@
 import java.util.Iterator;
 import java.util.LinkedList;
 import java.util.Vector;
-import java.util.concurrent.atomic.AtomicInteger;
 
 import org.gnome.yarrr.xmlrpc.XmlRpcHandler;
 import org.gnome.yarrr.xmlrpc.XmlRpcMarshaller;
@@ -16,7 +15,7 @@
 /**
  * @author alex
  */
-public class LiveComment {
+public class LiveComment extends ReferencableObject {
 	private final int MAX_OLD_VERSIONS = 10;
 	
 	static class Content implements XmlRpcMarshaller {
@@ -35,13 +34,18 @@
 	static class EditingConflictException extends Exception {
 		private static final long serialVersionUID = 1L;
 	}
+	
+	static class ClosedCommentException extends Exception {
+		private static final long serialVersionUID = 1L;
+	}
+	
 	static class TooOldVersionException extends Exception {
 		private static final long serialVersionUID = 1L;
 	}
 	
 	private ActiveTopic topic;
-	private static AtomicInteger idseq = new AtomicInteger(1);
-	private int id; // id of the live comment in the topic
+	private String closer;
+	private boolean closed;
 	private Date startTime;
 	private ArrayList authors; // List of names for now
 	private String contents;
@@ -65,11 +69,6 @@
 		return null;
 	}
 	
-	
-	public int getId() {
-		return id;
-	}
-	
 	/**
 	 * Gets the current text contents of the comment, and the
 	 * version of the content. 
@@ -94,8 +93,13 @@
 	 * @param basedOnVersion the version of the comment this change was based on
 	 * @return the new version number
 	 * @throws EditingConflictException if there is a conflict updating the text
+	 * @throws ClosedCommentException if comment has been closed
 	 */
-	synchronized public int updateContents(String author, StringDiff diff, int basedOnVersion) throws EditingConflictException {
+	synchronized public int updateContents(String author, StringDiff diff, int basedOnVersion) throws EditingConflictException, ClosedCommentException {
+		if (this.closed) {
+			throw new ClosedCommentException();
+		}
+		
 		String newContent = diff.applyTo(contents);
 		if (newContent == null) {
 			throw new EditingConflictException();
@@ -107,10 +111,18 @@
 			authors.add(author);
 		}
 		
-		topic.signalChanged("livecomment");
+		topic.signalChanged(this);
 		return version;
 	}
 	
+	synchronized public void close(String closer) throws ClosedCommentException {
+		if (this.closed) {
+			throw new ClosedCommentException();
+		}
+		this.closed = true;
+		this.closer = closer;
+	}
+	
 	synchronized public StringDiff getDiffFrom(int oldVersion) throws TooOldVersionException {
 		if (oldVersion == version)
 			return new StringDiff();
@@ -129,11 +141,11 @@
 		contents = "";
 		version = 0;
 		authors = new ArrayList();
-		this.id = idseq.getAndAdd(1);
 	}
 	
 	public LiveComment(ActiveTopic topic) {
 		this();
 		this.topic = topic;
+		topic.getRegistry().register(this);
 	}
 }
Index: org/gnome/yarrr/Topic.java
===================================================================
RCS file: /cvs/gnome/yarrr/src/org/gnome/yarrr/Topic.java,v
retrieving revision 1.2
diff -u -r1.2 Topic.java
--- org/gnome/yarrr/Topic.java	24 Mar 2005 20:15:29 -0000	1.2
+++ org/gnome/yarrr/Topic.java	25 Mar 2005 00:16:50 -0000
@@ -4,38 +4,18 @@
  */
 package org.gnome.yarrr;
 
-import java.util.Hashtable;
-import java.util.Map;
-import java.util.concurrent.atomic.AtomicLong;
 
 /**
  * Describes the persistant state of a topic.
  */
-public class Topic {
-	private Long id;
+public class Topic extends ReferencableObject {
 	private String name;
-	// Should be in database later
-	private static AtomicLong idseq = new AtomicLong(0);
-	private static Map topicMap = new Hashtable();
 	
 	public Topic(String name) {
 		this.name = name;
-		this.id = new Long(idseq.addAndGet(1));
-		topicMap.put(name, this);
-	}
-	
-	public long getId() {
-		return id.longValue();
 	}
 	
 	public String getName() {
 		return name;
-	}
-	public void setName(String name) {
-		this.name = name;
-	}
-
-	public static Topic lookupTopic(String name) {
-		return (Topic) topicMap.get(name);
 	}
 }
Index: org/gnome/yarrr/Whiteboard.java
===================================================================
RCS file: /cvs/gnome/yarrr/src/org/gnome/yarrr/Whiteboard.java,v
retrieving revision 1.2
diff -u -r1.2 Whiteboard.java
--- org/gnome/yarrr/Whiteboard.java	23 Mar 2005 05:55:59 -0000	1.2
+++ org/gnome/yarrr/Whiteboard.java	25 Mar 2005 00:16:50 -0000
@@ -26,7 +26,7 @@
  * @author dmalcolm
  * 
  */
-public class Whiteboard {
+public class Whiteboard extends ReferencableObject {
     /**
      * @author dmalcolm
      *
@@ -65,12 +65,13 @@
     Whiteboard(ActiveTopic topic) {
         this.topic = topic;
         this.strokes = new ArrayList();
+        this.topic.getRegistry().register(this);
     }
     
     synchronized public int addStroke(Stroke stroke) {
         this.strokes.add(stroke);
         this.version++;
-        this.topic.signalChanged("whiteboard");
+        this.topic.signalChanged(this);
         return this.version;
     }
     
Index: org/gnome/yarrr/Yarrr.java
===================================================================
RCS file: /cvs/gnome/yarrr/src/org/gnome/yarrr/Yarrr.java,v
retrieving revision 1.4
diff -u -r1.4 Yarrr.java
--- org/gnome/yarrr/Yarrr.java	24 Mar 2005 20:15:29 -0000	1.4
+++ org/gnome/yarrr/Yarrr.java	25 Mar 2005 00:16:50 -0000
@@ -19,8 +19,13 @@
 	static Log logger = LogFactory.getLog(XmlRpcHandler.class);
 
 	StandaloneXmlRpcServer rpcServer;
-	private Map /*Long,ActiveTopic*/ activeTopics;
+	private Map /*String,ActiveTopic*/ activeTopics;
+	
+	/* Should go away later when we have a DB */
+	private Map /*String,Topic */ topics;
+	
 	private XmlRpcHandler handler;
+	private ReferencableObjectRegistry registry;
 	
 	/**
 	 * This function is just used initially to test stuff a bit
@@ -30,20 +35,21 @@
 		
 		ActiveTopic active = activateTopic(testTopic);
 		
-		LiveComment comment = active.openComment();
-		active.openComment();
-		
-		Chat chat = active.getChat();
+		Discussion discussion = active.openDiscussion();
+		discussion.openComment();
+		discussion.openComment();
 	}
 	
 	public Yarrr() {
 		activeTopics = new HashMap();
+		topics = new HashMap();
 		handler = new YarrrXmlRpcMethods(this);
+		registry = new ReferencableObjectRegistry();
 		setupTestObjects();
 	}
 	
 	public synchronized Topic lookupTopic(String name) {
-		return Topic.lookupTopic(name);
+		return (Topic) this.topics.get(name);
 	}
 
 	/**
@@ -56,16 +62,16 @@
 	public synchronized ActiveTopic activateTopic(Topic topic) {
 		ActiveTopic activeTopic;
 		
-		activeTopic = lookupActiveTopic(topic.getId());
+		activeTopic = lookupActiveTopic(topic);
 		if (activeTopic == null) {
 			activeTopic = new ActiveTopic(this, topic);
-			activeTopics.put(new Long(topic.getId()), activeTopic);
+			activeTopics.put(topic, activeTopic);
 		}
 		return activeTopic;
 	}
 	
-	public synchronized ActiveTopic lookupActiveTopic(long id) {
-		return (ActiveTopic) activeTopics.get(new Long(id));
+	public synchronized ActiveTopic lookupActiveTopic(Topic topic) {
+		return (ActiveTopic) activeTopics.get(topic);
 	}
 	
 	public void stop() {
@@ -91,5 +97,9 @@
 		
 		logger.info("Yarrr shutting down.");
 		yarrr.stop();
+	}
+
+	public ReferencableObjectRegistry getRegistry() {
+		return this.registry;
 	}
 }
Index: org/gnome/yarrr/YarrrServlet.java
===================================================================
RCS file: /cvs/gnome/yarrr/src/org/gnome/yarrr/YarrrServlet.java,v
retrieving revision 1.3
diff -u -r1.3 YarrrServlet.java
--- org/gnome/yarrr/YarrrServlet.java	24 Mar 2005 20:15:29 -0000	1.3
+++ org/gnome/yarrr/YarrrServlet.java	25 Mar 2005 00:16:50 -0000
@@ -6,14 +6,11 @@
 import java.io.IOException;
 import java.io.OutputStream;
 import java.io.PrintWriter;
-import java.net.MalformedURLException;
-import java.net.URL;
 
 import javax.servlet.RequestDispatcher;
 import javax.servlet.ServletException;
 import javax.servlet.http.HttpServlet;
 import javax.servlet.http.HttpServletRequest;
-import javax.servlet.http.HttpServletRequestWrapper;
 import javax.servlet.http.HttpServletResponse;
 
 import org.apache.xmlrpc.XmlRpcServer;
@@ -62,7 +59,7 @@
     public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
     	String path;
     	Topic topic;
-    	long topicid;
+    	String topicref;
     	
     	path = request.getPathInfo();
     	if (path == null) {
@@ -78,11 +75,12 @@
     		handleUnknownTopic(request, response);
     		return;
     	}
-    	topicid = topic.getId();
+    	
+    	topicref = yarrr.getRegistry().getReference(topic);
     	
     	String fileParam = request.getParameter("file");
         if (fileParam != null && fileParam.equals("whiteboard.png")) {
-            ActiveTopic atopic = this.yarrr.lookupActiveTopic(topic.getId());
+            ActiveTopic atopic = this.yarrr.lookupActiveTopic(topic);
             Whiteboard whiteboard = atopic.getWhiteboard();
             String versionStr = request.getParameter("version");
             int version;
@@ -94,7 +92,7 @@
             response.setContentType("image/png");
             whiteboard.getImage(version, response.getOutputStream());
         } else {
-        	RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/topic.jsp?topicname=" + topic.getName() + "&topicid=" + topicid);
+        	RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/topic.jsp?topicname=" + topic.getName() + "&topicid=" + topicref);
         	if (dispatcher == null)
         		super.doGet(request, response);
         	else {
Index: org/gnome/yarrr/YarrrXmlRpcMethods.java
===================================================================
RCS file: /cvs/gnome/yarrr/src/org/gnome/yarrr/YarrrXmlRpcMethods.java,v
retrieving revision 1.8
diff -u -r1.8 YarrrXmlRpcMethods.java
--- org/gnome/yarrr/YarrrXmlRpcMethods.java	24 Mar 2005 20:15:29 -0000	1.8
+++ org/gnome/yarrr/YarrrXmlRpcMethods.java	25 Mar 2005 00:16:50 -0000
@@ -3,6 +3,7 @@
  */
 package org.gnome.yarrr;
 
+import java.util.List;
 import java.util.Vector;
 
 import org.apache.commons.logging.Log;
@@ -16,17 +17,19 @@
 	static Log logger = LogFactory.getLog(Chat.class);
 	Yarrr yarrr;
 	
-	public LiveComment.Content getLiveComment(ActiveTopic topic, int commentId) {
-		
-		LiveComment comment = topic.getComment(commentId);
-		LiveComment.Content content = comment.getContents();
-		
-		return content;
+	public List /* Discussion */ getDiscussions (ActiveTopic topic) {
+		return topic.getDiscussions();
 	}
 	
-	public Vector getLiveCommentDiff(ActiveTopic topic, int commentId, int oldVersion) throws Exception {
-		LiveComment comment = topic.getComment(commentId);
-
+	public List /* LiveComment */ getLiveComments(Discussion discussion) {
+		return discussion.getComments(); 
+	}
+	
+	public List /* LiveComment */ getClosedComments(ActiveTopic topic) {
+		return topic.getClosedComments();
+	}
+	
+	public Vector getLiveCommentDiff(LiveComment comment, int oldVersion) throws Exception {
 		StringDiff diff;
 		int version;
 		
@@ -41,21 +44,20 @@
 		return res;
 	}
 	
-	public int updateLiveComment(ActiveTopic topic, int commentId, StringDiff diff, int version) throws Exception {
-		LiveComment comment = topic.getComment(commentId);
+	public int updateLiveComment(LiveComment comment, StringDiff diff, int version) throws Exception {
 		int newVersion = comment.updateContents("nobody", diff, version);
 		
 		return newVersion;
 	}
 	
-	public int addChatMessage(ActiveTopic topic, String author, String contents) {
-		Chat c = topic.getChat();
+	public int addChatMessage(Discussion discussion, String author, String contents) {
+		Chat c = discussion.getChat();
 		c.addMessage(author, contents);
 		return 0;
 	}
 	
-	public Chat.MessageFetchResult getMessagesSince(ActiveTopic topic, int version) {
-		Chat c = topic.getChat();
+	public Chat.MessageFetchResult getMessagesSince(Discussion discussion, int version) {
+		Chat c = discussion.getChat();
 		return c.getMessagesSince(version);
 	}
 	
Index: org/gnome/yarrr/Discussion.java
===================================================================
RCS file: org/gnome/yarrr/Discussion.java
diff -N org/gnome/yarrr/Discussion.java
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ org/gnome/yarrr/Discussion.java	1 Jan 1970 00:00:00 -0000
@@ -0,0 +1,44 @@
+/*
+ * Created on Mar 24, 2005
+ */
+package org.gnome.yarrr;
+
+import java.util.Hashtable;
+import java.util.List;
+import java.util.Map;
+import java.util.Vector;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+/**
+ * @author walters
+ */
+public class Discussion extends ReferencableObject {
+
+	static Log logger = LogFactory.getLog(Discussion.class);
+	private static Map topicMap = new Hashtable();
+	
+	private ActiveTopic topic;
+	private List /* LiveComment */ comments;
+	private Chat chat;
+	
+	public Discussion(ActiveTopic topic) {
+		this.topic = topic;
+		this.comments = new Vector();
+		this.chat = new Chat(this.topic);
+		topic.getRegistry().register(this);
+	}
+	
+	public synchronized void openComment() {
+		this.comments.add(new LiveComment(this.topic));
+	}
+	
+	public synchronized List /* LiveComment */ getComments() {
+		return new Vector(comments);
+	}
+	
+	public Chat getChat() {
+		return this.chat;
+	}
+}
Index: org/gnome/yarrr/ReferencableObject.java
===================================================================
RCS file: org/gnome/yarrr/ReferencableObject.java
diff -N org/gnome/yarrr/ReferencableObject.java
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ org/gnome/yarrr/ReferencableObject.java	1 Jan 1970 00:00:00 -0000
@@ -0,0 +1,35 @@
+/*
+ * Created on Mar 24, 2005
+ */
+package org.gnome.yarrr;
+
+import org.gnome.yarrr.ReferencableObjectRegistry.UnreferencedObjectException;
+import org.gnome.yarrr.xmlrpc.XmlRpcHandler;
+import org.gnome.yarrr.xmlrpc.XmlRpcDemarshaller.DemarshallingException;
+
+/**
+ * @author walters
+ */
+public abstract class ReferencableObject implements ReferencableObjectInterface {
+	
+	protected long referenceId;
+	
+	public long getReferenceId() {
+		return this.referenceId;
+	}
+	
+	public void setReferenceId(long id) {
+		this.referenceId = id;
+	}
+	
+	static public Object xmlRpcDemarshal(Object xmlRpcObject, XmlRpcHandler handler) throws DemarshallingException {
+		try {
+			YarrrXmlRpcMethods methods = (YarrrXmlRpcMethods) handler;
+			String objref = (String) xmlRpcObject;
+			return methods.getYarrr().getRegistry().lookup(objref);
+		} catch (UnreferencedObjectException e) {
+			throw new DemarshallingException(e);
+		}
+	}
+	static public Class xmlRpcMarshalledType = String.class;
+}
Index: org/gnome/yarrr/ReferencableObjectInterface.java
===================================================================
RCS file: org/gnome/yarrr/ReferencableObjectInterface.java
diff -N org/gnome/yarrr/ReferencableObjectInterface.java
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ org/gnome/yarrr/ReferencableObjectInterface.java	1 Jan 1970 00:00:00 -0000
@@ -0,0 +1,13 @@
+/*
+ * Created on Mar 24, 2005
+ */
+package org.gnome.yarrr;
+
+
+/**
+ * @author walters
+ */
+public interface ReferencableObjectInterface {
+	public long getReferenceId();
+	public void setReferenceId(long id);
+}
Index: org/gnome/yarrr/ReferencableObjectRegistry.java
===================================================================
RCS file: org/gnome/yarrr/ReferencableObjectRegistry.java
diff -N org/gnome/yarrr/ReferencableObjectRegistry.java
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ org/gnome/yarrr/ReferencableObjectRegistry.java	1 Jan 1970 00:00:00 -0000
@@ -0,0 +1,57 @@
+/*
+ * Created on Mar 24, 2005
+ */
+package org.gnome.yarrr;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.concurrent.atomic.AtomicLong;
+
+/**
+ * @author walters
+ */
+public class ReferencableObjectRegistry {
+
+	/**
+	 * @author walters
+	 */
+	public class UnreferencedObjectException extends Exception {
+		private static final long serialVersionUID = 1L;
+
+		public UnreferencedObjectException() {
+		}
+		
+		public UnreferencedObjectException(Throwable e) {
+			super(e);
+		}
+	}
+	
+	private Map /* Long,Object */ objectRegistry;
+	AtomicLong idseq;
+	
+	public ReferencableObjectRegistry() {
+		this.objectRegistry = new HashMap();
+		idseq = new AtomicLong();
+	}
+	
+	public void register(ReferencableObjectInterface obj) {
+		long newid = idseq.addAndGet(1);
+		obj.setReferenceId(newid);
+	}
+	
+	public String getReference(ReferencableObjectInterface obj) {
+		return obj.getClass().toString() + "/" + obj.getReferenceId();
+	}
+	
+	public Object lookup(String reference) throws UnreferencedObjectException {
+		int index;
+		
+		index = reference.indexOf('/');
+		if (index < 0 || index >= reference.length() - 1)
+			throw new UnreferencedObjectException();
+		
+		long id = Long.parseLong(reference.substring(index));
+		
+		return this.objectRegistry.get(new Long(id));
+	}
+}


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]