KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > SnowMailClient > model > Address


1 package SnowMailClient.model;
2
3 import snow.utils.storage.*;
4 import SnowMailClient.Language.Language;
5 import java.util.*;
6
7
8 /** a mail address
9     parsed from a format like "Ugly snow" <ugly@snow>
10 */

11 public final class Address implements Vectorizable, Comparable JavaDoc
12 {
13     private String JavaDoc name;
14     private String JavaDoc mailAddress;
15
16     private int numberOfMailsSendedTo;
17     private int numberOfMailsReceivedFrom;
18     private long lastUse = 0;
19     private long birthday = 0;
20     //TODO
21
private int[] birthdate = new int[]{-1,-1,-1};
22
23     private String JavaDoc phone = "";
24     private String JavaDoc street = "";
25     private String JavaDoc city = "";
26
27
28     // used for the view
29
public boolean selected = false;
30
31     public Address() {}
32
33     /** parse the fromFieldText to get an address
34     */

35     public Address(String JavaDoc fromFieldText)
36     {
37        this.parseFromField(fromFieldText);
38     }
39
40
41     public static Vector<Address> parseAddressList(String JavaDoc list)
42     {
43        Vector<Address> ads = new Vector<Address>();
44        StringTokenizer st = new StringTokenizer(list, ";,");
45        while(st.hasMoreTokens())
46        {
47          ads.add(new Address(st.nextToken()));
48        }
49        return ads;
50     }
51
52     /** usually, addresses are in the following format
53          "Sibylle Hello" <sib@befree.com>
54         outlook in exports write them
55          Sibylle Hello [sib@befree.com]
56
57     */

58     private void parseFromField(String JavaDoc fromFieldText)
59     {
60       // kill white spaces
61
String JavaDoc ad = (fromFieldText!=null ? fromFieldText.trim() : "");
62
63       // check if the address contains a < if so then just use whats inside
64
// otherwise use the address given
65
int foundStart = ad.indexOf('<');
66       if(foundStart==-1) { foundStart = ad.indexOf('['); }
67
68       if (foundStart != -1)
69       {
70          // found it strip everything from inside
71
int foundEnd = ad.indexOf('>', foundStart);
72          if(foundEnd==-1) { foundEnd = ad.indexOf(']', foundStart); }
73
74          if (foundEnd != -1)
75          {
76             mailAddress = ad.substring(foundStart+1,foundEnd).trim();
77             name = ad.substring(0,foundStart);
78          }
79          else
80          {
81             mailAddress = ad.substring(foundStart+1).trim();
82             name = ad.substring(0,foundStart);
83          }
84          name = parseName(name);
85       }
86       else
87       {
88          // not found - use as is
89
mailAddress = ad;
90          name = "?";
91       }
92     }
93
94
95     /** when the address is extracted, remains the name,
96        we just trim and remove quotes
97     */

98     private String JavaDoc parseName(String JavaDoc _n)
99     {
100        String JavaDoc n = _n.trim();
101        if(n.startsWith("'") || n.startsWith("\""))
102        {
103           n = n.substring(1);
104        }
105        if(n.endsWith("'") || n.endsWith("\""))
106        {
107           n = n.substring(0,n.length()-1);
108        }
109        return n;
110     }
111
112     public String JavaDoc toString()
113     {
114        return "\""+name+"\" <"+mailAddress+">";
115     }
116
117     public String JavaDoc getMailAddress() { return mailAddress;}
118     public String JavaDoc getName() { return name;}
119     public void setName(String JavaDoc n) { name = n;}
120     public void setMailAddress(String JavaDoc a) { mailAddress = a;}
121
122     public int getNumberOfMailsReceivedFrom() { return this.numberOfMailsReceivedFrom; }
123     public int getNumberOfMailsSendedTo() { return this.numberOfMailsSendedTo; }
124
125     public long getBirthday() { return birthday; }
126     public void setBirthday(long b) { birthday = b; }
127
128     public String JavaDoc getPhone() { return phone; }
129     public void setPhone(String JavaDoc p) { phone = p; }
130
131     public String JavaDoc getStreet() { return street; }
132     public void setStreet(String JavaDoc p) { street = p; }
133
134     public String JavaDoc getCity() { return city; }
135     public void setCity(String JavaDoc p) { city = p; }
136
137     public void incrementMailsReceivedFrom_()
138     {
139       this.numberOfMailsReceivedFrom++;
140       lastUse = new Date().getTime();
141     }
142     public void incrementMailsSendedTo_()
143     {
144       this.numberOfMailsSendedTo++;
145       lastUse = new Date().getTime();
146     }
147
148     public long getLastUse() { return lastUse; }
149
150     // Comparable interface
151
//
152

153     public int compareTo(Object JavaDoc o)
154     {
155       if(o instanceof Address)
156       {
157         Address a2 = (Address) o;
158         //if(this.equals(a2)) return 0;
159
return (this.mailAddress+this.getName()).compareTo(a2.getMailAddress()+a2.getName());
160       }
161       throw new RuntimeException JavaDoc("Bad Address compare, wrong class "+o);
162     }
163
164
165     public boolean equals(Object JavaDoc o)
166     {
167       if(o instanceof Address)
168       {
169         Address a2 = (Address) o;
170         if( a2.getName().equals(getName())
171           &&a2.getMailAddress().equals(getMailAddress()))
172         {
173           return true;
174         }
175       }
176       return false;
177     }
178
179     public int hashCode()
180     {
181       return getMailAddress().hashCode() + getName().hashCode();
182     }
183
184     //
185
// Vectorizable
186
//
187

188     Address(Vector v) throws VectorizeException
189     {
190        createFromVectorRepresentation(v);
191     }
192
193     public final void createFromVectorRepresentation (Vector v) throws VectorizeException
194     {
195        int version = (Integer JavaDoc) v.elementAt(0);
196        if(version>=1)
197        {
198          mailAddress = (String JavaDoc) v.elementAt(1);
199          name = (String JavaDoc) v.elementAt(2);
200          this.numberOfMailsReceivedFrom = (Integer JavaDoc) v.elementAt(3);
201          this.numberOfMailsSendedTo = (Integer JavaDoc) v.elementAt(4);
202
203          if(version>=3)
204          {
205            selected = (Boolean JavaDoc) v.elementAt(5);
206          }
207          if(version>=4)
208          {
209            lastUse = (Long JavaDoc) v.elementAt(6);
210          }
211          if(version>=5)
212          {
213            birthday = (Long JavaDoc) v.elementAt(7);
214          }
215          if(version>=6)
216          {
217            phone = (String JavaDoc) v.elementAt(8);
218            street = (String JavaDoc) v.elementAt(9);
219            city = (String JavaDoc) v.elementAt(10);
220          }
221
222        }
223        else
224        {
225          throw new VectorizeException(Language.translate("Bad address version %",""+version));
226        }
227     }
228
229     public final Vector<Object JavaDoc> getVectorRepresentation () throws VectorizeException
230     {
231        Vector<Object JavaDoc> v = new Vector<Object JavaDoc>();
232        v.addElement(6); //0
233

234        v.addElement(mailAddress);
235        v.addElement(name);
236        v.addElement(numberOfMailsReceivedFrom);
237        v.addElement(numberOfMailsSendedTo);
238        v.addElement(selected);
239        v.addElement(lastUse);
240        v.addElement(birthday);
241        v.addElement(phone);
242        v.addElement(street);
243        v.addElement(city);
244
245        return v;
246     }
247
248 }
Popular Tags