-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEmailContainer.java
94 lines (81 loc) · 3.32 KB
/
EmailContainer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package com.company;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
// contains deserialized email objects for the given date.
public class EmailContainer implements Container{
private static final ArrayList<Email> emails = new ArrayList<>();
private static final EmailContainer emailContainer = new EmailContainer();
// singleton pattern.
private EmailContainer(){}
public static EmailContainer getEmailContainer(){
return emailContainer;
}
// deserialize and add Email objects to the list.
private void addEmails(String date){
// get the path of the folder for the given date.
String path = System.getProperty("user.dir") + "\\emails\\" + date.replace('/','_') + "\\";
File emailSet = new File(path);
try {
// if that folder exists and that folder contains any files deserialize.
if (emailSet.exists() && Files.list(Paths.get(path)).findAny().isPresent()) {
// get the list of files in the folder.
String[] fileNames = emailSet.list();
for (String fileName : fileNames) {
FileInputStream fileInputStream = null;
ObjectInputStream objectInputStream = null;
try {
// deserialization and adding email objects to an arraylist.
fileInputStream = new FileInputStream(path + fileName);
objectInputStream = new ObjectInputStream(fileInputStream);
Email recipient = (Email) objectInputStream.readObject();
emails.add(recipient);
} finally {
// close streams.
if (fileInputStream != null) {
try {
fileInputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
if (objectInputStream != null) {
try {
objectInputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
}catch (IOException | ClassNotFoundException e){
e.printStackTrace();
}
}
// provide an iterator to iterate through the list containing Recipient objects.
@Override
public Iterator getIterator(String date){
addEmails(date);
return new EmailIterator();
}
// inner class for iteration in iterator pattern.
public static class EmailIterator implements Iterator{
private int index;
@Override
public boolean hasNext() {
return index < emails.size();
}
@Override
public Object next() {
if (this.hasNext()){
return emails.get(index++);
}
return null;
}
}
}