Repository: incubator-juneau Updated Branches: refs/heads/master 58dc46e29 -> de8a66b87
Add PropertyNamerULC Project: http://git-wip-us.apache.org/repos/asf/incubator-juneau/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-juneau/commit/0f030b18 Tree: http://git-wip-us.apache.org/repos/asf/incubator-juneau/tree/0f030b18 Diff: http://git-wip-us.apache.org/repos/asf/incubator-juneau/diff/0f030b18 Branch: refs/heads/master Commit: 0f030b188bcec308ea1662291fb6b7358f76489a Parents: 58dc46e Author: JamesBognar <[email protected]> Authored: Wed May 31 08:24:06 2017 -0400 Committer: JamesBognar <[email protected]> Committed: Wed May 31 08:24:06 2017 -0400 ---------------------------------------------------------------------- .../java/org/apache/juneau/BeanMapTest.java | 4 +- .../juneau/PropertyNamerDashedLcTest.java | 40 ------------ .../org/apache/juneau/PropertyNamerTest.java | 57 +++++++++++++++++ .../java/org/apache/juneau/PropertyNamer.java | 2 +- .../org/apache/juneau/PropertyNamerDLC.java | 64 ++++++++++++++++++++ .../apache/juneau/PropertyNamerDashedLC.java | 64 -------------------- .../java/org/apache/juneau/annotation/Bean.java | 2 +- 7 files changed, 125 insertions(+), 108 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/0f030b18/juneau-core-test/src/test/java/org/apache/juneau/BeanMapTest.java ---------------------------------------------------------------------- diff --git a/juneau-core-test/src/test/java/org/apache/juneau/BeanMapTest.java b/juneau-core-test/src/test/java/org/apache/juneau/BeanMapTest.java index 5dd13b0..44ecd4f 100755 --- a/juneau-core-test/src/test/java/org/apache/juneau/BeanMapTest.java +++ b/juneau-core-test/src/test/java/org/apache/juneau/BeanMapTest.java @@ -1052,7 +1052,7 @@ public class BeanMapTest { assertEquals(6, b.bingBooURL); } - @Bean(propertyNamer=PropertyNamerDashedLC.class) + @Bean(propertyNamer=PropertyNamerDLC.class) public static class P1 { public int foo, barBaz, bingBooURL; } @@ -1075,7 +1075,7 @@ public class BeanMapTest { assertEquals(4, b.getBazBING()); } - @Bean(propertyNamer=PropertyNamerDashedLC.class) + @Bean(propertyNamer=PropertyNamerDLC.class) public static class P2 { private int fooBar, bazBING; public int getFooBar() { http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/0f030b18/juneau-core-test/src/test/java/org/apache/juneau/PropertyNamerDashedLcTest.java ---------------------------------------------------------------------- diff --git a/juneau-core-test/src/test/java/org/apache/juneau/PropertyNamerDashedLcTest.java b/juneau-core-test/src/test/java/org/apache/juneau/PropertyNamerDashedLcTest.java deleted file mode 100755 index 5d2e1f3..0000000 --- a/juneau-core-test/src/test/java/org/apache/juneau/PropertyNamerDashedLcTest.java +++ /dev/null @@ -1,40 +0,0 @@ -// *************************************************************************************************************************** -// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file * -// * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file * -// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance * -// * with the License. You may obtain a copy of the License at * -// * * -// * http://www.apache.org/licenses/LICENSE-2.0 * -// * * -// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an * -// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the * -// * specific language governing permissions and limitations under the License. * -// *************************************************************************************************************************** -package org.apache.juneau; - -import static org.junit.Assert.*; - -import org.junit.*; - -@SuppressWarnings("javadoc") -public class PropertyNamerDashedLcTest { - - //==================================================================================================== - // test - //==================================================================================================== - @Test - public void test() throws Exception { - PropertyNamer n = new PropertyNamerDashedLC(); - - assertEquals("abc", n.getPropertyName("ABC")); - assertEquals("abc", n.getPropertyName("abc")); - assertEquals("foo-bar-baz", n.getPropertyName("FooBarBaz")); - assertEquals("foo-bar-baz", n.getPropertyName("FooBarBAZ")); - assertEquals("foo-bar-baz", n.getPropertyName("fooBarBAZ")); - assertEquals("", n.getPropertyName("")); - assertNull(n.getPropertyName(null)); - assertEquals("a", n.getPropertyName("A")); - assertEquals("a", n.getPropertyName("A")); - - } -} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/0f030b18/juneau-core-test/src/test/java/org/apache/juneau/PropertyNamerTest.java ---------------------------------------------------------------------- diff --git a/juneau-core-test/src/test/java/org/apache/juneau/PropertyNamerTest.java b/juneau-core-test/src/test/java/org/apache/juneau/PropertyNamerTest.java new file mode 100755 index 0000000..4a0331e --- /dev/null +++ b/juneau-core-test/src/test/java/org/apache/juneau/PropertyNamerTest.java @@ -0,0 +1,57 @@ +// *************************************************************************************************************************** +// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file * +// * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file * +// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance * +// * with the License. You may obtain a copy of the License at * +// * * +// * http://www.apache.org/licenses/LICENSE-2.0 * +// * * +// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an * +// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the * +// * specific language governing permissions and limitations under the License. * +// *************************************************************************************************************************** +package org.apache.juneau; + +import static org.junit.Assert.*; + +import org.junit.*; + +@SuppressWarnings("javadoc") +public class PropertyNamerTest { + + //==================================================================================================== + // test dashed-lower-case + //==================================================================================================== + @Test + public void testDLC() throws Exception { + PropertyNamer n = new PropertyNamerDLC(); + + assertEquals("abc", n.getPropertyName("ABC")); + assertEquals("abc", n.getPropertyName("abc")); + assertEquals("foo-bar-baz", n.getPropertyName("FooBarBaz")); + assertEquals("foo-bar-baz", n.getPropertyName("FooBarBAZ")); + assertEquals("foo-bar-baz", n.getPropertyName("fooBarBAZ")); + assertEquals("", n.getPropertyName("")); + assertNull(n.getPropertyName(null)); + assertEquals("a", n.getPropertyName("A")); + assertEquals("a", n.getPropertyName("A")); + } + + //==================================================================================================== + // test underscore-lower-case + //==================================================================================================== + @Test + public void testULC() throws Exception { + PropertyNamer n = new PropertyNamerULC(); + + assertEquals("abc", n.getPropertyName("ABC")); + assertEquals("abc", n.getPropertyName("abc")); + assertEquals("foo_bar_baz", n.getPropertyName("FooBarBaz")); + assertEquals("foo_bar_baz", n.getPropertyName("FooBarBAZ")); + assertEquals("foo_bar_baz", n.getPropertyName("fooBarBAZ")); + assertEquals("", n.getPropertyName("")); + assertNull(n.getPropertyName(null)); + assertEquals("a", n.getPropertyName("A")); + assertEquals("a", n.getPropertyName("A")); + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/0f030b18/juneau-core/src/main/java/org/apache/juneau/PropertyNamer.java ---------------------------------------------------------------------- diff --git a/juneau-core/src/main/java/org/apache/juneau/PropertyNamer.java b/juneau-core/src/main/java/org/apache/juneau/PropertyNamer.java index fafa081..2d6a33e 100644 --- a/juneau-core/src/main/java/org/apache/juneau/PropertyNamer.java +++ b/juneau-core/src/main/java/org/apache/juneau/PropertyNamer.java @@ -17,7 +17,7 @@ import org.apache.juneau.annotation.*; /** * Defines an API for converting conventional bean property names to some other form. * <p> - * For example, given the bean property <js>"fooBarURL"</js>, the {@link PropertyNamerDashedLC} + * For example, given the bean property <js>"fooBarURL"</js>, the {@link PropertyNamerDLC} * property namer will convert this to <js>"foo-bar-url"</js>. * <p> * Property namers are associated with beans through the {@link Bean#propertyNamer} annotation. http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/0f030b18/juneau-core/src/main/java/org/apache/juneau/PropertyNamerDLC.java ---------------------------------------------------------------------- diff --git a/juneau-core/src/main/java/org/apache/juneau/PropertyNamerDLC.java b/juneau-core/src/main/java/org/apache/juneau/PropertyNamerDLC.java new file mode 100644 index 0000000..db21ed7 --- /dev/null +++ b/juneau-core/src/main/java/org/apache/juneau/PropertyNamerDLC.java @@ -0,0 +1,64 @@ +// *************************************************************************************************************************** +// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file * +// * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file * +// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance * +// * with the License. You may obtain a copy of the License at * +// * * +// * http://www.apache.org/licenses/LICENSE-2.0 * +// * * +// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an * +// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the * +// * specific language governing permissions and limitations under the License. * +// *************************************************************************************************************************** +package org.apache.juneau; + +/** + * Converts property names to dashed-lower-case format. + * + * <h5 class='section'>Examples:</h5> + * <ul> + * <li><js>"fooBar"</js> -> <js>"foo-bar"</js> + * <li><js>"fooBarURL"</js> -> <js>"foo-bar-url"</js> + * <li><js>"FooBarURL"</js> -> <js>"foo-bar-url"</js> + * </ul> + */ +public final class PropertyNamerDLC implements PropertyNamer { + + @Override /* PropertyNamer */ + public String getPropertyName(String name) { + if (name == null || name.isEmpty()) + return name; + + int numUCs = 0; + boolean isPrevUC = Character.isUpperCase(name.charAt(0)); + for (int i = 1; i < name.length(); i++) { + char c = name.charAt(i); + if (Character.isUpperCase(c)) { + if (! isPrevUC) + numUCs++; + isPrevUC = true; + } else { + isPrevUC = false; + } + } + + char[] name2 = new char[name.length() + numUCs]; + isPrevUC = Character.isUpperCase(name.charAt(0)); + name2[0] = Character.toLowerCase(name.charAt(0)); + int ni = 0; + for (int i = 0; i < name.length(); i++) { + char c = name.charAt(i); + if (Character.isUpperCase(c)) { + if (! isPrevUC) + name2[ni++] = '-'; + isPrevUC = true; + name2[ni++] = Character.toLowerCase(c); + } else { + isPrevUC = false; + name2[ni++] = c; + } + } + + return new String(name2); + } +} http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/0f030b18/juneau-core/src/main/java/org/apache/juneau/PropertyNamerDashedLC.java ---------------------------------------------------------------------- diff --git a/juneau-core/src/main/java/org/apache/juneau/PropertyNamerDashedLC.java b/juneau-core/src/main/java/org/apache/juneau/PropertyNamerDashedLC.java deleted file mode 100644 index 6829262..0000000 --- a/juneau-core/src/main/java/org/apache/juneau/PropertyNamerDashedLC.java +++ /dev/null @@ -1,64 +0,0 @@ -// *************************************************************************************************************************** -// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file * -// * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file * -// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance * -// * with the License. You may obtain a copy of the License at * -// * * -// * http://www.apache.org/licenses/LICENSE-2.0 * -// * * -// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an * -// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the * -// * specific language governing permissions and limitations under the License. * -// *************************************************************************************************************************** -package org.apache.juneau; - -/** - * Converts property names to dashed-lower-case format. - * - * <h5 class='section'>Examples:</h5> - * <ul> - * <li><js>"fooBar"</js> -> <js>"foo-bar"</js> - * <li><js>"fooBarURL"</js> -> <js>"foo-bar-url"</js> - * <li><js>"FooBarURL"</js> -> <js>"foo-bar-url"</js> - * </ul> - */ -public final class PropertyNamerDashedLC implements PropertyNamer { - - @Override /* PropertyNamer */ - public String getPropertyName(String name) { - if (name == null || name.isEmpty()) - return name; - - int numUCs = 0; - boolean isPrevUC = Character.isUpperCase(name.charAt(0)); - for (int i = 1; i < name.length(); i++) { - char c = name.charAt(i); - if (Character.isUpperCase(c)) { - if (! isPrevUC) - numUCs++; - isPrevUC = true; - } else { - isPrevUC = false; - } - } - - char[] name2 = new char[name.length() + numUCs]; - isPrevUC = Character.isUpperCase(name.charAt(0)); - name2[0] = Character.toLowerCase(name.charAt(0)); - int ni = 0; - for (int i = 0; i < name.length(); i++) { - char c = name.charAt(i); - if (Character.isUpperCase(c)) { - if (! isPrevUC) - name2[ni++] = '-'; - isPrevUC = true; - name2[ni++] = Character.toLowerCase(c); - } else { - isPrevUC = false; - name2[ni++] = c; - } - } - - return new String(name2); - } -} http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/0f030b18/juneau-core/src/main/java/org/apache/juneau/annotation/Bean.java ---------------------------------------------------------------------- diff --git a/juneau-core/src/main/java/org/apache/juneau/annotation/Bean.java b/juneau-core/src/main/java/org/apache/juneau/annotation/Bean.java index 9c61189..7e89c07 100644 --- a/juneau-core/src/main/java/org/apache/juneau/annotation/Bean.java +++ b/juneau-core/src/main/java/org/apache/juneau/annotation/Bean.java @@ -181,7 +181,7 @@ public @interface Bean { * Associates a {@link PropertyNamer} with this bean to tailor the names of the bean properties. * <p> * Property namers are used to transform bean property names from standard form to some other form. - * For example, the {@link PropertyNamerDashedLC} will convert property names to dashed-lowercase, and + * For example, the {@link PropertyNamerDLC} will convert property names to dashed-lowercase, and * these will be used as attribute names in JSON, and element names in XML. * <p> * This annotation is an alternative to using the {@link BeanFilter} class with an implemented {@link BeanFilter#getPropertyNamer()} method.
