KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lucene > search > DateFilter


1 package org.apache.lucene.search;
2
3 /**
4  * Copyright 2004 The Apache Software Foundation
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 import java.util.BitSet JavaDoc;
20 import java.util.Date JavaDoc;
21 import java.io.IOException JavaDoc;
22
23 import org.apache.lucene.document.DateField;
24 import org.apache.lucene.index.Term;
25 import org.apache.lucene.index.TermDocs;
26 import org.apache.lucene.index.TermEnum;
27 import org.apache.lucene.index.IndexReader;
28
29 /**
30  * A Filter that restricts search results to a range of time.
31  *
32  * <p>For this to work, documents must have been indexed with a
33  * {@link DateField}.</p>
34  *
35  * @deprecated Instead, use {@link RangeFilter} combined with
36  * {@link org.apache.lucene.document.DateTools}.
37  */

38 public class DateFilter extends Filter {
39   String JavaDoc field;
40
41   String JavaDoc start = DateField.MIN_DATE_STRING();
42   String JavaDoc end = DateField.MAX_DATE_STRING();
43
44   private DateFilter(String JavaDoc f) {
45     field = f;
46   }
47
48   /**
49    * Constructs a filter for field <code>f</code> matching dates
50    * between <code>from</code> and <code>to</code> inclusively.
51    */

52   public DateFilter(String JavaDoc f, Date JavaDoc from, Date JavaDoc to) {
53     field = f;
54     start = DateField.dateToString(from);
55     end = DateField.dateToString(to);
56   }
57
58   /**
59    * Constructs a filter for field <code>f</code> matching times
60    * between <code>from</code> and <code>to</code> inclusively.
61    */

62   public DateFilter(String JavaDoc f, long from, long to) {
63     field = f;
64     start = DateField.timeToString(from);
65     end = DateField.timeToString(to);
66   }
67
68   /**
69    * Constructs a filter for field <code>f</code> matching
70    * dates on or before before <code>date</code>.
71    */

72   public static DateFilter Before(String JavaDoc field, Date JavaDoc date) {
73     DateFilter result = new DateFilter(field);
74     result.end = DateField.dateToString(date);
75     return result;
76   }
77
78   /**
79    * Constructs a filter for field <code>f</code> matching times
80    * on or before <code>time</code>.
81    */

82   public static DateFilter Before(String JavaDoc field, long time) {
83     DateFilter result = new DateFilter(field);
84     result.end = DateField.timeToString(time);
85     return result;
86   }
87
88   /**
89    * Constructs a filter for field <code>f</code> matching
90    * dates on or after <code>date</code>.
91    */

92   public static DateFilter After(String JavaDoc field, Date JavaDoc date) {
93     DateFilter result = new DateFilter(field);
94     result.start = DateField.dateToString(date);
95     return result;
96   }
97
98   /**
99    * Constructs a filter for field <code>f</code> matching
100    * times on or after <code>time</code>.
101    */

102   public static DateFilter After(String JavaDoc field, long time) {
103     DateFilter result = new DateFilter(field);
104     result.start = DateField.timeToString(time);
105     return result;
106   }
107
108   /**
109    * Returns a BitSet with true for documents which should be
110    * permitted in search results, and false for those that should
111    * not.
112    */

113   public BitSet JavaDoc bits(IndexReader reader) throws IOException JavaDoc {
114     BitSet JavaDoc bits = new BitSet JavaDoc(reader.maxDoc());
115     TermEnum enumerator = reader.terms(new Term(field, start));
116     TermDocs termDocs = reader.termDocs();
117     if (enumerator.term() == null) {
118       return bits;
119     }
120
121     try {
122       Term stop = new Term(field, end);
123       while (enumerator.term().compareTo(stop) <= 0) {
124         termDocs.seek(enumerator.term());
125         while (termDocs.next()) {
126           bits.set(termDocs.doc());
127         }
128         if (!enumerator.next()) {
129           break;
130         }
131       }
132     } finally {
133       enumerator.close();
134       termDocs.close();
135     }
136     return bits;
137   }
138
139   public String JavaDoc toString() {
140     StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
141     buffer.append(field);
142     buffer.append(":");
143     buffer.append(DateField.stringToDate(start).toString());
144     buffer.append("-");
145     buffer.append(DateField.stringToDate(end).toString());
146     return buffer.toString();
147   }
148 }
149
Popular Tags