Skip to content

Commit 84c74ad

Browse files
wenshaoliach
authored andcommitted
8335802: Improve startup speed HexFormat uses boolean instead of enum
Reviewed-by: liach
1 parent 4f312d6 commit 84c74ad

File tree

1 file changed

+20
-25
lines changed

1 file changed

+20
-25
lines changed

src/java.base/share/classes/java/util/HexFormat.java

+20-25
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/*
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.
34
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
45
*
56
* This code is free software; you can redistribute it and/or modify it
@@ -158,37 +159,32 @@ public final class HexFormat {
158159
* The hexadecimal characters are from lowercase alpha digits.
159160
*/
160161
private static final HexFormat HEX_FORMAT =
161-
new HexFormat("", "", "", Case.LOWERCASE);
162+
new HexFormat("", "", "", false);
162163

163164
private static final HexFormat HEX_UPPER_FORMAT =
164-
new HexFormat("", "", "", Case.UPPERCASE);
165+
new HexFormat("", "", "", true);
165166

166167
private static final byte[] EMPTY_BYTES = {};
167168

168169
private final String delimiter;
169170
private final String prefix;
170171
private final String suffix;
171-
private final Case digitCase;
172-
173-
private enum Case {
174-
LOWERCASE,
175-
UPPERCASE
176-
}
172+
private final boolean ucase;
177173

178174
/**
179175
* Returns a HexFormat with a delimiter, prefix, suffix, and array of digits.
180176
*
181177
* @param delimiter a delimiter, non-null
182178
* @param prefix a prefix, non-null
183179
* @param suffix a suffix, non-null
184-
* @param digitCase enum indicating how to case digits
180+
* @param ucase enum indicating how to case digits
185181
* @throws NullPointerException if any argument is null
186182
*/
187-
private HexFormat(String delimiter, String prefix, String suffix, Case digitCase) {
183+
private HexFormat(String delimiter, String prefix, String suffix, boolean ucase) {
188184
this.delimiter = Objects.requireNonNull(delimiter, "delimiter");
189185
this.prefix = Objects.requireNonNull(prefix, "prefix");
190186
this.suffix = Objects.requireNonNull(suffix, "suffix");
191-
this.digitCase = digitCase;
187+
this.ucase = ucase;
192188
}
193189

194190
/**
@@ -217,7 +213,7 @@ public static HexFormat of() {
217213
* @return a {@link HexFormat} with the delimiter and lowercase characters
218214
*/
219215
public static HexFormat ofDelimiter(String delimiter) {
220-
return new HexFormat(delimiter, "", "", Case.LOWERCASE);
216+
return new HexFormat(delimiter, "", "", false);
221217
}
222218

223219
/**
@@ -226,7 +222,7 @@ public static HexFormat ofDelimiter(String delimiter) {
226222
* @return a copy of this {@code HexFormat} with the delimiter
227223
*/
228224
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);
230226
}
231227

232228
/**
@@ -236,7 +232,7 @@ public HexFormat withDelimiter(String delimiter) {
236232
* @return a copy of this {@code HexFormat} with the prefix
237233
*/
238234
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);
240236
}
241237

242238
/**
@@ -246,7 +242,7 @@ public HexFormat withPrefix(String prefix) {
246242
* @return a copy of this {@code HexFormat} with the suffix
247243
*/
248244
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);
250246
}
251247

252248
/**
@@ -258,7 +254,7 @@ public HexFormat withSuffix(String suffix) {
258254
public HexFormat withUpperCase() {
259255
if (this == HEX_FORMAT)
260256
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);
262258
}
263259

264260
/**
@@ -268,7 +264,7 @@ public HexFormat withUpperCase() {
268264
* @return a copy of this {@code HexFormat} with lowercase hexadecimal characters
269265
*/
270266
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);
272268
}
273269

274270
/**
@@ -306,7 +302,7 @@ public String suffix() {
306302
* otherwise {@code false}
307303
*/
308304
public boolean isUpperCase() {
309-
return digitCase == Case.UPPERCASE;
305+
return ucase;
310306
}
311307

312308
/**
@@ -436,7 +432,6 @@ private String formatOptDelimiter(byte[] bytes, int fromIndex, int toIndex) {
436432
return null;
437433
}
438434

439-
boolean ucase = digitCase == Case.UPPERCASE;
440435
int length = toIndex - fromIndex;
441436
if (delimiter.isEmpty()) {
442437
// Allocate the byte array and fill in the hex pairs for each byte
@@ -637,7 +632,7 @@ public char toLowHexDigit(int value) {
637632
if (value < 10) {
638633
return (char)('0' + value);
639634
}
640-
if (digitCase == Case.LOWERCASE) {
635+
if (!ucase) {
641636
return (char)('a' - 10 + value);
642637
}
643638
return (char)('A' - 10 + value);
@@ -658,7 +653,7 @@ public char toHighHexDigit(int value) {
658653
if (value < 10) {
659654
return (char)('0' + value);
660655
}
661-
if (digitCase == Case.LOWERCASE) {
656+
if (!ucase) {
662657
return (char)('a' - 10 + value);
663658
}
664659
return (char)('A' - 10 + value);
@@ -1067,7 +1062,7 @@ public boolean equals(Object o) {
10671062
if (o == null || getClass() != o.getClass())
10681063
return false;
10691064
HexFormat otherHex = (HexFormat) o;
1070-
return digitCase == otherHex.digitCase &&
1065+
return ucase == otherHex.ucase &&
10711066
delimiter.equals(otherHex.delimiter) &&
10721067
prefix.equals(otherHex.prefix) &&
10731068
suffix.equals(otherHex.suffix);
@@ -1081,7 +1076,7 @@ public boolean equals(Object o) {
10811076
@Override
10821077
public int hashCode() {
10831078
int result = Objects.hash(delimiter, prefix, suffix);
1084-
result = 31 * result + Boolean.hashCode(digitCase == Case.UPPERCASE);
1079+
result = 31 * result + Boolean.hashCode(ucase);
10851080
return result;
10861081
}
10871082

@@ -1093,7 +1088,7 @@ public int hashCode() {
10931088
*/
10941089
@Override
10951090
public String toString() {
1096-
return escapeNL("uppercase: " + (digitCase == Case.UPPERCASE) +
1091+
return escapeNL("uppercase: " + ucase +
10971092
", delimiter: \"" + delimiter +
10981093
"\", prefix: \"" + prefix +
10991094
"\", suffix: \"" + suffix + "\"");

0 commit comments

Comments
 (0)