JAVAMAIL中提供搜索邮件的API,具体用法可以参考之前的博文,下面是我对其源代码的学习:
1、运行实例:
package mysearch;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class Main {
public static void main(String[] args) {
List<Msg> list = new ArrayList<Msg>();
long time = System.currentTimeMillis();
list.add(new Msg(new Date(time), 1, "subject1"));
list.add(new Msg(new Date(time - 1000), 2, "subject2"));
list.add(new Msg(new Date(time - 60 * 1000), 3, "subject3"));
list.add(new Msg(new Date(time - 2 * 60 * 1000), 1, "subject14"));
// 查询条件:!((subject like ject1 and senddate < time)or size>2)
// 应该得到第1、4条记录
MySearchTerm subTerm = new MySubjectTerm("ject1");
MySearchTerm dateTerm = new MySendDateTerm(MyCompTerm.LT,new Date(time));
MySearchTerm sizeTerm = new MySizeTerm(MyCompTerm.GT,2);
MySearchTerm term = new MyAndTerm(subTerm,dateTerm);
term = new MyOrTerm(term,sizeTerm);
term = new MyNotTerm(term);
int count = 0;
for (Msg m : list) {
if (term.match(m)) {
count++;
System.out.println(m);
}
}
System.out.println("count:" + count);
}
}
输出结果:
sendDate:Fri Apr 24 21:58:12 CST 2009 size:1 subject:subject1
sendDate:Fri Apr 24 21:58:11 CST 2009 size:2 subject:subject2
count:2
2、代码结构:
file://F:\dev\eclipse\workspace\hibertest\src\mysearch
Main.java
Msg.java
MyAndTerm.java
MyCompTerm.java
MyDateTerm.java
MyIntTerm.java
MyNotTerm.java
MyOrTerm.java
MySearchTerm.java
MySendDateTerm.java
MySizeTerm.java
MyStrTerm.java
MySubjectTerm.java
3、具体实现:
(1)抽象类MySearchTerm:
package mysearch;
public abstract class MySearchTerm {
public abstract boolean match(Msg msg);
}
(2)与、或、非关联关系类:
与操作:
package mysearch;
/** *//**
* 与操作
* @author kinkding
*/
public class MyAndTerm extends MySearchTerm {
protected MySearchTerm terms[];
public MyAndTerm(MySearchTerm t1, MySearchTerm t2) {
terms = new MySearchTerm[2];
terms[0] = t1;
terms[1] = t2;
}
public MyAndTerm(MySearchTerm t[]) {
terms = new MyAndTerm[t.length];
for (int i = 0; i < t.length; i++) {
terms[i] = t[i];
}
}
@Override
public boolean match(Msg msg) {
for (MySearchTerm term : terms) {
if (!term.match(msg)) {
return false;
}
}
return true;
}
}
或操作:
package mysearch;
/** *//**
* 或操作
* @author kinkding
*/
public class MyOrTerm extends MySearchTerm {
protected MySearchTerm terms[];
public MyOrTerm(MySearchTerm t1, MySearchTerm t2) {
terms = new MySearchTerm[2];
terms[0] = t1;
terms[1] = t2;
}
public MyOrTerm(MySearchTerm t[]) {
terms = new MySearchTerm[t.length];
for (int i = 0; i < t.length; i++) {
terms[i] = t[i];
}
}
@Override
public boolean match(Msg msg) {
for (MySearchTerm term : terms) {
if (term.match(msg)) {
return true;
}
}
return false;
}
}
非操作:
package mysearch;
/** *//**
* 非操作
* @author kinkding
*/
public class MyNotTerm extends MySearchTerm {
protected MySearchTerm term;
public MyNotTerm(MySearchTerm t) {
term = t;
}
@Override
public boolean match(Msg msg) {
return !term.match(msg);
}
}
(3)数值比较类:
package mysearch;
public abstract class MyCompTerm extends MySearchTerm{
public static final int LE = 1;// <=
public static final int LT = 2;// <
public static final int EQ = 3;// ==
public static final int NE = 4;// !=
public static final int GT = 5;// >
public static final int GE = 6;// >=
protected int comparison;
}
(3)字符、日期、整型比较类:
字符比较:
package mysearch;
public abstract class MyStrTerm extends MySearchTerm {
protected String str;
public MyStrTerm(String s) {
str = s;
}
/** *//** 相当于 %str%=s*/
protected boolean match(String s) {
for(int i=0,len = s.length()-str.length();i<=len;i++){
if(s.regionMatches(i, str, 0, str.length())){
return true;
}
}
return false;
}
}
日期比较:
package mysearch;
import java.util.Date;
public abstract class MyDateTerm extends MyCompTerm {
protected Date date;
public MyDateTerm(int comp, Date d) {
this.comparison = comp;
this.date = d;
}
/** *//** d comp date */
protected boolean match(Date d) {
long td = d.getTime();
long tt = date.getTime();
switch (comparison) {
case LT:
return td < tt;
case LE:
return td <= tt;
case EQ:
return td == tt;
case NE:
return td != tt;
case GT:
return td > tt;
case GE:
return td >= tt;
default:
return false;
}
}
}
整型比较:
package mysearch;
public abstract class MyIntTerm extends MyCompTerm {
protected int value;
public MyIntTerm(int comp, int i) {
this.comparison = comp;
this.value = i;
}
/** *//** i comp value */
protected boolean match(int i) {
switch (comparison) {
case LT:
return i < value;
case LE:
return i <= value;
case EQ:
return i == value;
case NE:
return i != value;
case GT:
return i > value;
case GE:
return i >= value;
default:
return false;
}
}
}
(4)具体业务类:
消息类:
package mysearch;
import java.util.Date;
public class Msg {
private Date sendDate;
private String subject;
private int size;
public Date getSendDate() {
return sendDate;
}
public String getSubject() {
return subject;
}
public int getSize() {
return size;
}
public Msg(Date sendDate, int size, String subject) {
super();
this.sendDate = sendDate;
this.size = size;
this.subject = subject;
}
public String toString() {
return "sendDate:" + sendDate + " size:" + size + " subject:" + subject;
}
}
发送日期查询:
package mysearch;
import java.util.Date;
public class MySendDateTerm extends MyDateTerm {
public MySendDateTerm(int comp, Date d) {
super(comp, d);
}
@Override
public boolean match(Msg msg) {
return msg == null ? false : super.match(msg.getSendDate());
}
}
大小查询:
package mysearch;
public class MySizeTerm extends MyIntTerm {
public MySizeTerm(int comp, int i) {
super(comp, i);
}
@Override
public boolean match(Msg msg) {
return msg==null?false:super.match(msg.getSize());
}
}
主题查询:
package mysearch;
public class MySubjectTerm extends MyStrTerm {
public MySubjectTerm(String s) {
super(s);
}
@Override
public boolean match(Msg msg) {
return msg == null ? false : super.match(msg.getSubject());
}
}
4、代码下载地址
mysearchrar