FileDocCategorySizeDatePackage
Flags.javaAPI DocJavaMail 1.4.316484Tue Nov 17 10:38:12 GMT 2009javax.mail

Flags

public class Flags extends Object implements Serializable, Cloneable
The Flags class represents the set of flags on a Message. Flags are composed of predefined system flags, and user defined flags.

A System flag is represented by the Flags.Flag inner class. A User defined flag is represented as a String. User flags are case-independent.

A set of standard system flags are predefined. Most folder implementations are expected to support these flags. Some implementations may also support arbitrary user-defined flags. The getPermanentFlags method on a Folder returns a Flags object that holds all the flags that are supported by that folder implementation.

A Flags object is serializable so that (for example) the use of Flags objects in search terms can be serialized along with the search terms.

Warning: Serialized objects of this class may not be compatible with future JavaMail API releases. The current serialization support is appropriate for short term storage.

The below code sample illustrates how to set, examine and get the flags for a message.


Message m = folder.getMessage(1);
m.setFlag(Flags.Flag.DELETED, true); // set the DELETED flag

// Check if DELETED flag is set of this message
if (m.isSet(Flags.Flag.DELETED))
System.out.println("DELETED message");

// Examine ALL system flags for this message
Flags flags = m.getFlags();
Flags.Flag[] sf = flags.getSystemFlags();
for (int i = 0; i < sf.length; i++) {
if (sf[i] == Flags.Flag.DELETED)
System.out.println("DELETED message");
else if (sf[i] == Flags.Flag.SEEN)
System.out.println("SEEN message");
......
......
}

see
Folder#getPermanentFlags
author
John Mani
author
Bill Shannon

Fields Summary
private int
system_flags
private Hashtable
user_flags
private static final int
ANSWERED_BIT
private static final int
DELETED_BIT
private static final int
DRAFT_BIT
private static final int
FLAGGED_BIT
private static final int
RECENT_BIT
private static final int
SEEN_BIT
private static final int
USER_BIT
private static final long
serialVersionUID
Constructors Summary
public Flags()
Construct an empty Flags object.

 
public Flags(Flags flags)
Construct a Flags object initialized with the given flags.

param
flags the flags for initialization

	this.system_flags = flags.system_flags;
	if (flags.user_flags != null)
	    this.user_flags = (Hashtable)flags.user_flags.clone();
    
public Flags(Flag flag)
Construct a Flags object initialized with the given system flag.

param
flag the flag for initialization

	this.system_flags |= flag.bit;
    
public Flags(String flag)
Construct a Flags object initialized with the given user flag.

param
flag the flag for initialization

	user_flags = new Hashtable(1);
	user_flags.put(flag.toLowerCase(Locale.ENGLISH), flag);
    
Methods Summary
public voidadd(javax.mail.Flags$Flag flag)
Add the specified system flag to this Flags object.

param
flag the flag to add

	system_flags |= flag.bit;
    
public voidadd(java.lang.String flag)
Add the specified user flag to this Flags object.

param
flag the flag to add

	if (user_flags == null)
	    user_flags = new Hashtable(1);
	user_flags.put(flag.toLowerCase(Locale.ENGLISH), flag);
    
public voidadd(javax.mail.Flags f)
Add all the flags in the given Flags object to this Flags object.

param
f Flags object

	system_flags |= f.system_flags; // add system flags

	if (f.user_flags != null) { // add user-defined flags
	    if (user_flags == null)
		user_flags = new Hashtable(1);

	    Enumeration e = f.user_flags.keys();

	    while (e.hasMoreElements()) {
		String s = (String)e.nextElement();
		user_flags.put(s, f.user_flags.get(s));
	    }
	}
    
public java.lang.Objectclone()
Returns a clone of this Flags object.

	Flags f = null;
	try {
	    f = (Flags)super.clone();
	} catch (CloneNotSupportedException cex) {
	    // ignore, can't happen
	}
	if (this.user_flags != null && f != null)
	    f.user_flags = (Hashtable)this.user_flags.clone();
	return f;
    
public booleancontains(javax.mail.Flags$Flag flag)
Check whether the specified system flag is present in this Flags object.

return
true of the given flag is present, otherwise false.

	return (system_flags & flag.bit) != 0;
    
public booleancontains(java.lang.String flag)
Check whether the specified user flag is present in this Flags object.

return
true of the given flag is present, otherwise false.

	if (user_flags == null) 
	    return false;
	else
	    return user_flags.containsKey(flag.toLowerCase(Locale.ENGLISH));
    
public booleancontains(javax.mail.Flags f)
Check whether all the flags in the specified Flags object are present in this Flags object.

return
true if all flags in the given Flags object are present, otherwise false.

	// Check system flags
	if ((f.system_flags & system_flags) != f.system_flags)
	    return false;

	// Check user flags
	if (f.user_flags != null) {
	    if (user_flags == null)
		return false;
	    Enumeration e = f.user_flags.keys();

	    while (e.hasMoreElements()) {
		if (!user_flags.containsKey(e.nextElement()))
		    return false;
	    }
	}

	// If we've made it till here, return true
	return true;
    
public booleanequals(java.lang.Object obj)
Check whether the two Flags objects are equal.

return
true if they're equal

	if (!(obj instanceof Flags))
	    return false;

	Flags f = (Flags)obj;

	// Check system flags
	if (f.system_flags != this.system_flags)
	    return false;

	// Check user flags
	if (f.user_flags == null && this.user_flags == null)
	    return true;
	if (f.user_flags != null && this.user_flags != null &&
		f.user_flags.size() == this.user_flags.size()) {
	    Enumeration e = f.user_flags.keys();

	    while (e.hasMoreElements()) {
		if (!this.user_flags.containsKey(e.nextElement()))
		    return false;
	    }
	    return true;
	}

	return false;
    
public javax.mail.Flags$Flag[]getSystemFlags()
Return all the system flags in this Flags object. Returns an array of size zero if no flags are set.

return
array of Flags.Flag objects representing system flags

	Vector v = new Vector();
	if ((system_flags & ANSWERED_BIT) != 0)
	    v.addElement(Flag.ANSWERED);
	if ((system_flags & DELETED_BIT) != 0)
	    v.addElement(Flag.DELETED);
	if ((system_flags & DRAFT_BIT) != 0)
	    v.addElement(Flag.DRAFT);
	if ((system_flags & FLAGGED_BIT) != 0)
	    v.addElement(Flag.FLAGGED);
	if ((system_flags & RECENT_BIT) != 0)
	    v.addElement(Flag.RECENT);
	if ((system_flags & SEEN_BIT) != 0)
	    v.addElement(Flag.SEEN);
	if ((system_flags & USER_BIT) != 0)
	    v.addElement(Flag.USER);

	Flag[] f = new Flag[v.size()];
	v.copyInto(f);
	return f;
    
public java.lang.String[]getUserFlags()
Return all the user flags in this Flags object. Returns an array of size zero if no flags are set.

return
array of Strings, each String represents a flag.

	Vector v = new Vector();
	if (user_flags != null) {
	    Enumeration e = user_flags.elements();

	    while (e.hasMoreElements())
		v.addElement(e.nextElement());
	}

	String[] f = new String[v.size()];
	v.copyInto(f);
	return f;
    
public inthashCode()
Compute a hash code for this Flags object.

return
the hash code

	int hash = system_flags;
	if (user_flags != null) {
	    Enumeration e = user_flags.keys();
	    while (e.hasMoreElements())
		hash += ((String)e.nextElement()).hashCode();
	}
	return hash;
    
public voidremove(javax.mail.Flags f)
Remove all flags in the given Flags object from this Flags object.

param
f the flag to be removed

	system_flags &= ~f.system_flags; // remove system flags

	if (f.user_flags != null) {
	    if (user_flags == null)
		return;

	    Enumeration e = f.user_flags.keys();
	    while (e.hasMoreElements())
		user_flags.remove(e.nextElement());
	}
    
public voidremove(javax.mail.Flags$Flag flag)
Remove the specified system flag from this Flags object.

param
flag the flag to be removed

	system_flags &= ~flag.bit;
    
public voidremove(java.lang.String flag)
Remove the specified user flag from this Flags object.

param
flag the flag to be removed

	if (user_flags != null)
	    user_flags.remove(flag.toLowerCase(Locale.ENGLISH));