001 package gate.creole.annic.apache.lucene.document;
002
003 /**
004 * Copyright 2004 The Apache Software Foundation
005 *
006 * Licensed under the Apache License, Version 2.0 (the "License");
007 * you may not use this file except in compliance with the License.
008 * You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018
019 import java.util.Date;
020
021 /**
022 * Provides support for converting dates to strings and vice-versa.
023 * The strings are structured so that lexicographic sorting orders by date,
024 * which makes them suitable for use as field values and search terms.
025 *
026 * <P>
027 * Note that you do not have to use this class, you can just save your
028 * dates as strings if lexicographic sorting orders them by date. This is
029 * the case for example for dates like <code>yyyy-mm-dd hh:mm:ss</code>
030 * (of course you can leave out the delimiter characters to save some space).
031 * The advantage with using such a format is that you can easily save dates
032 * with the required granularity, e.g. leaving out seconds. This saves memory
033 * when searching with a RangeQuery or PrefixQuery, as Lucene
034 * expands these queries to a BooleanQuery with potentially very many terms.
035 *
036 * <P>
037 * Note: dates before 1970 cannot be used, and therefore cannot be
038 * indexed when using this class.
039 */
040 public class DateField {
041 private DateField() {}
042
043 // make date strings long enough to last a millenium
044 private static int DATE_LEN = Long.toString(1000L*365*24*60*60*1000,
045 Character.MAX_RADIX).length();
046
047 public static String MIN_DATE_STRING() {
048 return timeToString(0);
049 }
050
051 public static String MAX_DATE_STRING() {
052 char[] buffer = new char[DATE_LEN];
053 char c = Character.forDigit(Character.MAX_RADIX-1, Character.MAX_RADIX);
054 for (int i = 0 ; i < DATE_LEN; i++)
055 buffer[i] = c;
056 return new String(buffer);
057 }
058
059 /**
060 * Converts a Date to a string suitable for indexing.
061 * @throws RuntimeException if the date specified in the
062 * method argument is before 1970
063 */
064 public static String dateToString(Date date) {
065 return timeToString(date.getTime());
066 }
067 /**
068 * Converts a millisecond time to a string suitable for indexing.
069 * @throws RuntimeException if the time specified in the
070 * method argument is negative, that is, before 1970
071 */
072 public static String timeToString(long time) {
073 if (time < 0)
074 throw new RuntimeException("time too early");
075
076 String s = Long.toString(time, Character.MAX_RADIX);
077
078 if (s.length() > DATE_LEN)
079 throw new RuntimeException("time too late");
080
081 // Pad with leading zeros
082 if (s.length() < DATE_LEN) {
083 StringBuffer sb = new StringBuffer(s);
084 while (sb.length() < DATE_LEN)
085 sb.insert(0, 0);
086 s = sb.toString();
087 }
088
089 return s;
090 }
091
092 /** Converts a string-encoded date into a millisecond time. */
093 public static long stringToTime(String s) {
094 return Long.parseLong(s, Character.MAX_RADIX);
095 }
096 /** Converts a string-encoded date into a Date object. */
097 public static Date stringToDate(String s) {
098 return new Date(stringToTime(s));
099 }
100 }
|