Author: xor
Date: 2008-10-29 16:12:29 +0000 (Wed, 29 Oct 2008)
New Revision: 23173

Modified:
   trunk/plugins/FMSPlugin/FMSBoard.java
   trunk/plugins/FMSPlugin/FMSMessage.java
   trunk/plugins/FMSPlugin/WoT/FMSMessageManagerWoT.java
   trunk/plugins/FMSPlugin/WoT/FMSMessageWoT.java
Log:
Use UpdatableSortedLinkedListWithForeignIndex instead of a HashTable and a 
LinkedList.

Modified: trunk/plugins/FMSPlugin/FMSBoard.java
===================================================================
--- trunk/plugins/FMSPlugin/FMSBoard.java       2008-10-29 15:49:31 UTC (rev 
23172)
+++ trunk/plugins/FMSPlugin/FMSBoard.java       2008-10-29 16:12:29 UTC (rev 
23173)
@@ -11,30 +11,38 @@
 import java.util.NoSuchElementException;

 import freenet.keys.FreenetURI;
+import freenet.support.DoublyLinkedList;
+import freenet.support.IndexableUpdatableSortedLinkedListItem;
+import freenet.support.UpdatableSortedLinkedListItemImpl;
+import freenet.support.UpdatableSortedLinkedListKilledException;
+import freenet.support.UpdatableSortedLinkedListWithForeignIndex;
+import freenet.support.DoublyLinkedList.Item;

 /**
  * @author xor
  *
  */
-public class FMSBoard {
+public class FMSBoard extends UpdatableSortedLinkedListItemImpl implements 
IndexableUpdatableSortedLinkedListItem {

        private final FMSBoard self = this;
-       /*
-        * FIXME: We need a datastructure which is a HashTable and a LinkedList 
which is sorted by Date of the messages.
-        * java.util.LinkedHashSet is the right thing but it does not provide 
sorting. 
+       
+       /**
+        * Contains all messages in this board, both as a Hashmap and as a 
linked list which is sorted by date.
+        * The hashmap is useful for checking whether a message was already 
stored.
+        * The linked list allows fast displaying of all messages.
         */
-       private final Hashtable<FreenetURI, FMSMessage> mMessages = new 
Hashtable<FreenetURI, FMSMessage>();
-       /* FIXME: Use UpdatableSortedLinkedList<FMSMessage> as soon as it is 
implemented */
-       private final LinkedList<FMSMessage> mMessagesSorted = new 
LinkedList<FMSMessage>();
+       private final UpdatableSortedLinkedListWithForeignIndex mMessages = new 
UpdatableSortedLinkedListWithForeignIndex();
+       
        private final FMSMessageManager mMessageManager;
+       
        private final String mName;
+       
        private String mDescription;

        public FMSBoard(FMSMessageManager newMessageManager, String newName, 
String newDescription) {
                if(newName==null || newName.isEmpty())
                        throw new IllegalArgumentException("Empty board name.");

-
                assert(newMessageManager != null);
                mMessageManager = newMessageManager;
                // FIXME: Remove anything dangerous from name and description.
@@ -64,16 +72,14 @@
                return mName;
        }

-       public void addMessage(FMSMessage newMessage) {
-               if(mMessages.put(newMessage.getURI(), newMessage) != null) {
+       public void addMessage(FMSMessage newMessage) throws 
UpdatableSortedLinkedListKilledException {
+               if(mMessages.containsKey(newMessage)) {
                        /* The message was already stored */
                        assert(false); /* TODO: Add logging. I don't know 
whether this should happen. */
                        return;
                }

-               /* FIXME: As soon as mMessagesSorted is implemented uncomment 
this
-               mMessagesSorted.put(newMessage);
-               */ 
+               mMessages.add(newMessage);
        }

        /**
@@ -84,7 +90,7 @@
        public synchronized Iterator<FMSMessage> messageIterator(final 
FMSOwnIdentity identity) {
                return new Iterator<FMSMessage>() {
                        private final FMSOwnIdentity mIdentity = identity;
-                       private Iterator<FMSMessage> iter = 
self.mMessagesSorted.iterator();
+                       private Iterator<FMSMessage> iter = 
self.mMessages.iterator();
                        private FMSMessage next = iter.hasNext() ? iter.next() 
: null;

                        public boolean hasNext() {
@@ -108,5 +114,19 @@

                };
        }
-       
+
+       /* (non-Javadoc)
+        * @see 
freenet.support.IndexableUpdatableSortedLinkedListItem#indexValue()
+        */
+       public Object indexValue() {
+               return mName;
+       }
+
+       /* (non-Javadoc)
+        * @see java.lang.Comparable#compareTo(java.lang.Object)
+        */
+       public int compareTo(Object o) {
+               FMSBoard b = (FMSBoard)o;
+               return mName.compareTo(b.getName());
+       }
 }

Modified: trunk/plugins/FMSPlugin/FMSMessage.java
===================================================================
--- trunk/plugins/FMSPlugin/FMSMessage.java     2008-10-29 15:49:31 UTC (rev 
23172)
+++ trunk/plugins/FMSPlugin/FMSMessage.java     2008-10-29 16:12:29 UTC (rev 
23173)
@@ -3,38 +3,50 @@
  * http://www.gnu.org/ for further details of the GPL. */
 package plugins.FMSPlugin;

+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
 import java.util.Date;
 import java.util.Iterator;
 import java.util.List;
-import java.util.SortedSet;
+import java.util.Set;

 import freenet.keys.FreenetURI;
+import freenet.support.IndexableUpdatableSortedLinkedListItem;
+import freenet.support.UpdatableSortedLinkedListItemImpl;

 /**
  * @author saces, xor
  *
  */
-public abstract class FMSMessage {
+public abstract class FMSMessage extends UpdatableSortedLinkedListItemImpl 
implements IndexableUpdatableSortedLinkedListItem {

-       public final FreenetURI mURI;
+       private final FreenetURI mURI;

-       public final SortedSet<FMSBoard> mBoards; 
+       private final ArrayList<FMSBoard> mBoards; 

-       public final FMSIdentity mAuthor;
+       private final FMSIdentity mAuthor;

-       public final String mTitle;
+       private final String mTitle;

        /**
         * The date when the message was written in UTC time.
         */
-       public final Date mDate;
+       private final Date mDate;

-       public FMSMessage(FreenetURI newURI, SortedSet<FMSBoard> newBoards, 
FMSIdentity newAuthor, String newTitle, Date newDate) {
+       private final String mText;
+       
+       private final ArrayList<FreenetURI> mAttachments;
+       
+       public FMSMessage(FreenetURI newURI, Set<FMSBoard> newBoards, 
FMSIdentity newAuthor, String newTitle, Date newDate, String newText, 
List<FreenetURI> newAttachments) {
                mURI = newURI;
-               mBoards = newBoards;
+               mBoards = new ArrayList<FMSBoard>(newBoards);
+               Collections.sort(mBoards);
                mAuthor = newAuthor;
                mTitle = newTitle;
                mDate = newDate; // TODO: Check out whether Date provides a 
function for getting the timezone and throw an Exception if not UTC.
+               mText = newText;
+               mAttachments = new ArrayList<FreenetURI>(newAttachments);
        }

        /**
@@ -77,11 +89,28 @@
         * Get the text of the message.
         * @return The text of the message.
         */
-       public abstract String getText();
+       public String getText() {
+               return mText;
+       }

        /**
         * Get the attachments of the message.
         * @return The attachments of the message.
         */
-       public abstract List<FreenetURI> getAttachments();
+       public ArrayList<FreenetURI> getAttachments() {
+               return mAttachments;
+       }
+
+       /**
+        * Compare by Date to the other FMSMessage.
+        * @param o An object of type FMSMessage 
+        */
+       public int compareTo(Object o) {
+               FMSMessage m = (FMSMessage)o;
+               return mDate.compareTo(m.getDate());
+       }
+       
+       public Object indexValue() {
+               return mURI;
+       }
 }

Modified: trunk/plugins/FMSPlugin/WoT/FMSMessageManagerWoT.java
===================================================================
--- trunk/plugins/FMSPlugin/WoT/FMSMessageManagerWoT.java       2008-10-29 
15:49:31 UTC (rev 23172)
+++ trunk/plugins/FMSPlugin/WoT/FMSMessageManagerWoT.java       2008-10-29 
16:12:29 UTC (rev 23173)
@@ -11,6 +11,8 @@

 import freenet.keys.FreenetURI;
 import freenet.support.UpdatableSortedLinkedList;
+import freenet.support.UpdatableSortedLinkedListKilledException;
+import freenet.support.UpdatableSortedLinkedListWithForeignIndex;

 import plugins.FMSPlugin.FMSBoard;
 import plugins.FMSPlugin.FMSIdentityManager;
@@ -25,16 +27,9 @@
         * the board. Adding a newly downloaded message therefore is done by 
searching its board and calling 
         * <code>addMessage()</code> on that board. Further, the message is 
also added to mMessages, see below.
         */
-       private Hashtable<String, FMSBoard> mBoards = new Hashtable<String, 
FMSBoard>();
-       
+       private UpdatableSortedLinkedListWithForeignIndex mBoards = new 
UpdatableSortedLinkedListWithForeignIndex();
+
        /**
-        * Contains an alphabetically sorted list of all boards for retrieval 
by boardIterator() for example.
-        */
-       // FIXME: Toad, please make the UpdatableSortedLinkedList use generics.
-       // FIXME: Then we should create a Comparator for FMSBoard
-       private UpdatableSortedLinkedList<FMSBoard> mBoardsSorted = new 
UpdateableSortedLinkedList<FMSBoard>();
-       
-       /**
         * Contains all messages, even though they are also stored in their 
FMSBoard. Used for checking whether
         * a message was already downloaded or not.
         */
@@ -43,26 +38,25 @@
        private ArrayList<FMSOwnIdentityWoT> mOwnIdentites = new 
ArrayList<FMSOwnIdentityWoT>();

        public synchronized FMSBoard getBoardByName(String name) {
-               return mBoards.get(name);
+               return (FMSBoard)mBoards.get(name);
        }

        public synchronized Iterator<FMSBoard> boardIterator(FMSOwnIdentity 
identity) {
-               
+               return (Iterator<FMSBoard>)mBoards.iterator();
        }

-       private boolean shouldDownloadMessage(FreenetURI uri) {
+       private synchronized boolean shouldDownloadMessage(FreenetURI uri) {
                return (mMessages.containsKey(uri));
        }

-       private void onMessageReceived(String blah) { 
-               FMSMessageWoT newMessage = new FMSMessageWoT(null, null, null, 
null, null);
+       private synchronized void onMessageReceived(String newMessageData) 
throws UpdatableSortedLinkedListKilledException { 
+               FMSMessageWoT newMessage = new FMSMessageWoT(null, null, null, 
null, null, null, null);
                String boardName = "";
                String boardDescription = "";
-               FMSBoard board = mBoards.get(boardName);
+               FMSBoard board = getBoardByName(boardName);
                if(board == null) {
                        board = new FMSBoard(this, boardName, boardDescription);
-                       mBoards.put(board.getName(), board);
-                       mBoardsSorted.put(board);
+                       mBoards.add(board);
                }

                mMessages.put(newMessage.getURI(), newMessage);

Modified: trunk/plugins/FMSPlugin/WoT/FMSMessageWoT.java
===================================================================
--- trunk/plugins/FMSPlugin/WoT/FMSMessageWoT.java      2008-10-29 15:49:31 UTC 
(rev 23172)
+++ trunk/plugins/FMSPlugin/WoT/FMSMessageWoT.java      2008-10-29 16:12:29 UTC 
(rev 23173)
@@ -3,11 +3,14 @@
  * http://www.gnu.org/ for further details of the GPL. */
 package plugins.FMSPlugin.WoT;

+import java.util.ArrayList;
 import java.util.Date;
 import java.util.List;
+import java.util.Set;
 import java.util.SortedSet;

 import freenet.keys.FreenetURI;
+import freenet.support.DoublyLinkedList;
 import plugins.FMSPlugin.FMSBoard;
 import plugins.FMSPlugin.FMSIdentity;
 import plugins.FMSPlugin.FMSMessage;
@@ -18,23 +21,9 @@
  */
 public class FMSMessageWoT extends FMSMessage {

-
-       public FMSMessageWoT(FreenetURI newURI, SortedSet<FMSBoard> newBoards, 
FMSIdentity newAuthor, String newTitle, Date newDate) {
-               super(newURI, newBoards, newAuthor, newTitle, newDate);
+       public FMSMessageWoT(FreenetURI newURI, Set<FMSBoard> newBoards, 
FMSIdentity newAuthor, String newTitle, Date newDate, String newText,
+                       List<FreenetURI> newAttachments) {
+               super(newURI, newBoards, newAuthor, newTitle, newDate, newText, 
newAttachments);
                // TODO Auto-generated constructor stub
        }
-
-       @Override
-       public List<FreenetURI> getAttachments() {
-               // TODO Auto-generated method stub
-               return null;
-       }
-
-       @Override
-       public String getText() {
-               // TODO Auto-generated method stub
-               return null;
-       }
-
-
 }


Reply via email to