01: /*
02: * NumberStringCache.java
03: *
04: * This file is part of SQL Workbench/J, http://www.sql-workbench.net
05: *
06: * Copyright 2002-2008, Thomas Kellerer
07: * No part of this code maybe reused without the permission of the author
08: *
09: * To contact the author please send an email to: support@sql-workbench.net
10: *
11: */
12: package workbench.util;
13:
14: /**
15: *
16: * @author support@sql-workbench.net
17: */
18: public class NumberStringCache {
19: // As this is class is used to cache the String representation for
20: // Line numbers in the editor, caching 5000 numbers should suffice
21: // for most cases.
22: public static final int CACHE_SIZE = 5000;
23: private static final String[] cache = new String[CACHE_SIZE];
24: private static final String[] hexCache = new String[256];
25:
26: private NumberStringCache() {
27: }
28:
29: public static String getHexString(int value) {
30: if (value > 255 || value < 0)
31: return Integer.toHexString(value);
32: if (hexCache[value] == null) {
33: if (value < 16) {
34: hexCache[value] = "0" + Integer.toHexString(value);
35: } else {
36: hexCache[value] = Integer.toHexString(value);
37: }
38:
39: }
40: return hexCache[value];
41: }
42:
43: public static String getNumberString(long lvalue) {
44: if (lvalue < 0 || lvalue >= CACHE_SIZE)
45: return Long.toString(lvalue);
46:
47: int value = (int) lvalue;
48: // I'm not synchronizing this, because the worst that can
49: // happen is, that the same number is created two or three times
50: // instead of exactly one time.
51: // And as this is most of the time called from Swing Event Thread
52: // it is more or less a single-threaded access anyway.
53: if (cache[value] == null) {
54: cache[value] = Integer.toString(value);
55: }
56: return cache[value];
57: }
58:
59: }
|