Clover Coverage Report
Coverage timestamp: Fri May 9 2008 10:54:27 EST
../../../../img/srcFileCovDistChart5.png 77% of files have more coverage
20   105   12   2.86
10   43   0.6   7
7     1.71  
1    
 
  DateField       Line # 43 20 12 45.9% 0.45945945
 
  (2)
 
1    package org.apache.lucene.document;
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 org.apache.lucene.search.PrefixQuery;
20    import org.apache.lucene.search.RangeQuery;
21   
22    import java.util.Date; // for javadoc
23   
24    /**
25    * Provides support for converting dates to strings and vice-versa.
26    * The strings are structured so that lexicographic sorting orders by date,
27    * which makes them suitable for use as field values and search terms.
28    *
29    * <P>Note that this class saves dates with millisecond granularity,
30    * which is bad for {@link RangeQuery} and {@link PrefixQuery}, as those
31    * queries are expanded to a BooleanQuery with a potentially large number
32    * of terms when searching. Thus you might want to use
33    * {@link DateTools} instead.
34    *
35    * <P>
36    * Note: dates before 1970 cannot be used, and therefore cannot be
37    * indexed when using this class. See {@link DateTools} for an
38    * alternative without such a limitation.
39    *
40    * @deprecated If you build a new index, use {@link DateTools} instead. This class is included for use with existing
41    * indices and will be removed in a future release.
42    */
 
43    public class DateField {
44   
 
45  0 toggle private DateField() {}
46   
47    // make date strings long enough to last a millenium
48    private static int DATE_LEN = Long.toString(1000L*365*24*60*60*1000,
49    Character.MAX_RADIX).length();
50   
 
51  0 toggle public static String MIN_DATE_STRING() {
52  0 return timeToString(0);
53    }
54   
 
55  0 toggle public static String MAX_DATE_STRING() {
56  0 char[] buffer = new char[DATE_LEN];
57  0 char c = Character.forDigit(Character.MAX_RADIX-1, Character.MAX_RADIX);
58  0 for (int i = 0 ; i < DATE_LEN; i++)
59  0 buffer[i] = c;
60  0 return new String(buffer);
61    }
62   
63    /**
64    * Converts a Date to a string suitable for indexing.
65    * @throws RuntimeException if the date specified in the
66    * method argument is before 1970
67    */
 
68  22 toggle public static String dateToString(Date date) {
69  22 return timeToString(date.getTime());
70    }
71    /**
72    * Converts a millisecond time to a string suitable for indexing.
73    * @throws RuntimeException if the time specified in the
74    * method argument is negative, that is, before 1970
75    */
 
76  22 toggle public static String timeToString(long time) {
77  22 if (time < 0)
78  0 throw new RuntimeException("time '" + time + "' is too early, must be >= 0");
79   
80  22 String s = Long.toString(time, Character.MAX_RADIX);
81   
82  22 if (s.length() > DATE_LEN)
83  0 throw new RuntimeException("time '" + time + "' is too late, length of string " +
84    "representation must be <= " + DATE_LEN);
85   
86    // Pad with leading zeros
87  22 if (s.length() < DATE_LEN) {
88  22 StringBuffer sb = new StringBuffer(s);
89  44 while (sb.length() < DATE_LEN)
90  22 sb.insert(0, 0);
91  22 s = sb.toString();
92    }
93   
94  22 return s;
95    }
96   
97    /** Converts a string-encoded date into a millisecond time. */
 
98  0 toggle public static long stringToTime(String s) {
99  0 return Long.parseLong(s, Character.MAX_RADIX);
100    }
101    /** Converts a string-encoded date into a Date object. */
 
102  0 toggle public static Date stringToDate(String s) {
103  0 return new Date(stringToTime(s));
104    }
105    }