001        package com.croftsoft.core.util.mail;
002    
003        import java.util.*;
004    
005        import com.croftsoft.core.lang.lifecycle.Updatable;
006    
007        /**********************************************************************
008        * Replaces incoming messages with outgoing messages when updated.
009        *
010        * For documentation on interface Mail and class FlipMail, please see
011        * the tutorial "Interface Mail" at
012        * <a target="_blank"
013        * href="https://www.croftsoft.com/library/tutorials/mail/">
014        * https://www.croftsoft.com/library/tutorials/mail/</a>
015        *
016        * @version
017        *   $Id: FlipMail.java,v 1.2 2008/01/28 03:40:39 croft Exp $
018        * @since
019        *   2008-01-27
020        * @author
021        *   <a href="https://www.croftsoft.com/">David Wallace Croft</a>
022        **********************************************************************/
023         
024        public final class  FlipMail<Message>
025          implements Mail<Message>, Updatable
026        ///////////////////////////////////////////////////////////////////////
027        ///////////////////////////////////////////////////////////////////////
028        {
029          
030        private List<Message>
031          incomingList,
032          outgoingList,
033          swappingList;
034          
035        ///////////////////////////////////////////////////////////////////////
036        ///////////////////////////////////////////////////////////////////////
037        
038        public  FlipMail ( )
039        ///////////////////////////////////////////////////////////////////////
040        {
041          incomingList = new LinkedList<Message> ( );
042          
043          outgoingList = new LinkedList<Message> ( );
044        }
045        
046        ///////////////////////////////////////////////////////////////////////
047        ///////////////////////////////////////////////////////////////////////
048        
049        public Message  get ( final int  index )
050        ///////////////////////////////////////////////////////////////////////
051        {
052          return incomingList.get ( index );
053        }
054    
055        public int  size ( )
056        ///////////////////////////////////////////////////////////////////////
057        {
058          return incomingList.size ( );
059        }
060    
061        public boolean  offer ( Message  message )
062        ///////////////////////////////////////////////////////////////////////
063        {
064          return outgoingList.add ( message );
065        }
066        
067        public void  update ( )
068        ///////////////////////////////////////////////////////////////////////
069        {
070          swappingList = incomingList;
071          
072          incomingList = outgoingList;
073         
074          outgoingList = swappingList;
075          
076          outgoingList.clear ( );
077        }
078        
079        ///////////////////////////////////////////////////////////////////////
080        ///////////////////////////////////////////////////////////////////////
081        }