1 21 22 27 28 package javax.mail; 29 30 import java.io.Serializable ; 31 import java.util.*; 32 33 86 87 public class Flags implements Cloneable , Serializable { 88 89 private int system_flags = 0; 90 private Hashtable user_flags = null; 91 92 private final static int ANSWERED_BIT = 0x01; 93 private final static int DELETED_BIT = 0x02; 94 private final static int DRAFT_BIT = 0x04; 95 private final static int FLAGGED_BIT = 0x08; 96 private final static int RECENT_BIT = 0x10; 97 private final static int SEEN_BIT = 0x20; 98 private final static int USER_BIT = 0x80000000; 99 100 private static final long serialVersionUID = 6243590407214169028L; 101 102 106 public static final class Flag { 107 111 public static final Flag ANSWERED = new Flag(ANSWERED_BIT); 112 113 118 public static final Flag DELETED = new Flag(DELETED_BIT); 119 120 124 public static final Flag DRAFT = new Flag(DRAFT_BIT); 125 126 130 public static final Flag FLAGGED = new Flag(FLAGGED_BIT); 131 132 139 public static final Flag RECENT = new Flag(RECENT_BIT); 140 141 150 public static final Flag SEEN = new Flag(SEEN_BIT); 151 152 161 public static final Flag USER = new Flag(USER_BIT); 162 163 private int bit; 165 private Flag(int bit) { 166 this.bit = bit; 167 } 168 } 169 170 171 174 public Flags() { } 175 176 181 public Flags(Flags flags) { 182 this.system_flags = flags.system_flags; 183 if (flags.user_flags != null) 184 this.user_flags = (Hashtable)flags.user_flags.clone(); 185 } 186 187 192 public Flags(Flag flag) { 193 this.system_flags |= flag.bit; 194 } 195 196 201 public Flags(String flag) { 202 user_flags = new Hashtable(1); 203 user_flags.put(flag.toLowerCase(), flag); 204 } 205 206 211 public void add(Flag flag) { 212 system_flags |= flag.bit; 213 } 214 215 220 public void add(String flag) { 221 if (user_flags == null) 222 user_flags = new Hashtable(1); 223 user_flags.put(flag.toLowerCase(), flag); 224 } 225 226 232 public void add(Flags f) { 233 system_flags |= f.system_flags; 235 if (f.user_flags != null) { if (user_flags == null) 237 user_flags = new Hashtable(1); 238 239 Enumeration e = f.user_flags.keys(); 240 241 while (e.hasMoreElements()) { 242 String s = (String )e.nextElement(); 243 user_flags.put(s, f.user_flags.get(s)); 244 } 245 } 246 } 247 248 253 public void remove(Flag flag) { 254 system_flags &= ~flag.bit; 255 } 256 257 262 public void remove(String flag) { 263 if (user_flags != null) 264 user_flags.remove(flag.toLowerCase()); 265 } 266 267 273 public void remove(Flags f) { 274 system_flags &= ~f.system_flags; 276 if (f.user_flags != null) { 277 if (user_flags == null) 278 return; 279 280 Enumeration e = f.user_flags.keys(); 281 while (e.hasMoreElements()) 282 user_flags.remove(e.nextElement()); 283 } 284 } 285 286 291 public boolean contains(Flag flag) { 292 return (system_flags & flag.bit) != 0; 293 } 294 295 300 public boolean contains(String flag) { 301 if (user_flags == null) 302 return false; 303 else 304 return user_flags.containsKey(flag.toLowerCase()); 305 } 306 307 314 public boolean contains(Flags f) { 315 if ((f.system_flags & system_flags) != f.system_flags) 317 return false; 318 319 if (f.user_flags != null) { 321 if (user_flags == null) 322 return false; 323 Enumeration e = f.user_flags.keys(); 324 325 while (e.hasMoreElements()) { 326 if (!user_flags.containsKey(e.nextElement())) 327 return false; 328 } 329 } 330 331 return true; 333 } 334 335 340 public boolean equals(Object obj) { 341 if (!(obj instanceof Flags )) 342 return false; 343 344 Flags f = (Flags )obj; 345 346 if (f.system_flags != this.system_flags) 348 return false; 349 350 if (f.user_flags == null && this.user_flags == null) 352 return true; 353 if (f.user_flags != null && this.user_flags != null && 354 f.user_flags.size() == this.user_flags.size()) { 355 Enumeration e = f.user_flags.keys(); 356 357 while (e.hasMoreElements()) { 358 if (!this.user_flags.containsKey(e.nextElement())) 359 return false; 360 } 361 return true; 362 } 363 364 return false; 365 } 366 367 372 public int hashCode() { 373 int hash = system_flags; 374 if (user_flags != null) { 375 Enumeration e = user_flags.keys(); 376 while (e.hasMoreElements()) 377 hash += ((String )e.nextElement()).hashCode(); 378 } 379 return hash; 380 } 381 382 388 public Flag[] getSystemFlags() { 389 Vector v = new Vector(); 390 if ((system_flags & ANSWERED_BIT) != 0) 391 v.addElement(Flag.ANSWERED); 392 if ((system_flags & DELETED_BIT) != 0) 393 v.addElement(Flag.DELETED); 394 if ((system_flags & DRAFT_BIT) != 0) 395 v.addElement(Flag.DRAFT); 396 if ((system_flags & FLAGGED_BIT) != 0) 397 v.addElement(Flag.FLAGGED); 398 if ((system_flags & RECENT_BIT) != 0) 399 v.addElement(Flag.RECENT); 400 if ((system_flags & SEEN_BIT) != 0) 401 v.addElement(Flag.SEEN); 402 if ((system_flags & USER_BIT) != 0) 403 v.addElement(Flag.USER); 404 405 Flag[] f = new Flag[v.size()]; 406 v.copyInto(f); 407 return f; 408 } 409 410 416 public String [] getUserFlags() { 417 Vector v = new Vector(); 418 if (user_flags != null) { 419 Enumeration e = user_flags.elements(); 420 421 while (e.hasMoreElements()) 422 v.addElement(e.nextElement()); 423 } 424 425 String [] f = new String [v.size()]; 426 v.copyInto(f); 427 return f; 428 } 429 430 433 public Object clone() { 434 return new Flags (this); 435 } 436 437 568 } 569 | Popular Tags |