2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
20 using NumericUtils = Mono.Lucene.Net.Util.NumericUtils;
21 using NumericRangeQuery = Mono.Lucene.Net.Search.NumericRangeQuery;
23 namespace Mono.Lucene.Net.Documents
26 // do not remove this class in 3.0, it may be needed to decode old indexes!
28 /// <summary> Provides support for converting longs to Strings, and back again. The strings
29 /// are structured so that lexicographic sorting order is preserved.
32 /// That is, if l1 is less than l2 for any two longs l1 and l2, then
33 /// NumberTools.longToString(l1) is lexicographically less than
34 /// NumberTools.longToString(l2). (Similarly for "greater than" and "equals".)
37 /// This class handles <b>all</b> long values (unlike
38 /// {@link Mono.Lucene.Net.Documents.DateField}).
41 /// <deprecated> For new indexes use {@link NumericUtils} instead, which
42 /// provides a sortable binary representation (prefix encoded) of numeric
44 /// To index and efficiently query numeric values use {@link NumericField}
45 /// and {@link NumericRangeQuery}.
46 /// This class is included for use with existing
47 /// indices and will be removed in a future release.
49 [Obsolete("For new indexes use NumericUtils instead, which provides a sortable binary representation (prefix encoded) of numeric values. To index and efficiently query numeric values use NumericField and NumericRangeQuery. This class is included for use with existing indices and will be removed in a future release.")]
50 public class NumberTools
53 private const int RADIX = 36;
55 private const char NEGATIVE_PREFIX = '-';
57 // NB: NEGATIVE_PREFIX must be < POSITIVE_PREFIX
58 private const char POSITIVE_PREFIX = '0';
60 //NB: this must be less than
61 /// <summary> Equivalent to longToString(Long.MIN_VALUE)</summary>
62 #if !PRE_LUCENE_NET_2_0_0_COMPATIBLE
63 public static readonly System.String MIN_STRING_VALUE = NEGATIVE_PREFIX + "0000000000000";
65 public static readonly System.String MIN_STRING_VALUE = NEGATIVE_PREFIX + "0000000000000000";
68 /// <summary> Equivalent to longToString(Long.MAX_VALUE)</summary>
69 #if !PRE_LUCENE_NET_2_0_0_COMPATIBLE
70 public static readonly System.String MAX_STRING_VALUE = POSITIVE_PREFIX + "1y2p0ij32e8e7";
72 public static readonly System.String MAX_STRING_VALUE = POSITIVE_PREFIX + "7fffffffffffffff";
75 /// <summary> The length of (all) strings returned by {@link #longToString}</summary>
76 public static readonly int STR_SIZE = MIN_STRING_VALUE.Length;
78 /// <summary> Converts a long to a String suitable for indexing.</summary>
79 public static System.String LongToString(long l)
82 if (l == System.Int64.MinValue)
84 // special case, because long is not symmetric around zero
85 return MIN_STRING_VALUE;
88 System.Text.StringBuilder buf = new System.Text.StringBuilder(STR_SIZE);
92 buf.Append(NEGATIVE_PREFIX);
93 l = System.Int64.MaxValue + l + 1;
97 buf.Append(POSITIVE_PREFIX);
99 #if !PRE_LUCENE_NET_2_0_0_COMPATIBLE
100 System.String num = ToString(l);
102 System.String num = System.Convert.ToString(l, RADIX);
105 int padLen = STR_SIZE - num.Length - buf.Length;
112 return buf.ToString();
115 /// <summary> Converts a String that was returned by {@link #longToString} back to a
119 /// <throws> IllegalArgumentException </throws>
120 /// <summary> if the input is null
122 /// <throws> NumberFormatException </throws>
123 /// <summary> if the input does not parse (it was not a String returned by
126 public static long StringToLong(System.String str)
130 throw new System.NullReferenceException("string cannot be null");
132 if (str.Length != STR_SIZE)
134 throw new System.FormatException("string is the wrong size");
137 if (str.Equals(MIN_STRING_VALUE))
139 return System.Int64.MinValue;
142 char prefix = str[0];
143 #if !PRE_LUCENE_NET_2_0_0_COMPATIBLE
144 long l = ToLong(str.Substring(1));
146 long l = System.Convert.ToInt64(str.Substring(1), RADIX);
149 if (prefix == POSITIVE_PREFIX)
153 else if (prefix == NEGATIVE_PREFIX)
155 l = l - System.Int64.MaxValue - 1;
159 throw new System.FormatException("string does not begin with the correct prefix");
165 #if !PRE_LUCENE_NET_2_0_0_COMPATIBLE
167 static System.String digits = "0123456789abcdefghijklmnopqrstuvwxyz";
168 static long[] powersOf36 =
176 36L*36L*36L*36L*36L*36L,
177 36L*36L*36L*36L*36L*36L*36L,
178 36L*36L*36L*36L*36L*36L*36L*36L,
179 36L*36L*36L*36L*36L*36L*36L*36L*36L,
180 36L*36L*36L*36L*36L*36L*36L*36L*36L*36L,
181 36L*36L*36L*36L*36L*36L*36L*36L*36L*36L*36L,
182 36L*36L*36L*36L*36L*36L*36L*36L*36L*36L*36L*36L
185 public static System.String ToString(long lval)
192 int maxStrLen = powersOf36.Length;
195 char[] tb = new char[maxStrLen];
197 for (int i = 0; i < maxStrLen; i++)
199 long pval = powersOf36[maxStrLen - i - 1];
200 int pos = (int)(curval / pval);
201 tb[outpos++] = digits.Substring(pos, 1).ToCharArray()[0];
202 curval = curval % pval;
206 return new System.String(tb, 0, outpos).TrimStart('0');
209 public static long ToLong(System.String t)
212 char[] tb = t.ToCharArray();
213 for (int i = 0; i < tb.Length; i++)
215 ival += powersOf36[i] * digits.IndexOf(tb[tb.Length - i - 1]);