Skip to content

Commit b5d77b7

Browse files
committed
Create NumbersCache.java
A cache for strings of integers
1 parent 5b42345 commit b5d77b7

File tree

1 file changed

+74
-0
lines changed

1 file changed

+74
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*******************************************************************************
2+
* Copyright (c) quickfixengine.org All rights reserved.
3+
*
4+
* This file is part of the QuickFIX FIX Engine
5+
*
6+
* This file may be distributed under the terms of the quickfixengine.org
7+
* license as defined by quickfixengine.org and appearing in the file
8+
* LICENSE included in the packaging of this file.
9+
*
10+
* This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING
11+
* THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A
12+
* PARTICULAR PURPOSE.
13+
*
14+
* See http://www.quickfixengine.org/LICENSE for licensing information.
15+
*
16+
* Contact [email protected] if any conditions of this licensing
17+
* are not clear to you.
18+
******************************************************************************/
19+
20+
package quickfix;
21+
22+
import java.util.ArrayList;
23+
24+
/**
25+
* A cache for commonly used string representing numbers.
26+
* Hold values from 0 to 999999 and from 1000 to 200 000 000 by step of 1000
27+
*/
28+
public final class NumbersCache {
29+
30+
private static final int littleNumbersLength = 1000000;
31+
private static final int bigNumbersLength = 200000;
32+
private static final int bigNumbersOffset = 1000;
33+
private static final int bigNumbersMax = bigNumbersLength * bigNumbersOffset;
34+
35+
public static final ArrayList<String> littleNumbers;
36+
public static final ArrayList<String> bigNumbers;
37+
38+
static {
39+
littleNumbers = new ArrayList<String>(littleNumbersLength);
40+
bigNumbers = new ArrayList<String>(bigNumbersLength);
41+
for (int i = 0; i < littleNumbersLength; i++)
42+
littleNumbers.add(Integer.toString(i));
43+
for (long i = 0; i < bigNumbersLength;)
44+
bigNumbers.add(Long.toString(++i * bigNumbersOffset));
45+
46+
}
47+
48+
/**
49+
* Get the string representing the given number
50+
*
51+
* @param i the long to convert
52+
* @return the String representing the long
53+
*/
54+
public static String get(long i) {
55+
if (i < littleNumbersLength)
56+
return littleNumbers.get((int)i);
57+
if (i <= bigNumbersMax && i % bigNumbersOffset == 0)
58+
return bigNumbers.get((int)(i/bigNumbersOffset)-1);
59+
return String.valueOf(i);
60+
}
61+
62+
/**
63+
* Get the string representing the given double if it's an integer
64+
*
65+
* @param d the double to convert
66+
* @return the String representing the double or null if the double is not an integer
67+
*/
68+
public static String get(double d) {
69+
long l = (long)d;
70+
if (d == (double)l)
71+
return get(l);
72+
return null;
73+
}
74+
}

0 commit comments

Comments
 (0)