Pankaj, gifted a mobile to her sister Arti. she recharged mobile with 94 rs for net connection with the upper limit of 3gb and surfing speed of only 12Kbps. She asked her friend Amit to code an application which queues her all mails and send them in bulk whenever she connects to net. She added more conditions in her need.
- Mails must be sending in the same order she wrote. Say, if she wrote 1st mail to Shreya, then to Shruti, then to Shilpi, then whenever she connects net mail must be send to Shreya first and so on. So if net gets disconnected by any reason oldest mails must be dispatched first. (Sequence is important)
- If there is a mail marked as important then it must be send before any unimportant mail. (Priority is important)
- Her mailing application must be password protected so no one else can send mails from her mobile.(Security is important)
- Since her mobile has only 26mb internal memory so if she already had wrote mails up to 3mb she must be asked to connect net first. So the old mails can be dispatched to free disk space.
- She must be able to delete a mail which is written but not send yet.
- There should be a facility to schedule mails to send on particular time or date.(Scheduling is important)
From the first point of Arti’s requirement it was clear that Amit was thinking to go for queue data structure since all the mails has to go in FIFIO order. But he had to select appropriate queue type. Amit created 3 storage areas/variable for this.
- Draft Collection: A collection which can store draft mails. It can be any bounded collection. Since the sequence doesn’t matter so any special data structure, like queue/stack, is not required. As soon as Arti click on send button, this mail must be moved from this Draft collection to Mails Collection. Since any mail can be sent or discarded any time so random access would be required. So I am going with ArrayList.
- Mail Collection: This collection will store all the mails which are ready to send. Since mails have to be send in the same sequence they were written so Queue is only the option for this. Some more points are here to decide which kind of Queue will fit to this application.
- Random access is not required from Mails Queue. So ArrayList doesn’t get fit here.
- Important mails must queue up. Amit can do this with PriorityQueue easily. Every mail will have some priority. Whichever mail has higher priority will be pulled ahead in the queue.
- If internet is connected and there is no mail in Mails Queue then the application must wait if Arti is writing a mail. This sort of requirement can be fulfilled by some kind of BlockingQueue where producer waits if queue is full and consumer waits if queue is empty. For this application PriorityQueue + BlockingQueue = PriorityBlockingQueue.
- We also need scheduling of mails. For this I am taking DelayQueue. Elements from DelayQueue can be accessed only if they are expired. For this need I wrote separate utility who take elements from Scheduled queue once their scheduled time is over and put it to Mails Queue.
- MailSend Collection: It fetch mail from Mails Queue whenever internet is connected. Since only one mail can be send at a time so a synchronized collection of size 1 is required. SynchronousQueue fit for this need.
- An ArralyList is required to keep draft mails.
- A PriorityBlockingQueue keeps mails ready to send
- A DelayQueue keeps scheduled mails. These mails move to PriorityBlockingQueue to send once their scheduled time is over.
- A SynchronousQueue hold the mail which is being send.
*This article is copyrighted with all examples and images used in this article. Publication of this article to any where without referencing to original article or to hard copy or any modification in original images or example is strictly prohibited.