001: /*
002: * RelativeDateMailboxFilter.java
003: *
004: * Copyright (C) 2002 Peter Graves
005: * $Id: RelativeDateMailboxFilter.java,v 1.1.1.1 2002/09/24 16:09:53 piso Exp $
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: */
021:
022: package org.armedbear.j.mail;
023:
024: import java.util.Calendar;
025:
026: public final class RelativeDateMailboxFilter extends MailboxFilter {
027: private boolean after;
028: private int days;
029: private int months;
030: private int years;
031:
032: private RFC822Date begin;
033: private RFC822Date end;
034:
035: private long initMillis;
036:
037: private RelativeDateMailboxFilter(int days, int months, int years,
038: boolean after) {
039: this .after = after;
040: this .days = days;
041: this .months = months;
042: this .years = years;
043: }
044:
045: public static MailboxFilter getMailboxFilter(String pattern) {
046: // Shortest valid pattern is 3 chars (e.g. "<3d").
047: if (pattern.length() < 3)
048: return null;
049: char c = pattern.charAt(0);
050: boolean after;
051: // First char must be either '<' or '>'.
052: if (c == '<')
053: after = true;
054: else if (c == '>')
055: after = false;
056: else
057: return null;
058: String number = pattern.substring(1, pattern.length() - 1);
059: int n;
060: try {
061: n = Integer.parseInt(number);
062: } catch (NumberFormatException e) {
063: return null;
064: }
065: if (n == 0)
066: return null;
067: c = pattern.charAt(pattern.length() - 1);
068: switch (c) {
069: case 'd':
070: return new RelativeDateMailboxFilter(n, 0, 0, after);
071: case 'w':
072: return new RelativeDateMailboxFilter(n * 7, 0, 0, after);
073: case 'm':
074: return new RelativeDateMailboxFilter(0, n, 0, after);
075: case 'y':
076: return new RelativeDateMailboxFilter(0, 0, n, after);
077: default:
078: return null;
079: }
080: }
081:
082: private void init() {
083: Calendar cal = Calendar.getInstance();
084: if (days != 0) {
085: cal.add(Calendar.DATE, -days);
086: if (after)
087: cal.add(Calendar.DATE, 1);
088: } else if (months != 0) {
089: cal.add(Calendar.MONTH, -months);
090: if (after)
091: cal.add(Calendar.DATE, 1);
092: } else if (years != 0) {
093: cal.add(Calendar.YEAR, -years);
094: if (after)
095: cal.add(Calendar.DATE, 1);
096: }
097: cal.set(Calendar.HOUR_OF_DAY, 0);
098: cal.set(Calendar.MINUTE, 0);
099: cal.set(Calendar.SECOND, 0);
100: cal.set(Calendar.MILLISECOND, 0);
101: if (after) {
102: begin = new RFC822Date(cal.getTime());
103: end = null;
104: } else {
105: begin = null;
106: end = new RFC822Date(cal.getTime());
107: }
108: initMillis = System.currentTimeMillis();
109: }
110:
111: public boolean accept(MailboxEntry entry) {
112: if (initMillis == 0
113: || System.currentTimeMillis() - initMillis > 60000)
114: init();
115: RFC822Date date = entry.getDate();
116: if (begin != null)
117: if (date.before(begin))
118: return false;
119: if (end != null)
120: if (date.after(end))
121: return false;
122: return true;
123: }
124: }
|