You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2024/09/12 12:34:57 UTC

(commons-csv) 01/02: Convert cascading if-else to switch

This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-csv.git

commit af49daadc69d81a5e699523ec8297a91e5e70f1d
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Thu Sep 12 08:34:23 2024 -0400

    Convert cascading if-else to switch
---
 .../java/org/apache/commons/csv/CSVFormatTest.java | 44 ++++++++++++++++------
 1 file changed, 32 insertions(+), 12 deletions(-)

diff --git a/src/test/java/org/apache/commons/csv/CSVFormatTest.java b/src/test/java/org/apache/commons/csv/CSVFormatTest.java
index 3220759e..939935ee 100644
--- a/src/test/java/org/apache/commons/csv/CSVFormatTest.java
+++ b/src/test/java/org/apache/commons/csv/CSVFormatTest.java
@@ -233,42 +233,62 @@ public class CSVFormatTest {
                 if (name.startsWith("with")) {
                     for (final Class<?> cls : method.getParameterTypes()) {
                         final String type = cls.getCanonicalName();
-                        if ("boolean".equals(type)) {
+                        switch (type) {
+                        case "boolean": {
                             final Object defTrue = method.invoke(CSVFormat.DEFAULT, Boolean.TRUE);
                             final Object defFalse = method.invoke(CSVFormat.DEFAULT, Boolean.FALSE);
                             assertNotEquals(name, type, defTrue, defFalse);
-                        } else if ("char".equals(type)) {
+                            break;
+                        }
+                        case "char": {
                             final Object a = method.invoke(CSVFormat.DEFAULT, 'a');
                             final Object b = method.invoke(CSVFormat.DEFAULT, 'b');
                             assertNotEquals(name, type, a, b);
-                        } else if ("java.lang.Character".equals(type)) {
+                            break;
+                        }
+                        case "java.lang.Character": {
                             final Object a = method.invoke(CSVFormat.DEFAULT, new Object[] { null });
                             final Object b = method.invoke(CSVFormat.DEFAULT, Character.valueOf('d'));
                             assertNotEquals(name, type, a, b);
-                        } else if ("java.lang.String".equals(type)) {
+                            break;
+                        }
+                        case "java.lang.String": {
                             final Object a = method.invoke(CSVFormat.DEFAULT, new Object[] { null });
                             final Object b = method.invoke(CSVFormat.DEFAULT, "e");
                             assertNotEquals(name, type, a, b);
-                        } else if ("java.lang.String[]".equals(type)) {
+                            break;
+                        }
+                        case "java.lang.String[]": {
                             final Object a = method.invoke(CSVFormat.DEFAULT, new Object[] { new String[] { null, null } });
                             final Object b = method.invoke(CSVFormat.DEFAULT, new Object[] { new String[] { "f", "g" } });
                             assertNotEquals(name, type, a, b);
-                        } else if ("org.apache.commons.csv.QuoteMode".equals(type)) {
+                            break;
+                        }
+                        case "org.apache.commons.csv.QuoteMode": {
                             final Object a = method.invoke(CSVFormat.DEFAULT, QuoteMode.MINIMAL);
                             final Object b = method.invoke(CSVFormat.DEFAULT, QuoteMode.ALL);
                             assertNotEquals(name, type, a, b);
-                        } else if ("org.apache.commons.csv.DuplicateHeaderMode".equals(type)) {
+                            break;
+                        }
+                        case "org.apache.commons.csv.DuplicateHeaderMode": {
                             final Object a = method.invoke(CSVFormat.DEFAULT, DuplicateHeaderMode.ALLOW_ALL);
                             final Object b = method.invoke(CSVFormat.DEFAULT, DuplicateHeaderMode.DISALLOW);
                             assertNotEquals(name, type, a, b);
-                        } else if ("java.lang.Object[]".equals(type)) {
+                            break;
+                        }
+                        case "java.lang.Object[]": {
                             final Object a = method.invoke(CSVFormat.DEFAULT, new Object[] { new Object[] { null, null } });
                             final Object b = method.invoke(CSVFormat.DEFAULT, new Object[] { new Object[] { new Object(), new Object() } });
                             assertNotEquals(name, type, a, b);
-                        } else if ("withHeader".equals(name)) { // covered above by String[]
-                            // ignored
-                        } else {
-                            fail("Unhandled method: " + name + "(" + type + ")");
+                            break;
+                        }
+                        default:
+                            if ("withHeader".equals(name)) { // covered above by String[]
+                                // ignored
+                            } else {
+                                fail("Unhandled method: " + name + "(" + type + ")");
+                            }
+                            break;
                         }
                     }
                 }