1
1
/*
2
- * Copyright (c) 2020, 2021, Oracle and/or its affiliates. All rights reserved.
2
+ * Copyright (c) 2020, 2024, Oracle and/or its affiliates. All rights reserved.
3
+ * Copyright (c) 2024, Alibaba Group Holding Limited. All rights reserved.
3
4
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
5
*
5
6
* This code is free software; you can redistribute it and/or modify it
@@ -158,37 +159,32 @@ public final class HexFormat {
158
159
* The hexadecimal characters are from lowercase alpha digits.
159
160
*/
160
161
private static final HexFormat HEX_FORMAT =
161
- new HexFormat ("" , "" , "" , Case . LOWERCASE );
162
+ new HexFormat ("" , "" , "" , false );
162
163
163
164
private static final HexFormat HEX_UPPER_FORMAT =
164
- new HexFormat ("" , "" , "" , Case . UPPERCASE );
165
+ new HexFormat ("" , "" , "" , true );
165
166
166
167
private static final byte [] EMPTY_BYTES = {};
167
168
168
169
private final String delimiter ;
169
170
private final String prefix ;
170
171
private final String suffix ;
171
- private final Case digitCase ;
172
-
173
- private enum Case {
174
- LOWERCASE ,
175
- UPPERCASE
176
- }
172
+ private final boolean ucase ;
177
173
178
174
/**
179
175
* Returns a HexFormat with a delimiter, prefix, suffix, and array of digits.
180
176
*
181
177
* @param delimiter a delimiter, non-null
182
178
* @param prefix a prefix, non-null
183
179
* @param suffix a suffix, non-null
184
- * @param digitCase enum indicating how to case digits
180
+ * @param ucase enum indicating how to case digits
185
181
* @throws NullPointerException if any argument is null
186
182
*/
187
- private HexFormat (String delimiter , String prefix , String suffix , Case digitCase ) {
183
+ private HexFormat (String delimiter , String prefix , String suffix , boolean ucase ) {
188
184
this .delimiter = Objects .requireNonNull (delimiter , "delimiter" );
189
185
this .prefix = Objects .requireNonNull (prefix , "prefix" );
190
186
this .suffix = Objects .requireNonNull (suffix , "suffix" );
191
- this .digitCase = digitCase ;
187
+ this .ucase = ucase ;
192
188
}
193
189
194
190
/**
@@ -217,7 +213,7 @@ public static HexFormat of() {
217
213
* @return a {@link HexFormat} with the delimiter and lowercase characters
218
214
*/
219
215
public static HexFormat ofDelimiter (String delimiter ) {
220
- return new HexFormat (delimiter , "" , "" , Case . LOWERCASE );
216
+ return new HexFormat (delimiter , "" , "" , false );
221
217
}
222
218
223
219
/**
@@ -226,7 +222,7 @@ public static HexFormat ofDelimiter(String delimiter) {
226
222
* @return a copy of this {@code HexFormat} with the delimiter
227
223
*/
228
224
public HexFormat withDelimiter (String delimiter ) {
229
- return new HexFormat (delimiter , this .prefix , this .suffix , this .digitCase );
225
+ return new HexFormat (delimiter , this .prefix , this .suffix , this .ucase );
230
226
}
231
227
232
228
/**
@@ -236,7 +232,7 @@ public HexFormat withDelimiter(String delimiter) {
236
232
* @return a copy of this {@code HexFormat} with the prefix
237
233
*/
238
234
public HexFormat withPrefix (String prefix ) {
239
- return new HexFormat (this .delimiter , prefix , this .suffix , this .digitCase );
235
+ return new HexFormat (this .delimiter , prefix , this .suffix , this .ucase );
240
236
}
241
237
242
238
/**
@@ -246,7 +242,7 @@ public HexFormat withPrefix(String prefix) {
246
242
* @return a copy of this {@code HexFormat} with the suffix
247
243
*/
248
244
public HexFormat withSuffix (String suffix ) {
249
- return new HexFormat (this .delimiter , this .prefix , suffix , this .digitCase );
245
+ return new HexFormat (this .delimiter , this .prefix , suffix , this .ucase );
250
246
}
251
247
252
248
/**
@@ -258,7 +254,7 @@ public HexFormat withSuffix(String suffix) {
258
254
public HexFormat withUpperCase () {
259
255
if (this == HEX_FORMAT )
260
256
return HEX_UPPER_FORMAT ;
261
- return new HexFormat (this .delimiter , this .prefix , this .suffix , Case . UPPERCASE );
257
+ return new HexFormat (this .delimiter , this .prefix , this .suffix , true );
262
258
}
263
259
264
260
/**
@@ -268,7 +264,7 @@ public HexFormat withUpperCase() {
268
264
* @return a copy of this {@code HexFormat} with lowercase hexadecimal characters
269
265
*/
270
266
public HexFormat withLowerCase () {
271
- return new HexFormat (this .delimiter , this .prefix , this .suffix , Case . LOWERCASE );
267
+ return new HexFormat (this .delimiter , this .prefix , this .suffix , false );
272
268
}
273
269
274
270
/**
@@ -306,7 +302,7 @@ public String suffix() {
306
302
* otherwise {@code false}
307
303
*/
308
304
public boolean isUpperCase () {
309
- return digitCase == Case . UPPERCASE ;
305
+ return ucase ;
310
306
}
311
307
312
308
/**
@@ -436,7 +432,6 @@ private String formatOptDelimiter(byte[] bytes, int fromIndex, int toIndex) {
436
432
return null ;
437
433
}
438
434
439
- boolean ucase = digitCase == Case .UPPERCASE ;
440
435
int length = toIndex - fromIndex ;
441
436
if (delimiter .isEmpty ()) {
442
437
// Allocate the byte array and fill in the hex pairs for each byte
@@ -637,7 +632,7 @@ public char toLowHexDigit(int value) {
637
632
if (value < 10 ) {
638
633
return (char )('0' + value );
639
634
}
640
- if (digitCase == Case . LOWERCASE ) {
635
+ if (! ucase ) {
641
636
return (char )('a' - 10 + value );
642
637
}
643
638
return (char )('A' - 10 + value );
@@ -658,7 +653,7 @@ public char toHighHexDigit(int value) {
658
653
if (value < 10 ) {
659
654
return (char )('0' + value );
660
655
}
661
- if (digitCase == Case . LOWERCASE ) {
656
+ if (! ucase ) {
662
657
return (char )('a' - 10 + value );
663
658
}
664
659
return (char )('A' - 10 + value );
@@ -1067,7 +1062,7 @@ public boolean equals(Object o) {
1067
1062
if (o == null || getClass () != o .getClass ())
1068
1063
return false ;
1069
1064
HexFormat otherHex = (HexFormat ) o ;
1070
- return digitCase == otherHex .digitCase &&
1065
+ return ucase == otherHex .ucase &&
1071
1066
delimiter .equals (otherHex .delimiter ) &&
1072
1067
prefix .equals (otherHex .prefix ) &&
1073
1068
suffix .equals (otherHex .suffix );
@@ -1081,7 +1076,7 @@ public boolean equals(Object o) {
1081
1076
@ Override
1082
1077
public int hashCode () {
1083
1078
int result = Objects .hash (delimiter , prefix , suffix );
1084
- result = 31 * result + Boolean .hashCode (digitCase == Case . UPPERCASE );
1079
+ result = 31 * result + Boolean .hashCode (ucase );
1085
1080
return result ;
1086
1081
}
1087
1082
@@ -1093,7 +1088,7 @@ public int hashCode() {
1093
1088
*/
1094
1089
@ Override
1095
1090
public String toString () {
1096
- return escapeNL ("uppercase: " + ( digitCase == Case . UPPERCASE ) +
1091
+ return escapeNL ("uppercase: " + ucase +
1097
1092
", delimiter: \" " + delimiter +
1098
1093
"\" , prefix: \" " + prefix +
1099
1094
"\" , suffix: \" " + suffix + "\" " );
0 commit comments