001: /*
002: * $Header: /cvsroot/mvnforum/mvnforum/src/com/mvnforum/search/IntegerFilter.java,v 1.11 2007/10/09 11:09:21 lexuanttkhtn Exp $
003: * $Author: lexuanttkhtn $
004: * $Revision: 1.11 $
005: * $Date: 2007/10/09 11:09:21 $
006: *
007: * ====================================================================
008: *
009: * Copyright (C) 2002-2007 by MyVietnam.net
010: *
011: * All copyright notices regarding mvnForum MUST remain
012: * intact in the scripts and in the outputted HTML.
013: * The "powered by" text/logo with a link back to
014: * http://www.mvnForum.com and http://www.MyVietnam.net in
015: * the footer of the pages MUST remain visible when the pages
016: * are viewed on the internet or intranet.
017: *
018: * This program is free software; you can redistribute it and/or modify
019: * it under the terms of the GNU General Public License as published by
020: * the Free Software Foundation; either version 2 of the License, or
021: * any later version.
022: *
023: * This program is distributed in the hope that it will be useful,
024: * but WITHOUT ANY WARRANTY; without even the implied warranty of
025: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
026: * GNU General Public License for more details.
027: *
028: * You should have received a copy of the GNU General Public License
029: * along with this program; if not, write to the Free Software
030: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
031: *
032: * Support can be obtained from support forums at:
033: * http://www.mvnForum.com/mvnforum/index
034: *
035: * Correspondence and Marketing Questions can be sent to:
036: * info at MyVietnam net
037: *
038: * @author: Phong Ta Quoc
039: */
040: package com.mvnforum.search;
041:
042: import java.io.IOException;
043: import java.util.BitSet;
044:
045: import org.apache.lucene.index.IndexReader;
046: import org.apache.lucene.index.Term;
047: import org.apache.lucene.index.TermDocs;
048: import org.apache.lucene.index.TermEnum;
049: import org.apache.lucene.search.Filter;
050:
051: /**
052: * A Filter that restricts search results to a range of integer.
053: *
054: * <p>
055: * For this to work, documents must have been indexed with a
056: */
057: public class IntegerFilter extends Filter {
058: String field;
059: String start = intToString(0);
060: String end = intToString(Integer.MAX_VALUE);
061:
062: private IntegerFilter(String f) {
063: field = f;
064: }
065:
066: /**
067: * Constructs a filter for field <code>f</code> matching Integers between
068: * <code>from</code> and <code>to</code> inclusively.
069: */
070: public IntegerFilter(String field, int from, int to) {
071: this .field = field;
072: start = intToString(from);
073: end = intToString(to);
074: }
075:
076: /**
077: * Constructs a filter for field <code>f</code> matching Integers between
078: * <code>from</code> and <code>to</code> inclusively.
079: */
080: public IntegerFilter(String field, Integer from, Integer to) {
081: this .field = field;
082: start = intToString(from.intValue());
083: end = intToString(to.intValue());
084: }
085:
086: /**
087: * Constructs a filter for field <code>f</code> matching Integers on or
088: * more than <code>Integer</code>.
089: */
090: public static IntegerFilter greaterThan(String field, Integer i) {
091: IntegerFilter result = new IntegerFilter(field);
092: result.start = intToString(i.intValue());
093: return result;
094: }
095:
096: /**
097: * Constructs a filter for field <code>f</code> matching Integers on or
098: * more than <code>Integer</code>.
099: */
100: public static IntegerFilter greaterThan(String field, int i) {
101: IntegerFilter result = new IntegerFilter(field);
102: result.start = intToString(i);
103: return result;
104: }
105:
106: /**
107: * Constructs a filter for field <code>f</code> matching integers on or
108: * less than <code>Integer</code>.
109: */
110: public static IntegerFilter lessThan(String field, Integer i) {
111: IntegerFilter result = new IntegerFilter(field);
112: result.end = intToString(i.intValue());
113: return result;
114: }
115:
116: /**
117: * Constructs a filter for field <code>f</code> matching Integers on or
118: * more than <code>Integer</code>.
119: */
120: public static IntegerFilter lessThan(String field, int i) {
121: IntegerFilter result = new IntegerFilter(field);
122: result.end = intToString(i);
123: return result;
124: }
125:
126: public static String intToString(int i) {
127: // please note that max int with radian=36 is "zik0zj", length is 6
128: // min int is "-zik0zk"
129: String temp = Integer.toString(i, Character.MAX_RADIX);
130: int need0 = 6 - temp.length();
131: StringBuffer result = new StringBuffer(7);
132: if (i < 0) {
133: result.append("0");
134: } else {
135: // if 0, then we MUST append 1
136: result.append("1");
137: }
138: for (int j = 0; j < need0; j++) {
139: result.append("0");
140: }
141: result.append(temp);
142: return result.toString();
143: }
144:
145: public static int stringToInt(String i) {
146: return Integer.parseInt(i, Character.MAX_RADIX);
147: }
148:
149: /**
150: * Returns a BitSet with true for documents which should be permitted in
151: * search results, and false for those that should not.
152: */
153: public BitSet bits(IndexReader reader) throws IOException {
154: BitSet bits = new BitSet(reader.maxDoc());
155: TermEnum enumerator = reader.terms(new Term(field, start));
156: TermDocs termDocs = reader.termDocs();
157: if (enumerator.term() == null) {
158: return bits;
159: }
160: try {
161: Term stop = new Term(field, end);
162: while (enumerator.term().compareTo(stop) <= 0) {
163: termDocs.seek(enumerator.term());
164: while (termDocs.next()) {
165: bits.set(termDocs.doc());
166: }
167: if (!enumerator.next()) {
168: break;
169: }
170: }
171: } finally {
172: enumerator.close();
173: termDocs.close();
174: }
175: return bits;
176: }
177:
178: public String toString() {
179: StringBuffer buffer = new StringBuffer();
180: buffer.append(field);
181: buffer.append(":");
182: buffer.append(start);
183: buffer.append("-");
184: buffer.append(end);
185: return buffer.toString();
186: }
187: }
|