Show
Ignore:
Timestamp:
08/01/08 10:18:16 PM (5 months ago)
Author:
octorian
Message:

Mailbox node serialization

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/LogicMail/src/org/logicprobe/LogicMail/model/MailboxNode.java

    r256 r259  
    3131package org.logicprobe.LogicMail.model; 
    3232 
     33import java.io.DataInputStream; 
     34import java.io.DataOutputStream; 
     35import java.io.IOException; 
    3336import java.util.Hashtable; 
    3437import java.util.Vector; 
     
    3740import org.logicprobe.LogicMail.mail.FolderTreeItem; 
    3841import org.logicprobe.LogicMail.util.EventListenerList; 
     42import org.logicprobe.LogicMail.util.Serializable; 
     43import org.logicprobe.LogicMail.util.UniqueIdGenerator; 
    3944 
    4045/** 
     
    4348 * <tt>MessageNode</tt> instances as its children. 
    4449 */ 
    45 public class MailboxNode implements Node { 
     50public class MailboxNode implements Node, Serializable { 
     51        private long uniqueId; 
    4652        private AccountNode parentAccount; 
    4753        private MailboxNode parentMailbox; 
     
    5965        public final static int TYPE_TRASH  = 4; 
    6066 
     67        /** 
     68         * Initializes a new instance of <tt>MailboxNode</tt>. 
     69         *  
     70         * @param folderTreeItem The folder item this node wraps. 
     71         * @param type The type of mailbox this is representing. 
     72         */ 
    6173        MailboxNode(FolderTreeItem folderTreeItem, int type) { 
     74                this.uniqueId = UniqueIdGenerator.getInstance().getUniqueId(); 
    6275                this.mailboxes = new Vector(); 
    6376                this.messages = new Vector(); 
    6477                this.messageMap = new Hashtable(); 
    65                 this.folderTreeItem = folderTreeItem; 
     78                if(folderTreeItem != null) { 
     79                        this.setFolderTreeItem(new FolderTreeItem(folderTreeItem)); 
     80                } 
    6681                this.type = type; 
    6782        } 
    6883         
     84        /** 
     85         * Initializes a new instance of <tt>MailboxNode</tt>. 
     86         *  
     87         * @param folderTreeItem The folder item this node wraps. 
     88         */ 
    6989        MailboxNode(FolderTreeItem folderTreeItem) { 
    7090                this(folderTreeItem, TYPE_NORMAL); 
    7191        } 
    7292         
    73         MailboxNode() { 
     93        /** 
     94         * Initializes a new instance of <tt>MailboxNode</tt>. 
     95         *  
     96         * <p><i>Note:</i> This constructor is only exposed for 
     97         * serialization purposes, and should never be called from 
     98         * outside this package for any other reason. 
     99         */ 
     100        public MailboxNode() { 
    74101                this(null, TYPE_NORMAL); 
    75102        } 
     
    121148         * that maintain cache data for it. 
    122149         *  
     150         * <p><i>Note:</i> The actual <tt>FolderTreeItem</tt> used by this 
     151         * class is a deep copy of the one passed to this method.  It is 
     152         * implemented this way since we need a standalone object that 
     153         * is not part of a tree.  This is necessary to avoid excessive 
     154         * recursion during serialization. 
     155         *  
    123156         * @param folderTreeItem Folder tree item. 
    124157         */ 
    125158        void setFolderTreeItem(FolderTreeItem folderTreeItem) { 
    126                 this.folderTreeItem = folderTreeItem; 
     159                this.folderTreeItem = new FolderTreeItem(folderTreeItem); 
    127160        } 
    128161         
     
    452485        } 
    453486    } 
     487 
     488        public long getUniqueId() { 
     489                return uniqueId; 
     490        } 
     491         
     492        public void serialize(DataOutputStream output) throws IOException { 
     493                output.writeLong(uniqueId); 
     494                output.writeInt(type); 
     495                folderTreeItem.serialize(output); 
     496                synchronized(mailboxes) { 
     497                        int size = mailboxes.size(); 
     498                        output.writeInt(size); 
     499                        for(int i=0; i<size; i++) { 
     500                                ((MailboxNode)mailboxes.elementAt(i)).serialize(output); 
     501                        } 
     502                } 
     503        } 
     504         
     505        public void deserialize(DataInputStream input) throws IOException { 
     506                uniqueId = input.readLong(); 
     507                type = input.readInt(); 
     508                folderTreeItem = new FolderTreeItem(); 
     509                folderTreeItem.deserialize(input); 
     510                synchronized(mailboxes) { 
     511                        int size = input.readInt(); 
     512                        for(int i=0; i<size; i++) { 
     513                                MailboxNode child = new MailboxNode(); 
     514                                child.deserialize(input); 
     515                                child.parentMailbox = this; 
     516                                mailboxes.addElement(child); 
     517                        } 
     518                } 
     519        } 
    454520}