KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lucene > document > NumberTools


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 JavaDoc MIN_STRING_VALUE = NEGATIVE_PREFIX
49             + "0000000000000";
50
51     /**
52      * Equivalent to longToString(Long.MAX_VALUE)
53      */

54     public static final String JavaDoc 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     public static String JavaDoc longToString(long l) {
66
67         if (l == Long.MIN_VALUE) {
68             // special case, because long is not symetric around zero
69
return MIN_STRING_VALUE;
70         }
71
72         StringBuffer JavaDoc buf = new StringBuffer JavaDoc(STR_SIZE);
73
74         if (l < 0) {
75             buf.append(NEGATIVE_PREFIX);
76             l = Long.MAX_VALUE + l + 1;
77         } else {
78             buf.append(POSITIVE_PREFIX);
79         }
80         String JavaDoc num = Long.toString(l, RADIX);
81
82         int padLen = STR_SIZE - num.length() - buf.length();
83         while (padLen-- > 0) {
84             buf.append('0');
85         }
86         buf.append(num);
87
88         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     public static long stringToLong(String JavaDoc str) {
102         if (str == null) {
103             throw new NullPointerException JavaDoc("string cannot be null");
104         }
105         if (str.length() != STR_SIZE) {
106             throw new NumberFormatException JavaDoc("string is the wrong size");
107         }
108
109         if (str.equals(MIN_STRING_VALUE)) {
110             return Long.MIN_VALUE;
111         }
112
113         char prefix = str.charAt(0);
114         long l = Long.parseLong(str.substring(1), RADIX);
115
116         if (prefix == POSITIVE_PREFIX) {
117             // nop
118
} else if (prefix == NEGATIVE_PREFIX) {
119             l = l - Long.MAX_VALUE - 1;
120         } else {
121             throw new NumberFormatException JavaDoc(
122                     "string does not begin with the correct prefix");
123         }
124
125         return l;
126     }
127 }
Popular Tags