001: package org.apache.lucene.document;
002:
003: /**
004: * Licensed to the Apache Software Foundation (ASF) under one or more
005: * contributor license agreements. See the NOTICE file distributed with
006: * this work for additional information regarding copyright ownership.
007: * The ASF licenses this file to You under the Apache License, Version 2.0
008: * (the "License"); you may not use this file except in compliance with
009: * the License. You may obtain a copy of the License at
010: *
011: * http://www.apache.org/licenses/LICENSE-2.0
012: *
013: * Unless required by applicable law or agreed to in writing, software
014: * distributed under the License is distributed on an "AS IS" BASIS,
015: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016: * See the License for the specific language governing permissions and
017: * limitations under the License.
018: */
019:
020: import org.apache.lucene.search.PrefixQuery;
021: import org.apache.lucene.search.RangeQuery;
022:
023: import java.util.Date; // for javadoc
024:
025: /**
026: * Provides support for converting dates to strings and vice-versa.
027: * The strings are structured so that lexicographic sorting orders by date,
028: * which makes them suitable for use as field values and search terms.
029: *
030: * <P>Note that this class saves dates with millisecond granularity,
031: * which is bad for {@link RangeQuery} and {@link PrefixQuery}, as those
032: * queries are expanded to a BooleanQuery with a potentially large number
033: * of terms when searching. Thus you might want to use
034: * {@link DateTools} instead.
035: *
036: * <P>
037: * Note: dates before 1970 cannot be used, and therefore cannot be
038: * indexed when using this class. See {@link DateTools} for an
039: * alternative without such a limitation.
040: *
041: * @deprecated If you build a new index, use {@link DateTools} instead. This class is included for use with existing
042: * indices and will be removed in a future release.
043: */
044: public class DateField {
045:
046: private DateField() {
047: }
048:
049: // make date strings long enough to last a millenium
050: private static int DATE_LEN = Long.toString(
051: 1000L * 365 * 24 * 60 * 60 * 1000, Character.MAX_RADIX)
052: .length();
053:
054: public static String MIN_DATE_STRING() {
055: return timeToString(0);
056: }
057:
058: public static String MAX_DATE_STRING() {
059: char[] buffer = new char[DATE_LEN];
060: char c = Character.forDigit(Character.MAX_RADIX - 1,
061: Character.MAX_RADIX);
062: for (int i = 0; i < DATE_LEN; i++)
063: buffer[i] = c;
064: return new String(buffer);
065: }
066:
067: /**
068: * Converts a Date to a string suitable for indexing.
069: * @throws RuntimeException if the date specified in the
070: * method argument is before 1970
071: */
072: public static String dateToString(Date date) {
073: return timeToString(date.getTime());
074: }
075:
076: /**
077: * Converts a millisecond time to a string suitable for indexing.
078: * @throws RuntimeException if the time specified in the
079: * method argument is negative, that is, before 1970
080: */
081: public static String timeToString(long time) {
082: if (time < 0)
083: throw new RuntimeException("time '" + time
084: + "' is too early, must be >= 0");
085:
086: String s = Long.toString(time, Character.MAX_RADIX);
087:
088: if (s.length() > DATE_LEN)
089: throw new RuntimeException("time '" + time
090: + "' is too late, length of string "
091: + "representation must be <= " + DATE_LEN);
092:
093: // Pad with leading zeros
094: if (s.length() < DATE_LEN) {
095: StringBuffer sb = new StringBuffer(s);
096: while (sb.length() < DATE_LEN)
097: sb.insert(0, 0);
098: s = sb.toString();
099: }
100:
101: return s;
102: }
103:
104: /** Converts a string-encoded date into a millisecond time. */
105: public static long stringToTime(String s) {
106: return Long.parseLong(s, Character.MAX_RADIX);
107: }
108:
109: /** Converts a string-encoded date into a Date object. */
110: public static Date stringToDate(String s) {
111: return new Date(stringToTime(s));
112: }
113: }
|