5369f553273d5d629ff167ba95ccc6e574ea7e9a
[org.ibex.mail.git] / src / org / ibex / mail / Query.java
1 package org.ibex.mail;
2 import java.util.*;
3 import org.ibex.mail.target.*;
4
5 /**
6  *  [immutable] This class encapsulates a query against a mailbox.
7  *
8  *  The default implementation of Mailbox.query() simply retrieves
9  *  each message and calls Query.match() on it to filter out any
10  *  messages which do not match.
11  *
12  *  Production-quality Mailbox implementations will want to inspect
13  *  the Query object passed to Mailbox.query() to perform more
14  *  efficient queries.
15  */
16 public class Query {
17
18     public static Query not(Query q)                { return new Query(NOT, new Query[] { q },0,0,0,null,null,null,null,null); }
19     public static Query and(Query q1, Query q2)     { return new Query(AND,new Query[]{q1,q2},0,0,0,null,null,null,null,null); }
20     public static Query and(Query[] q)              { return new Query(AND, q,0,0,0,null, null, null, null, null ); }
21     public static Query or(Query q1, Query q2)      { return new Query(OR,new Query[] {q1,q2},0,0,0,null,null, null, null, null); }
22     public static Query or(Query[] q)               { return new Query(OR, q, 0, 0, 0, null, null, null, null, null); }
23     public static Query uid(int min, int max)       { return new Query(UID, null, min, max, 0, null, null, null, null, null); }
24     public static Query messagenum(int min,int max) { return new Query(NUM,null,min,max,0,null,null,null,null, null); }
25     public static Query sent(Date e, Date l)        { return new Query(SENT,null,0,0,0,null,null,e,l,null); }
26     public static Query arrival(Date e, Date l)     { return new Query(ARRIVAL,null,0,0,0,null,null, e,l,null);}
27     public static Query header(String k, String v)  { return new Query(HEADER, null, 0, 0, 0, k, v, null, null, null);}
28     public static Query size(int min, int max)      { return new Query(SIZE, null, min, max, 0, null, null, null, null, null);}
29     public static Query body(String text)           { return new Query(BODY, null, 0, 0, 0, null, text, null, null, null);}
30     public static Query full(String text)           { return new Query(FULL, null, 0, 0, 0, null, text, null, null, null);}
31     public static Query uid(int[] set)              { return new Query(UID,  null, 0 ,0 ,0, null, null, null, null, set);}
32     public static Query num(int[] set)              { return new Query(NUM,  null, 0 ,0 ,0, null, null, null, null, set);}
33     public static Query all()                       { return new Query(ALL, null, 0, 0, 0, null, null, null, null, null); }
34     public static Query deleted()                   { return new Query(DELETED, null, 0, 0, 0, null, null, null, null, null); }
35     public static Query seen()                      { return new Query(SEEN, null, 0, 0, 0, null, null, null, null, null); }
36     public static Query flagged()                   { return new Query(FLAGGED, null, 0, 0, 0, null, null, null, null, null); }
37     public static Query draft()                     { return new Query(DRAFT, null, 0, 0, 0, null, null, null, null, null); }
38     public static Query answered()                  { return new Query(ANSWERED, null, 0, 0, 0, null, null, null, null, null); }
39     public static Query recent()                    { return new Query(RECENT, null, 0, 0, 0, null, null, null, null, null); }
40     public static Query set(boolean uid, int[] set) { return uid ? uid(set) : num(set); }
41
42     private Query(int type, Query[] q,int min,int max, int flags, String key, String text, Date earliest, Date latest, int[] set) {
43         this.type = type; this.q = q; this.min = min; this.max = max; this.flags = flags; this.key = key; this.text = text;
44         this.earliest = earliest; this.latest = latest; this.set = set; }
45
46     public static final int ALL        = 0;
47     public static final int NOT        = 1;
48     public static final int AND        = 2;
49     public static final int OR         = 3;
50     public static final int UID        = 4;
51     public static final int SENT       = 5;
52     public static final int ARRIVAL    = 6;
53     public static final int HEADER     = 7;
54     public static final int SIZE       = 8;
55     public static final int BODY       = 9;
56     public static final int FULL       = 10;
57     public static final int NUM        = 11;
58     public static final int DELETED    = 12;
59     public static final int SEEN       = 13;
60     public static final int FLAGGED    = 14;
61     public static final int DRAFT      = 15;
62     public static final int ANSWERED   = 16;
63     public static final int RECENT     = 17;
64
65     public final int type;
66     public final Query[] q;
67     public final int min;
68     public final int max;
69     public final int flags;
70     public final String key;
71     public final String text;
72     public final Date earliest;
73     public final Date latest;
74     public final int[] set;
75
76     public boolean match(Mailbox.Iterator it) {
77         switch(type) {
78             case ALL:        return true;
79             case NOT:        return !q[0].match(it);
80             case OR:         for(int i=0; i<q.length; i++) if (q[i].match(it)) return true; return false;
81             case AND:        for(int i=0; i<q.length; i++) if (!q[i].match(it)) return false; return true;
82             case UID:        if (set != null) {
83                                  for(int i=0; i<set.length; i+=2) if (set[i] <= it.uid() && set[i+1] >= it.uid()) return true;
84                                  return false; }
85                              else return it.uid() >= min && it.uid() <= max;
86             case NUM:        if (set != null) {
87                                  for(int i=0; i<set.length; i+=2) if (set[i] <= it.num() && set[i+1] >= it.num()) return true;
88                                  return false; }
89                              else return it.num() >= min && it.num() <= max;
90             case SENT:       return (latest==null||it.cur().date.before(latest)) && 
91                                     (earliest==null||it.cur().date.after(earliest));
92             case ARRIVAL:    return (latest == null || it.cur().arrival.before(latest)) &&
93                                     (earliest == null || it.cur().arrival.after(earliest));
94             case SIZE:       return it.cur().rfc822size() >= min && it.cur().rfc822size() <= max;
95             case HEADER:     return it.cur().headers.get(key) != null && ((String)it.cur().headers.get(key)).indexOf(text) != -1;
96             case BODY:       return it.cur().body.indexOf(text) != -1;
97             case FULL:       return it.cur().body.indexOf(text) != -1 || it.cur().allHeaders.indexOf(text) != -1;
98             case DELETED:    return it.deleted();
99             case SEEN:       return it.seen();
100             case FLAGGED:    return it.flagged();
101             case DRAFT:      return it.draft();
102             case ANSWERED:   return it.answered();
103             case RECENT:     return it.recent();
104             default:         throw new Error("this should not happen");
105         }
106     }
107 }