Skip to content

Commit 67642f0

Browse files
committed
Cleanup: Util.toMap() is deprecated in favor of plain Java alternatives
1 parent f5ab0eb commit 67642f0

3 files changed

Lines changed: 22 additions & 14 deletions

File tree

cayenne-cgen/src/main/java/org/apache/cayenne/gen/ImportUtils.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,13 @@
2323
import org.apache.cayenne.map.DbAttribute;
2424
import org.apache.cayenne.map.EmbeddableAttribute;
2525
import org.apache.cayenne.map.ObjAttribute;
26-
import org.apache.cayenne.util.Util;
2726

2827
import java.util.ArrayList;
2928
import java.util.HashMap;
3029
import java.util.List;
3130
import java.util.Map;
31+
import java.util.stream.Collectors;
32+
import java.util.stream.IntStream;
3233

3334
/**
3435
* Methods for mangling strings.
@@ -44,8 +45,12 @@ public class ImportUtils {
4445
Byte.class.getName(), Boolean.class.getName(), Float.class.getName(), Short.class.getName(),
4546
Integer.class.getName(), Character.class.getName() };
4647

47-
static Map<String, String> classesForPrimitives = Util.toMap(primitives, primitiveClasses);
48-
static Map<String, String> primitivesForClasses = Util.toMap(primitiveClasses, primitives);
48+
static final Map<String, String> classesForPrimitives = IntStream.range(0, primitives.length)
49+
.boxed()
50+
.collect(Collectors.toUnmodifiableMap(i -> primitives[i], i -> primitiveClasses[i]));
51+
static final Map<String, String> primitivesForClasses = IntStream.range(0, primitiveClasses.length)
52+
.boxed()
53+
.collect(Collectors.toUnmodifiableMap(i -> primitiveClasses[i], i -> primitives[i]));
4954

5055
protected Map<String, String> importTypesMap = new HashMap<>();
5156

cayenne/src/main/java/org/apache/cayenne/util/Util.java

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public class Util {
8181
}
8282

8383
@Deprecated
84-
private static DefaultAdhocObjectFactory objectFactory;
84+
private static final DefaultAdhocObjectFactory objectFactory;
8585

8686
static {
8787
objectFactory = new DefaultAdhocObjectFactory(null, new DefaultClassLoaderManager());
@@ -109,7 +109,7 @@ public static File toFile(URL url) throws IllegalArgumentException {
109109
* separator.
110110
*/
111111
public static String stringFromFile(File file) throws IOException {
112-
return stringFromFile(file, System.getProperty("line.separator"));
112+
return stringFromFile(file, System.lineSeparator());
113113
}
114114

115115
/**
@@ -119,8 +119,8 @@ public static String stringFromFile(File file) throws IOException {
119119
public static String stringFromFile(File file, String joinWith) throws IOException {
120120
StringBuilder buf = new StringBuilder();
121121

122-
try (BufferedReader in = new BufferedReader(new FileReader(file));) {
123-
String line = null;
122+
try (BufferedReader in = new BufferedReader(new FileReader(file))) {
123+
String line;
124124
while ((line = in.readLine()) != null) {
125125
buf.append(line).append(joinWith);
126126
}
@@ -148,7 +148,7 @@ public static String join(Iterable<?> objects, String separator) {
148148
StringBuilder builder = new StringBuilder();
149149

150150
for (Object o : objects) {
151-
if (builder.length() > 0) {
151+
if (!builder.isEmpty()) {
152152
builder.append(separator);
153153
}
154154

@@ -173,8 +173,7 @@ public static String substBackslashes(String string) {
173173
* recursively "unwraps" it, and returns the result to the user.
174174
*/
175175
public static Throwable unwindException(Throwable th) {
176-
if (th instanceof SAXException) {
177-
SAXException sax = (SAXException) th;
176+
if (th instanceof SAXException sax) {
178177
if (sax.getException() != null) {
179178
return unwindException(sax.getException());
180179
}
@@ -226,7 +225,7 @@ public static <T> int nullSafeCompare(boolean nullsFirst, Comparable<T> o1, T o2
226225
* Returns true, if the String is null or an empty string.
227226
*/
228227
public static boolean isEmptyString(CharSequence string) {
229-
return string == null || string.length() == 0;
228+
return string == null || string.isEmpty();
230229
}
231230

232231
/**
@@ -291,7 +290,7 @@ public static int countMatches(final String str, final String sub) {
291290
* @since 4.1
292291
*/
293292
public static String capitalized(String name) {
294-
if (name == null || name.length() == 0) {
293+
if (name == null || name.isEmpty()) {
295294
return name;
296295
}
297296

@@ -305,7 +304,7 @@ public static String capitalized(String name) {
305304
* @since 4.2
306305
*/
307306
public static String uncapitalized(String aString) {
308-
if (aString == null || aString.length() == 0) {
307+
if (aString == null || aString.isEmpty()) {
309308
return aString;
310309
}
311310

@@ -402,7 +401,7 @@ public static String getPackagePath(String className) {
402401
* @since 3.0
403402
*/
404403
public static String stripPackageName(String className) {
405-
if (className == null || className.length() == 0) {
404+
if (className == null || className.isEmpty()) {
406405
return className;
407406
}
408407

@@ -419,7 +418,10 @@ public static String stripPackageName(String className) {
419418
* Creates a mutable map out of two arrays with keys and values.
420419
*
421420
* @since 1.2
421+
* @deprecated use {@link java.util.stream.IntStream#range} with
422+
* {@link java.util.stream.Collectors#toMap} to build a map from parallel arrays
422423
*/
424+
@Deprecated(since = "5.0", forRemoval = true)
423425
public static <K, V> Map<K, V> toMap(K[] keys, V[] values) {
424426
int keysSize = (keys != null) ? keys.length : 0;
425427
int valuesSize = (values != null) ? values.length : 0;

cayenne/src/test/java/org/apache/cayenne/util/UtilTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ public void getJavaClass() throws Exception {
7575
}
7676

7777
@Test
78+
@SuppressWarnings("deprecation")
7879
public void toMap() {
7980
Object[] keys = new Object[] { "a", "b" };
8081
Object[] values = new Object[] { "1", "2" };

0 commit comments

Comments
 (0)