KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > armedbear > j > mail > GenericMailboxFilter


1 /*
2  * GenericMailboxFilter.java
3  *
4  * Copyright (C) 2002 Peter Graves
5  * $Id: GenericMailboxFilter.java,v 1.1.1.1 2002/09/24 16:09:58 piso Exp $
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  */

21
22 package org.armedbear.j.mail;
23
24 import java.util.List JavaDoc;
25 import org.armedbear.j.Utilities;
26
27 public final class GenericMailboxFilter extends MailboxFilter
28 {
29     private String JavaDoc pattern;
30     private boolean ignoreCase;
31
32     public GenericMailboxFilter(String JavaDoc pattern)
33     {
34         this.pattern = pattern;
35         ignoreCase = Utilities.isLowerCase(pattern);
36     }
37
38     public boolean accept(MailboxEntry entry)
39     {
40         String JavaDoc subject = entry.getSubject();
41         if (subject != null) {
42             if (ignoreCase) {
43                 if (subject.toLowerCase().indexOf(pattern) >= 0)
44                     return true; // Subject matches.
45
} else {
46                 if (subject.indexOf(pattern) >= 0)
47                     return true; // Subject matches.
48
}
49         }
50         MailAddress[] from = entry.getFrom();
51         if (from != null) {
52             if (ignoreCase) {
53                 for (int i = from.length-1; i >= 0; i--) {
54                     if (from[i].matchesIgnoreCase(pattern))
55                         return true;
56                 }
57             } else {
58                 for (int i = from.length-1; i >= 0; i--) {
59                     if (from[i].matches(pattern))
60                         return true;
61                 }
62             }
63         }
64         return false;
65     }
66 }
67
Popular Tags