1 package SnowMailClient.model; 2 3 import snow.utils.storage.*; 4 import SnowMailClient.Language.Language; 5 import java.util.*; 6 7 8 11 public final class Address implements Vectorizable, Comparable 12 { 13 private String name; 14 private String mailAddress; 15 16 private int numberOfMailsSendedTo; 17 private int numberOfMailsReceivedFrom; 18 private long lastUse = 0; 19 private long birthday = 0; 20 private int[] birthdate = new int[]{-1,-1,-1}; 22 23 private String phone = ""; 24 private String street = ""; 25 private String city = ""; 26 27 28 public boolean selected = false; 30 31 public Address() {} 32 33 35 public Address(String fromFieldText) 36 { 37 this.parseFromField(fromFieldText); 38 } 39 40 41 public static Vector<Address> parseAddressList(String 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 58 private void parseFromField(String fromFieldText) 59 { 60 String ad = (fromFieldText!=null ? fromFieldText.trim() : ""); 62 63 int foundStart = ad.indexOf('<'); 66 if(foundStart==-1) { foundStart = ad.indexOf('['); } 67 68 if (foundStart != -1) 69 { 70 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 mailAddress = ad; 90 name = "?"; 91 } 92 } 93 94 95 98 private String parseName(String _n) 99 { 100 String 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 toString() 113 { 114 return "\""+name+"\" <"+mailAddress+">"; 115 } 116 117 public String getMailAddress() { return mailAddress;} 118 public String getName() { return name;} 119 public void setName(String n) { name = n;} 120 public void setMailAddress(String 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 getPhone() { return phone; } 129 public void setPhone(String p) { phone = p; } 130 131 public String getStreet() { return street; } 132 public void setStreet(String p) { street = p; } 133 134 public String getCity() { return city; } 135 public void setCity(String 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 153 public int compareTo(Object o) 154 { 155 if(o instanceof Address) 156 { 157 Address a2 = (Address) o; 158 return (this.mailAddress+this.getName()).compareTo(a2.getMailAddress()+a2.getName()); 160 } 161 throw new RuntimeException ("Bad Address compare, wrong class "+o); 162 } 163 164 165 public boolean equals(Object 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 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 ) v.elementAt(0); 196 if(version>=1) 197 { 198 mailAddress = (String ) v.elementAt(1); 199 name = (String ) v.elementAt(2); 200 this.numberOfMailsReceivedFrom = (Integer ) v.elementAt(3); 201 this.numberOfMailsSendedTo = (Integer ) v.elementAt(4); 202 203 if(version>=3) 204 { 205 selected = (Boolean ) v.elementAt(5); 206 } 207 if(version>=4) 208 { 209 lastUse = (Long ) v.elementAt(6); 210 } 211 if(version>=5) 212 { 213 birthday = (Long ) v.elementAt(7); 214 } 215 if(version>=6) 216 { 217 phone = (String ) v.elementAt(8); 218 street = (String ) v.elementAt(9); 219 city = (String ) 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 > getVectorRepresentation () throws VectorizeException 230 { 231 Vector<Object > v = new Vector<Object >(); 232 v.addElement(6); 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 |