2 * Copyright 2004 The Apache Software Foundation
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
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.
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".)
31 /// This class handles <b>all</b> long values (unlike
32 /// {@link Lucene.Net.document.DateField}).
35 /// <author> Matt Quail (spud at madbean dot com)
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
);
70 buf
.Append(NEGATIVE_PREFIX
);
71 l
= System
.Int64
.MaxValue
+ l
+ 1;
75 buf
.Append(POSITIVE_PREFIX
);
77 System
.String num
= System
.Convert
.ToString(l
, RADIX
);
79 int padLen
= STR_SIZE
- num
.Length
- buf
.Length
;
86 return buf
.ToString();
89 /// <summary> Converts a String that was returned by {@link #longToString} back to a
93 /// <throws> IllegalArgumentException </throws>
94 /// <summary> if the input is null
96 /// <throws> NumberFormatException </throws>
97 /// <summary> if the input does not parse (it was not a String returned by
100 public static long StringToLong(System
.String str
)
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
)
123 else if (prefix
== NEGATIVE_PREFIX
)
125 l
= l
- System
.Int64
.MaxValue
- 1;
129 throw new System
.FormatException("string does not begin with the correct prefix");