18
18
19
19
public class RopeConstants {
20
20
21
+ public static final Map <String , LeafRope > ROPE_CONSTANTS = new HashMap <>();
22
+
21
23
public static final byte [] EMPTY_BYTES = new byte [0 ];
22
24
23
- public static final LeafRope EMPTY_ASCII_8BIT_ROPE = new AsciiOnlyLeafRope ( EMPTY_BYTES , ASCIIEncoding . INSTANCE )
24
- . computeHashCode ( );
25
- public static final LeafRope EMPTY_US_ASCII_ROPE = new AsciiOnlyLeafRope ( EMPTY_BYTES , USASCIIEncoding . INSTANCE )
26
- . computeHashCode ( );
27
- public static final LeafRope EMPTY_UTF8_ROPE = new AsciiOnlyLeafRope ( EMPTY_BYTES , UTF8Encoding . INSTANCE )
28
- . computeHashCode ( );
25
+ public static final LeafRope EMPTY_ASCII_8BIT_ROPE = withHashCode (
26
+ new AsciiOnlyLeafRope ( EMPTY_BYTES , ASCIIEncoding . INSTANCE ) );
27
+ public static final LeafRope EMPTY_US_ASCII_ROPE = withHashCode (
28
+ new AsciiOnlyLeafRope ( EMPTY_BYTES , USASCIIEncoding . INSTANCE ) );
29
+ public static final LeafRope EMPTY_UTF8_ROPE = withHashCode (
30
+ new AsciiOnlyLeafRope ( EMPTY_BYTES , UTF8Encoding . INSTANCE ) );
29
31
30
32
public static final LeafRope [] UTF8_SINGLE_BYTE_ROPES = new LeafRope [256 ];
31
33
public static final LeafRope [] US_ASCII_SINGLE_BYTE_ROPES = new LeafRope [256 ];
32
34
public static final LeafRope [] ASCII_8BIT_SINGLE_BYTE_ROPES = new LeafRope [256 ];
33
- static final Map <String , LeafRope > ROPE_CONSTANTS = new HashMap <>();
34
35
35
36
static {
36
37
for (int i = 0 ; i < 128 ; i ++) {
37
38
final byte [] bytes = new byte []{ (byte ) i };
38
39
39
- UTF8_SINGLE_BYTE_ROPES [i ] = new AsciiOnlyLeafRope (bytes , UTF8Encoding .INSTANCE ). computeHashCode ( );
40
- US_ASCII_SINGLE_BYTE_ROPES [i ] = new AsciiOnlyLeafRope (bytes , USASCIIEncoding .INSTANCE ). computeHashCode ( );
41
- ASCII_8BIT_SINGLE_BYTE_ROPES [i ] = new AsciiOnlyLeafRope (bytes , ASCIIEncoding .INSTANCE ). computeHashCode ( );
40
+ UTF8_SINGLE_BYTE_ROPES [i ] = withHashCode ( new AsciiOnlyLeafRope (bytes , UTF8Encoding .INSTANCE ));
41
+ US_ASCII_SINGLE_BYTE_ROPES [i ] = withHashCode ( new AsciiOnlyLeafRope (bytes , USASCIIEncoding .INSTANCE ));
42
+ ASCII_8BIT_SINGLE_BYTE_ROPES [i ] = withHashCode ( new AsciiOnlyLeafRope (bytes , ASCIIEncoding .INSTANCE ));
42
43
}
43
44
44
45
for (int i = 128 ; i < 256 ; i ++) {
45
46
final byte [] bytes = new byte []{ (byte ) i };
46
47
47
- UTF8_SINGLE_BYTE_ROPES [i ] = new InvalidLeafRope (bytes , UTF8Encoding .INSTANCE , 1 ). computeHashCode ( );
48
- US_ASCII_SINGLE_BYTE_ROPES [i ] = new InvalidLeafRope (bytes , USASCIIEncoding .INSTANCE , 1 ). computeHashCode ( );
49
- ASCII_8BIT_SINGLE_BYTE_ROPES [i ] = new ValidLeafRope (bytes , ASCIIEncoding .INSTANCE , 1 ). computeHashCode ( );
48
+ UTF8_SINGLE_BYTE_ROPES [i ] = withHashCode ( new InvalidLeafRope (bytes , UTF8Encoding .INSTANCE , 1 ));
49
+ US_ASCII_SINGLE_BYTE_ROPES [i ] = withHashCode ( new InvalidLeafRope (bytes , USASCIIEncoding .INSTANCE , 1 ));
50
+ ASCII_8BIT_SINGLE_BYTE_ROPES [i ] = withHashCode ( new ValidLeafRope (bytes , ASCIIEncoding .INSTANCE , 1 ));
50
51
}
51
52
}
52
53
@@ -109,7 +110,7 @@ private static Rope ascii(String string) {
109
110
return US_ASCII_SINGLE_BYTE_ROPES [string .charAt (0 )];
110
111
} else {
111
112
final byte [] bytes = RopeOperations .encodeAsciiBytes (string );
112
- final LeafRope rope = new AsciiOnlyLeafRope (bytes , USASCIIEncoding .INSTANCE ). computeHashCode ( );
113
+ final LeafRope rope = withHashCode ( new AsciiOnlyLeafRope (bytes , USASCIIEncoding .INSTANCE ));
113
114
final Rope existing = ROPE_CONSTANTS .putIfAbsent (string , rope );
114
115
if (existing != null ) {
115
116
throw new AssertionError ("Duplicate Rope in RopeConstants: " + existing );
@@ -126,4 +127,50 @@ public static LeafRope lookupUSASCII(String string) {
126
127
}
127
128
}
128
129
130
+ private static final LeafRope [] PADDED_NUMBERS = createPaddedNumbersTable ();
131
+
132
+ private static LeafRope [] createPaddedNumbersTable () {
133
+ final LeafRope [] table = new LeafRope [100 ];
134
+
135
+ for (int n = 0 ; n < table .length ; n ++) {
136
+ table [n ] = new AsciiOnlyLeafRope (
137
+ new byte []{ (byte ) ('0' + n / 10 ), (byte ) ('0' + n % 10 ) },
138
+ UTF8Encoding .INSTANCE );
139
+ }
140
+
141
+ return table ;
142
+ }
143
+
144
+ /*** Zero-padded numbers in the format %02d, between 00 and 99. */
145
+ public static LeafRope paddedNumber (int n ) {
146
+ return PADDED_NUMBERS [n ];
147
+ }
148
+
149
+ private static final LeafRope [] PADDING_ZEROS = createPaddingZeroTable ();
150
+
151
+ private static LeafRope [] createPaddingZeroTable () {
152
+ final LeafRope [] table = new LeafRope [6 ];
153
+
154
+ for (int n = 0 ; n < table .length ; n ++) {
155
+ final byte [] bytes = new byte [n ];
156
+
157
+ for (int i = 0 ; i < bytes .length ; i ++) {
158
+ bytes [i ] = '0' ;
159
+ }
160
+
161
+ table [n ] = new AsciiOnlyLeafRope (bytes , UTF8Encoding .INSTANCE );
162
+ }
163
+
164
+ return table ;
165
+ }
166
+
167
+ public static LeafRope paddingZeros (int n ) {
168
+ return PADDING_ZEROS [n ];
169
+ }
170
+
171
+ private static <T > T withHashCode (T object ) {
172
+ object .hashCode ();
173
+ return object ;
174
+ }
175
+
129
176
}
0 commit comments