Clover Coverage Report
Coverage timestamp: Fri May 9 2008 10:54:27 EST
../../../../img/srcFileCovDistChart9.png 37% of files have more coverage
26   127   10   13
16   50   0.38   2
2     5  
1    
 
  NumberTools       Line # 35 26 10 86.4% 0.8636364
 
  (3)
 
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   
20    /**
21    * Provides support for converting longs to Strings, and back again. The strings
22    * are structured so that lexicographic sorting order is preserved.
23    *
24    * <p>
25    * That is, if l1 is less than l2 for any two longs l1 and l2, then
26    * NumberTools.longToString(l1) is lexicographically less than
27    * NumberTools.longToString(l2). (Similarly for "greater than" and "equals".)
28    *
29    * <p>
30    * This class handles <b>all</b> long values (unlike
31    * {@link org.apache.lucene.document.DateField}).
32    *
33    * @author Matt Quail (spud at madbean dot com)
34    */
 
35    public class NumberTools {
36   
37    private static final int RADIX = 36;
38   
39    private static final char NEGATIVE_PREFIX = '-';
40   
41    // NB: NEGATIVE_PREFIX must be < POSITIVE_PREFIX
42    private static final char POSITIVE_PREFIX = '0';
43   
44    //NB: this must be less than
45    /**
46    * Equivalent to longToString(Long.MIN_VALUE)
47    */
48    public static final String MIN_STRING_VALUE = NEGATIVE_PREFIX
49    + "0000000000000";
50   
51    /**
52    * Equivalent to longToString(Long.MAX_VALUE)
53    */
54    public static final String MAX_STRING_VALUE = POSITIVE_PREFIX
55    + "1y2p0ij32e8e7";
56   
57    /**
58    * The length of (all) strings returned by {@link #longToString}
59    */
60    public static final int STR_SIZE = MIN_STRING_VALUE.length();
61   
62    /**
63    * Converts a long to a String suitable for indexing.
64    */
 
65  120804 toggle public static String longToString(long l) {
66   
67  120804 if (l == Long.MIN_VALUE) {
68    // special case, because long is not symetric around zero
69  2 return MIN_STRING_VALUE;
70    }
71   
72  120802 StringBuffer buf = new StringBuffer(STR_SIZE);
73   
74  120802 if (l < 0) {
75  60199 buf.append(NEGATIVE_PREFIX);
76  60199 l = Long.MAX_VALUE + l + 1;
77    } else {
78  60603 buf.append(POSITIVE_PREFIX);
79    }
80  120802 String num = Long.toString(l, RADIX);
81   
82  120802 int padLen = STR_SIZE - num.length() - buf.length();
83  784546 while (padLen-- > 0) {
84  663744 buf.append('0');
85    }
86  120802 buf.append(num);
87   
88  120802 return buf.toString();
89    }
90   
91    /**
92    * Converts a String that was returned by {@link #longToString} back to a
93    * long.
94    *
95    * @throws IllegalArgumentException
96    * if the input is null
97    * @throws NumberFormatException
98    * if the input does not parse (it was not a String returned by
99    * longToString()).
100    */
 
101  120804 toggle public static long stringToLong(String str) {
102  120804 if (str == null) {
103  0 throw new NullPointerException("string cannot be null");
104    }
105  120804 if (str.length() != STR_SIZE) {
106  0 throw new NumberFormatException("string is the wrong size");
107    }
108   
109  120804 if (str.equals(MIN_STRING_VALUE)) {
110  2 return Long.MIN_VALUE;
111    }
112   
113  120802 char prefix = str.charAt(0);
114  120802 long l = Long.parseLong(str.substring(1), RADIX);
115   
116  120802 if (prefix == POSITIVE_PREFIX) {
117    // nop
118  60199 } else if (prefix == NEGATIVE_PREFIX) {
119  60199 l = l - Long.MAX_VALUE - 1;
120    } else {
121  0 throw new NumberFormatException(
122    "string does not begin with the correct prefix");
123    }
124   
125  120802 return l;
126    }
127    }