]> source.dussan.org Git - vaadin-framework.git/commitdiff
Make UPPER_CASE_PROPERTY_IDS more human friendly by default (#20380)
authorArtur Signell <artur@vaadin.com>
Wed, 12 Oct 2016 19:44:00 +0000 (22:44 +0300)
committerVaadin Code Review <review@vaadin.com>
Mon, 17 Oct 2016 06:01:50 +0000 (06:01 +0000)
Change-Id: I9bd845a08fa3c440f05d102ccf8920dfc1c95b6b

shared/src/main/java/com/vaadin/shared/util/SharedUtil.java
shared/src/test/java/com/vaadin/shared/util/SharedUtilTest.java

index 141da10c35b3ae46eea88d6e915c3343bde0cff5..d2481c5d1147ead2ca23bf209959c488c6583d05 100644 (file)
@@ -126,6 +126,31 @@ public class SharedUtil implements Serializable {
         return join(parts, " ");
     }
 
+    /**
+     * Converts an UPPER_CASE_STRING to a human friendly format (Upper Case
+     * String).
+     * <p>
+     * Splits words on {@code _}. Examples:
+     * <p>
+     * {@literal MY_BEAN_CONTAINER} becomes {@literal My Bean Container}
+     * {@literal AWESOME_URL_FACTORY} becomes {@literal Awesome Url Factory}
+     * {@literal SOMETHING} becomes {@literal Something}
+     *
+     * @since
+     * @param upperCaseUnderscoreString
+     *            The input string in UPPER_CASE_UNDERSCORE format
+     * @return A human friendly version of the input
+     */
+    public static String upperCaseUnderscoreToHumanFriendly(
+            String upperCaseUnderscoreString) {
+        String[] parts = upperCaseUnderscoreString.replaceFirst("^_*", "")
+                .split("_");
+        for (int i = 0; i < parts.length; i++) {
+            parts[i] = capitalize(parts[i].toLowerCase(Locale.ENGLISH));
+        }
+        return join(parts, " ");
+    }
+
     private static boolean isAllUpperCase(String string) {
         for (int i = 0; i < string.length(); i++) {
             char c = string.charAt(i);
@@ -203,6 +228,11 @@ public class SharedUtil implements Serializable {
             string = string.substring(dotLocation + 1);
         }
 
+        if (string.matches("^[0-9A-Z_]+$")) {
+            // Deal with UPPER_CASE_PROPERTY_IDS
+            return upperCaseUnderscoreToHumanFriendly(string);
+        }
+
         return camelCaseToHumanFriendly(string);
     }
 
index 6d8cab4fea1b551fc40b7a3270b35e24188827db..791637e1b1128013b158945dd8889db4fb69d45e 100644 (file)
@@ -150,4 +150,32 @@ public class SharedUtilTest {
                 SharedUtil.getDuplicates(new Object[] { 1.2, "a", 1.2 }));
     }
 
+    @Test
+    public void propertyIdToHumanFriendly() {
+        Assert.assertEquals("", SharedUtil.propertyIdToHumanFriendly(""));
+        Assert.assertEquals("First Name",
+                SharedUtil.propertyIdToHumanFriendly("firstName"));
+        Assert.assertEquals("First Name",
+                SharedUtil.propertyIdToHumanFriendly("FirstName"));
+        Assert.assertEquals("First Name",
+                SharedUtil.propertyIdToHumanFriendly("FIRST_NAME"));
+        Assert.assertEquals("Firstname",
+                SharedUtil.propertyIdToHumanFriendly("FIRSTNAME"));
+
+        Assert.assertEquals("2015 Q3",
+                SharedUtil.propertyIdToHumanFriendly("2015_Q3"));
+        Assert.assertEquals("Column X",
+                SharedUtil.propertyIdToHumanFriendly("_COLUMN_X"));
+        Assert.assertEquals("Column X",
+                SharedUtil.propertyIdToHumanFriendly("__COLUMN_X"));
+        Assert.assertEquals("1column Foobar",
+                SharedUtil.propertyIdToHumanFriendly("1COLUMN_FOOBAR"));
+        Assert.assertEquals("Result 2015",
+                SharedUtil.propertyIdToHumanFriendly("RESULT_2015"));
+        Assert.assertEquals("2015result",
+                SharedUtil.propertyIdToHumanFriendly("2015RESULT"));
+        Assert.assertEquals("Result2015",
+                SharedUtil.propertyIdToHumanFriendly("RESULT2015"));
+
+    }
 }