Skip to content

Commit b30c63d

Browse files
committed
ICU-23054 Fix several types of errorprone warnings
Fixed: `CheckReturnValue`, `EqualsHashCode`, `JavaTimeDefaultTimeZone` And a couple of resource leaks (streams opened and not closed)
1 parent edd67f0 commit b30c63d

File tree

8 files changed

+58
-59
lines changed

8 files changed

+58
-59
lines changed

icu4j/main/core/src/main/java/com/ibm/icu/impl/JavaTimeConverters.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ public static Calendar temporalToCalendar(ZonedDateTime dateTime) {
9191
* @deprecated This API is ICU internal only.
9292
*/
9393
@Deprecated
94+
@SuppressWarnings("JavaTimeDefaultTimeZone")
9495
public static Calendar temporalToCalendar(OffsetTime time) {
9596
return temporalToCalendar(time.atDate(LocalDate.now()));
9697
}

icu4j/main/core/src/main/java/com/ibm/icu/impl/RBBIDataWrapper.java

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import java.nio.ByteBuffer;
1515
import java.nio.charset.StandardCharsets;
1616
import java.util.Arrays;
17+
import java.util.Objects;
1718

1819
import com.ibm.icu.impl.ICUBinary.Authenticate;
1920
import com.ibm.icu.text.RuleBasedBreakIterator;
@@ -142,6 +143,19 @@ public boolean equals (Object other) {
142143
if (fFlags != otherST.fFlags) return false;
143144
return Arrays.equals(fTable, otherST.fTable);
144145
}
146+
147+
/**
148+
* {@inheritDoc}
149+
*/
150+
@Override
151+
public int hashCode() {
152+
final int prime = 31;
153+
int result = 1;
154+
result = prime * result + Arrays.hashCode(fTable);
155+
result = prime * result
156+
+ Objects.hash(fDictCategoriesStart, fFlags, fLookAheadResultsSize, fNumStates, fRowLen);
157+
return result;
158+
}
145159
}
146160

147161
/**
@@ -170,14 +184,14 @@ static public boolean equals(RBBIStateTable left, RBBIStateTable right) {
170184

171185
public CodePointTrie fTrie;
172186
public String fRuleSource;
173-
public int fStatusTable[];
187+
public int [] fStatusTable;
174188

175189
public static final int DATA_FORMAT = 0x42726b20; // "Brk "
176190
public static final int FORMAT_VERSION = 0x06000000; // 6.0.0.0
177191

178192
private static final class IsAcceptable implements Authenticate {
179193
@Override
180-
public boolean isDataVersionAcceptable(byte version[]) {
194+
public boolean isDataVersionAcceptable(byte[] version) {
181195
int intVersion = (version[0] << 24) + (version[1] << 16) + (version[2] << 8) + version[3];
182196
return intVersion == FORMAT_VERSION;
183197
}
@@ -397,7 +411,6 @@ public static RBBIDataWrapper get(ByteBuffer bytes) throws IOException {
397411
throw new IOException("Break iterator Rule data corrupt");
398412
}
399413
ICUBinary.skipBytes(bytes, This.fHeader.fRuleSource - pos);
400-
pos = This.fHeader.fRuleSource;
401414
This.fRuleSource = new String(
402415
ICUBinary.getBytes(bytes, This.fHeader.fRuleSourceLen, 0), StandardCharsets.UTF_8);
403416

@@ -507,13 +520,13 @@ private void dumpRow(java.io.PrintStream out, RBBIStateTable table, char state
507520

508521
private void dumpCharCategories(java.io.PrintStream out) {
509522
int n = fHeader.fCatCount;
510-
String catStrings[] = new String[n+1];
523+
String[] catStrings = new String[n+1];
511524
int rangeStart = 0;
512525
int rangeEnd = 0;
513526
int lastCat = -1;
514527
int char32;
515528
int category;
516-
int lastNewline[] = new int[n+1];
529+
int[] lastNewline = new int[n+1];
517530

518531
for (category = 0; category <= fHeader.fCatCount; category ++) {
519532
catStrings[category] = "";

icu4j/main/core/src/main/java/com/ibm/icu/impl/number/DecimalQuantity_AbstractBCD.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import java.math.BigInteger;
77
import java.math.MathContext;
88
import java.text.FieldPosition;
9+
import java.util.Objects;
910

1011
import com.ibm.icu.impl.StandardPlural;
1112
import com.ibm.icu.impl.Utility;
@@ -1201,6 +1202,23 @@ public boolean equals(Object other) {
12011202
}
12021203
}
12031204

1205+
@Override
1206+
public int hashCode() {
1207+
int result = Objects.hash(scale, precision, flags, lReqPos, rReqPos, isApproximate);
1208+
1209+
if (precision == 0) {
1210+
return result;
1211+
}
1212+
if (isApproximate) {
1213+
return Objects.hash(result, origDouble, origDelta);
1214+
} else {
1215+
for (int m = getUpperDisplayMagnitude(); m >= getLowerDisplayMagnitude(); m--) {
1216+
return Objects.hash(result, getDigit(m));
1217+
}
1218+
}
1219+
return result;
1220+
}
1221+
12041222
/**
12051223
* Returns a single digit from the BCD list. No internal state is changed by calling this method.
12061224
*

icu4j/main/core/src/main/java/com/ibm/icu/impl/personname/PersonNameFormatterImpl.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ private Locale newLocaleWithScript(Locale oldLocale, String scriptCode, String r
363363
Locale.Builder builder = new Locale.Builder();
364364
if (oldLocale != null) {
365365
workingLocale = oldLocale;
366-
builder.setLocale(oldLocale);
366+
var unused = builder.setLocale(oldLocale);
367367
localeScript = ULocale.addLikelySubtags(ULocale.forLocale(oldLocale)).getScript();
368368
} else {
369369
ULocale tmpLocale = ULocale.addLikelySubtags(new ULocale("und_" + scriptCode));
@@ -372,7 +372,7 @@ private Locale newLocaleWithScript(Locale oldLocale, String scriptCode, String r
372372
localeScript = workingLocale.getScript();
373373

374374
if (regionCode != null) {
375-
builder.setRegion(regionCode);
375+
var unused = builder.setRegion(regionCode);
376376
}
377377
}
378378

icu4j/main/core/src/main/java/com/ibm/icu/text/NFSubstitution.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1651,11 +1651,10 @@ public Number doParse(String text, ParsePosition parsePosition, double baseValue
16511651
if (withZeros) {
16521652
String workText = text;
16531653
ParsePosition workPos = new ParsePosition(1);
1654-
//int digit;
16551654

16561655
while (workText.length() > 0 && workPos.getIndex() != 0) {
16571656
workPos.setIndex(0);
1658-
/*digit = */ruleSet.parse(workText, workPos, 1, nonNumericalExecutedRuleMask, recursionCount).intValue(); // parse zero or nothing at all
1657+
var unused = ruleSet.parse(workText, workPos, 1, nonNumericalExecutedRuleMask, recursionCount).intValue(); // parse zero or nothing at all
16591658
if (workPos.getIndex() == 0) {
16601659
// we failed, either there were no more zeros, or the number was formatted with digits
16611660
// either way, we're done

icu4j/main/core/src/test/java/com/ibm/icu/dev/test/number/NumberSkeletonTest.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,6 @@
22
// License & terms of use: http://www.unicode.org/copyright.html
33
package com.ibm.icu.dev.test.number;
44

5-
import static org.junit.Assert.assertEquals;
6-
import static org.junit.Assert.assertTrue;
7-
import static org.junit.Assert.fail;
8-
95
import java.math.RoundingMode;
106
import java.util.Locale;
117

icu4j/perf-tests/src/main/java/com/ibm/icu/dev/test/perf/CollationPerformanceTest.java

Lines changed: 14 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
import java.io.FileInputStream;
1414
import java.io.IOException;
1515
import java.io.InputStreamReader;
16+
import java.nio.charset.StandardCharsets;
17+
import java.nio.file.Files;
18+
import java.nio.file.Paths;
1619
import java.util.ArrayList;
1720
import java.util.Comparator;
1821

@@ -831,7 +834,7 @@ void setOptions() {
831834
opt_icu = false;
832835
}
833836

834-
if (opt_rules.length() != 0) {
837+
if (!opt_rules.isEmpty()) {
835838
try {
836839
icuCol = new com.ibm.icu.text.RuleBasedCollator(getCollationRules(opt_rules));
837840
} catch (Exception e) {
@@ -1119,37 +1122,20 @@ void readDataLines() {
11191122
* 3. File encoding is ISO-8859-1
11201123
*/
11211124
String getCollationRules(String ruleFileName) {
1122-
FileInputStream fis = null;
1123-
InputStreamReader isr = null;
1124-
BufferedReader br = null;
1125-
try {
1126-
fis = new FileInputStream(opt_rules);
1127-
isr = new InputStreamReader(fis,"ISO-8859-1");
1128-
br= new BufferedReader(isr);
1129-
} catch (Exception e) {
1125+
StringBuilder rules = new StringBuilder();
1126+
try (BufferedReader br = Files.newBufferedReader(Paths.get(ruleFileName), StandardCharsets.ISO_8859_1)) {
1127+
br.lines().forEach(line -> {
1128+
int commentPos = line.indexOf('#');
1129+
if (commentPos >= 0) line = line.substring(0, commentPos);
1130+
rules.append(line.trim());
1131+
});
1132+
} catch (IOException e) {
11301133
System.err.println("Error: File access exception: " + e.getMessage() + "!");
11311134
System.exit(2);
11321135
}
1133-
String rules = "";
1134-
String line = "";
1135-
while (true) {
1136-
try {
1137-
line = br.readLine();
1138-
} catch (IOException e) {
1139-
System.err.println("Read File Error" + e.getMessage() + "!");
1140-
System.exit(1);
1141-
}
1142-
if (line == null) {
1143-
break;
1144-
}
1145-
int commentPos = line.indexOf('#');
1146-
if (commentPos >= 0) line = line.substring(0, commentPos);
1147-
line = line.trim();
1148-
rules = rules + line;
1149-
}
1150-
return rules;
1136+
return rules.toString();
11511137
}
1152-
1138+
11531139
//Implementing qsort
11541140
void qSortImpl_java_usekeys(String src[], int fromIndex, int toIndex, java.text.Collator c) {
11551141
int low = fromIndex;

icu4j/perf-tests/src/main/java/com/ibm/icu/dev/test/perf/RBBIPerf.java

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,13 @@
77
**********************************************************************
88
*/
99
package com.ibm.icu.dev.test.perf;
10-
import java.io.FileInputStream;
1110
import java.io.IOException;
12-
import java.io.InputStream;
13-
import java.io.InputStreamReader;
11+
import java.nio.charset.StandardCharsets;
12+
import java.nio.file.Files;
13+
import java.nio.file.Paths;
1414
import java.text.BreakIterator;
1515

1616
import com.ibm.icu.text.RuleBasedBreakIterator;
17-
import com.ibm.icu.text.UTF16;
1817

1918
/**
2019
* A class for testing UnicodeSet performance.
@@ -24,7 +23,6 @@
2423
*/
2524
public class RBBIPerf extends PerfTest {
2625

27-
String dataFileName;
2826
RuleBasedBreakIterator bi;
2927
BreakIterator jdkbi;
3028
String testString;
@@ -40,19 +38,7 @@ protected void setup(String[] args) {
4038
}
4139

4240
try {
43-
dataFileName = args[0];
44-
StringBuffer testFileBuf = new StringBuffer();
45-
InputStream is = new FileInputStream(dataFileName);
46-
InputStreamReader isr = new InputStreamReader(is, "UTF-8");
47-
int c;
48-
for (;;) {
49-
c = isr.read();
50-
if (c < 0) {
51-
break;
52-
}
53-
UTF16.append(testFileBuf, c);
54-
}
55-
testString = testFileBuf.toString();
41+
testString = Files.readString(Paths.get(args[0]), StandardCharsets.UTF_8);
5642
}
5743
catch (IOException e) {
5844
throw new RuntimeException(e.toString());

0 commit comments

Comments
 (0)