configure.in, AssemblyInfo.cs: For those unfortunate earthlings without libchm, libwv...
[beagle.git] / beagled / Lucene.Net / Document / NumberTools.cs
blob0900cf192822d893d0c8a05b23a175b13cf45a71
1 /*
2 * Copyright 2004 The Apache Software Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 using System;
19 namespace Lucene.Net.Documents
22 /// <summary> Provides support for converting longs to Strings, and back again. The strings
23 /// are structured so that lexicographic sorting order is preserved.
24 ///
25 /// <p>
26 /// That is, if l1 is less than l2 for any two longs l1 and l2, then
27 /// NumberTools.longToString(l1) is lexicographically less than
28 /// NumberTools.longToString(l2). (Similarly for "greater than" and "equals".)
29 ///
30 /// <p>
31 /// This class handles <b>all</b> long values (unlike
32 /// {@link Lucene.Net.document.DateField}).
33 ///
34 /// </summary>
35 /// <author> Matt Quail (spud at madbean dot com)
36 /// </author>
37 public class NumberTools
40 private const int RADIX = 16;
42 private const char NEGATIVE_PREFIX = '-';
44 // NB: NEGATIVE_PREFIX must be < POSITIVE_PREFIX
45 private const char POSITIVE_PREFIX = '0';
47 //NB: this must be less than
48 /// <summary> Equivalent to longToString(Long.MIN_VALUE)</summary>
49 public static readonly System.String MIN_STRING_VALUE = NEGATIVE_PREFIX + "0000000000000000";
51 /// <summary> Equivalent to longToString(Long.MAX_VALUE)</summary>
52 public static readonly System.String MAX_STRING_VALUE = POSITIVE_PREFIX + "7fffffffffffffff";
54 /// <summary> The length of (all) strings returned by {@link #longToString}</summary>
55 public static readonly int STR_SIZE = MIN_STRING_VALUE.Length;
57 /// <summary> Converts a long to a String suitable for indexing.</summary>
58 public static System.String LongToString(long l)
60 if (l == System.Int64.MinValue)
62 // special case, because long is not symetric around zero
63 return MIN_STRING_VALUE;
66 System.Text.StringBuilder buf = new System.Text.StringBuilder(STR_SIZE);
68 if (l < 0)
70 buf.Append(NEGATIVE_PREFIX);
71 l = System.Int64.MaxValue + l + 1;
73 else
75 buf.Append(POSITIVE_PREFIX);
77 System.String num = System.Convert.ToString(l, RADIX);
79 int padLen = STR_SIZE - num.Length - buf.Length;
80 while (padLen-- > 0)
82 buf.Append('0');
84 buf.Append(num);
86 return buf.ToString();
89 /// <summary> Converts a String that was returned by {@link #longToString} back to a
90 /// long.
91 ///
92 /// </summary>
93 /// <throws> IllegalArgumentException </throws>
94 /// <summary> if the input is null
95 /// </summary>
96 /// <throws> NumberFormatException </throws>
97 /// <summary> if the input does not parse (it was not a String returned by
98 /// longToString()).
99 /// </summary>
100 public static long StringToLong(System.String str)
102 if (str == null)
104 throw new System.NullReferenceException("string cannot be null");
106 if (str.Length != STR_SIZE)
108 throw new System.FormatException("string is the wrong size");
111 if (str.Equals(MIN_STRING_VALUE))
113 return System.Int64.MinValue;
116 char prefix = str[0];
117 long l = System.Convert.ToInt64(str.Substring(1), RADIX);
119 if (prefix == POSITIVE_PREFIX)
121 // nop
123 else if (prefix == NEGATIVE_PREFIX)
125 l = l - System.Int64.MaxValue - 1;
127 else
129 throw new System.FormatException("string does not begin with the correct prefix");
132 return l;