From e4d824a0607f212abfd3fb36fef5224cc0034903 Mon Sep 17 00:00:00 2001 From: Artur Signell Date: Wed, 12 Oct 2016 22:44:00 +0300 Subject: [PATCH] Make UPPER_CASE_PROPERTY_IDS more human friendly by default (#20380) Change-Id: I9bd845a08fa3c440f05d102ccf8920dfc1c95b6b --- .../com/vaadin/shared/util/SharedUtil.java | 30 +++++++++++++++++++ .../vaadin/shared/util/SharedUtilTest.java | 28 +++++++++++++++++ 2 files changed, 58 insertions(+) diff --git a/shared/src/main/java/com/vaadin/shared/util/SharedUtil.java b/shared/src/main/java/com/vaadin/shared/util/SharedUtil.java index 141da10c35..d2481c5d11 100644 --- a/shared/src/main/java/com/vaadin/shared/util/SharedUtil.java +++ b/shared/src/main/java/com/vaadin/shared/util/SharedUtil.java @@ -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). + *

+ * Splits words on {@code _}. Examples: + *

+ * {@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); } diff --git a/shared/src/test/java/com/vaadin/shared/util/SharedUtilTest.java b/shared/src/test/java/com/vaadin/shared/util/SharedUtilTest.java index 6d8cab4fea..791637e1b1 100644 --- a/shared/src/test/java/com/vaadin/shared/util/SharedUtilTest.java +++ b/shared/src/test/java/com/vaadin/shared/util/SharedUtilTest.java @@ -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")); + + } } -- 2.39.5