Author: britter Date: Wed Dec 31 16:34:49 2014 New Revision: 1648730 URL: http://svn.apache.org/r1648730 Log: LANG-1080: add NoClassNameToStringStyle implementation of ToStringStyle. This closes #40 from github. Thanks to Innokenty Shuvalov.
Added: commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/builder/NoClassNameToStringStyleTest.java (with props) Modified: commons/proper/lang/trunk/src/changes/changes.xml commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/builder/ToStringStyle.java Modified: commons/proper/lang/trunk/src/changes/changes.xml URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/changes/changes.xml?rev=1648730&r1=1648729&r2=1648730&view=diff ============================================================================== --- commons/proper/lang/trunk/src/changes/changes.xml [utf-8] (original) +++ commons/proper/lang/trunk/src/changes/changes.xml [utf-8] Wed Dec 31 16:34:49 2014 @@ -22,6 +22,7 @@ <body> <release version="3.4" date="tba" description="tba"> + <action issue="LANG-1080" type="add" dev="britter" due-to="Innokenty Shuvalov">Add NoClassNameToStringStyle implementation of ToStringStyle</action> <action issue="LANG-1071" type="update" dev="britter" due-to="Arno Noordover">Fix wrong examples in JavaDoc of StringUtils.replaceEachRepeatedly(...), StringUtils.replaceEach(...)</action> <action issue="LANG-883" type="add" dev="britter" due-to="Daniel Stewart">Add StringUtils.containsAny(CharSequence, CharSequence...) method</action> <action issue="LANG-1073" type="fix" dev="kinow" due-to="haiyang li">Read wrong component type of array in add in ArrayUtils</action> Modified: commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/builder/ToStringStyle.java URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/builder/ToStringStyle.java?rev=1648730&r1=1648729&r2=1648730&view=diff ============================================================================== --- commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/builder/ToStringStyle.java (original) +++ commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/builder/ToStringStyle.java Wed Dec 31 16:34:49 2014 @@ -71,7 +71,7 @@ public abstract class ToStringStyle impl private static final long serialVersionUID = -2587890625525655916L; /** - * The default toString style. Using the Using the <code>Person</code> + * The default toString style. Using the <code>Person</code> * example from {@link ToStringBuilder}, the output would look like this: * * <pre> @@ -95,7 +95,7 @@ public abstract class ToStringStyle impl public static final ToStringStyle MULTI_LINE_STYLE = new MultiLineToStringStyle(); /** - * The no field names toString style. Using the Using the + * The no field names toString style. Using the * <code>Person</code> example from {@link ToStringBuilder}, the output * would look like this: * @@ -118,7 +118,7 @@ public abstract class ToStringStyle impl public static final ToStringStyle SHORT_PREFIX_STYLE = new ShortPrefixToStringStyle(); /** - * The simple toString style. Using the Using the <code>Person</code> + * The simple toString style. Using the <code>Person</code> * example from {@link ToStringBuilder}, the output would look like this: * * <pre> @@ -128,6 +128,18 @@ public abstract class ToStringStyle impl public static final ToStringStyle SIMPLE_STYLE = new SimpleToStringStyle(); /** + * The no class name toString style. Using the <code>Person</code> + * example from {@link ToStringBuilder}, the output would look like this: + * + * <pre> + * [name=John Doe,age=33,smoker=false] + * </pre> + * + * @since 3.4 + */ + public static final ToStringStyle NO_CLASS_NAME_STYLE = new NoClassNameToStringStyle(); + + /** * <p> * A registry of objects used by <code>reflectionToString</code> methods * to detect cyclical object references and avoid infinite loops. @@ -2278,5 +2290,40 @@ public abstract class ToStringStyle impl } } + + //---------------------------------------------------------------------------- + + /** + * <p><code>ToStringStyle</code> that does not print out the classname + * and identity hashcode but prints content start and field names.</p> + * + * <p>This is an inner class rather than using + * <code>StandardToStringStyle</code> to ensure its immutability.</p> + */ + private static final class NoClassNameToStringStyle extends ToStringStyle { + + private static final long serialVersionUID = 1L; + + /** + * <p>Constructor.</p> + * + * <p>Use the static constant rather than instantiating.</p> + */ + NoClassNameToStringStyle() { + super(); + this.setUseClassName(false); + this.setUseIdentityHashCode(false); + } + + /** + * <p>Ensure <code>Singleton</code> after serialization.</p> + * + * @return the singleton + */ + private Object readResolve() { + return ToStringStyle.NO_CLASS_NAME_STYLE; + } + + } } Added: commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/builder/NoClassNameToStringStyleTest.java URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/builder/NoClassNameToStringStyleTest.java?rev=1648730&view=auto ============================================================================== --- commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/builder/NoClassNameToStringStyleTest.java (added) +++ commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/builder/NoClassNameToStringStyleTest.java Wed Dec 31 16:34:49 2014 @@ -0,0 +1,129 @@ +/* + * 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.commons.lang3.builder; + +import org.apache.commons.lang3.builder.ToStringStyleTest.Person; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import java.util.ArrayList; +import java.util.HashMap; + +import static org.junit.Assert.assertEquals; + +/** + * Unit tests {@link ToStringStyle#NO_CLASS_NAME_STYLE}. + * + * @version $Id$ + */ +public class NoClassNameToStringStyleTest { + + private final Integer base = Integer.valueOf(5); + + @Before + public void setUp() throws Exception { + ToStringBuilder.setDefaultStyle(ToStringStyle.NO_CLASS_NAME_STYLE); + } + + @After + public void tearDown() throws Exception { + ToStringBuilder.setDefaultStyle(ToStringStyle.DEFAULT_STYLE); + } + + //---------------------------------------------------------------- + + @Test + public void testBlank() { + assertEquals("[]", new ToStringBuilder(base).toString()); + } + + @Test + public void testAppendSuper() { + assertEquals("[]", new ToStringBuilder(base).appendSuper("Integer@8888[]").toString()); + assertEquals("[<null>]", new ToStringBuilder(base).appendSuper("Integer@8888[<null>]").toString()); + + assertEquals("[a=hello]", new ToStringBuilder(base).appendSuper("Integer@8888[]").append("a", "hello").toString()); + assertEquals("[<null>,a=hello]", new ToStringBuilder(base).appendSuper("Integer@8888[<null>]").append("a", "hello").toString()); + assertEquals("[a=hello]", new ToStringBuilder(base).appendSuper(null).append("a", "hello").toString()); + } + + @Test + public void testObject() { + final Integer i3 = Integer.valueOf(3); + final Integer i4 = Integer.valueOf(4); + assertEquals("[<null>]", new ToStringBuilder(base).append((Object) null).toString()); + assertEquals("[3]", new ToStringBuilder(base).append(i3).toString()); + assertEquals("[a=<null>]", new ToStringBuilder(base).append("a", (Object) null).toString()); + assertEquals("[a=3]", new ToStringBuilder(base).append("a", i3).toString()); + assertEquals("[a=3,b=4]", new ToStringBuilder(base).append("a", i3).append("b", i4).toString()); + assertEquals("[a=<Integer>]", new ToStringBuilder(base).append("a", i3, false).toString()); + assertEquals("[a=<size=0>]", new ToStringBuilder(base).append("a", new ArrayList<Object>(), false).toString()); + assertEquals("[a=[]]", new ToStringBuilder(base).append("a", new ArrayList<Object>(), true).toString()); + assertEquals("[a=<size=0>]", new ToStringBuilder(base).append("a", new HashMap<Object, Object>(), false).toString()); + assertEquals("[a={}]", new ToStringBuilder(base).append("a", new HashMap<Object, Object>(), true).toString()); + assertEquals("[a=<size=0>]", new ToStringBuilder(base).append("a", (Object) new String[0], false).toString()); + assertEquals("[a={}]", new ToStringBuilder(base).append("a", (Object) new String[0], true).toString()); + } + + @Test + public void testPerson() { + final Person p = new Person(); + p.name = "John Q. Public"; + p.age = 45; + p.smoker = true; + assertEquals("[name=John Q. Public,age=45,smoker=true]", new ToStringBuilder(p).append("name", p.name).append("age", p.age).append("smoker", p.smoker).toString()); + } + + @Test + public void testLong() { + assertEquals("[3]", new ToStringBuilder(base).append(3L).toString()); + assertEquals("[a=3]", new ToStringBuilder(base).append("a", 3L).toString()); + assertEquals("[a=3,b=4]", new ToStringBuilder(base).append("a", 3L).append("b", 4L).toString()); + } + + @Test + public void testObjectArray() { + Object[] array = new Object[] {null, base, new int[] {3, 6}}; + assertEquals("[{<null>,5,{3,6}}]", new ToStringBuilder(base).append(array).toString()); + assertEquals("[{<null>,5,{3,6}}]", new ToStringBuilder(base).append((Object) array).toString()); + array = null; + assertEquals("[<null>]", new ToStringBuilder(base).append(array).toString()); + assertEquals("[<null>]", new ToStringBuilder(base).append((Object) array).toString()); + } + + @Test + public void testLongArray() { + long[] array = new long[] {1, 2, -3, 4}; + assertEquals("[{1,2,-3,4}]", new ToStringBuilder(base).append(array).toString()); + assertEquals("[{1,2,-3,4}]", new ToStringBuilder(base).append((Object) array).toString()); + array = null; + assertEquals("[<null>]", new ToStringBuilder(base).append(array).toString()); + assertEquals("[<null>]", new ToStringBuilder(base).append((Object) array).toString()); + } + + @Test + public void testLongArrayArray() { + long[][] array = new long[][] {{1, 2}, null, {5}}; + assertEquals("[{{1,2},<null>,{5}}]", new ToStringBuilder(base).append(array).toString()); + assertEquals("[{{1,2},<null>,{5}}]", new ToStringBuilder(base).append((Object) array).toString()); + array = null; + assertEquals("[<null>]", new ToStringBuilder(base).append(array).toString()); + assertEquals("[<null>]", new ToStringBuilder(base).append((Object) array).toString()); + } + +} Propchange: commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/builder/NoClassNameToStringStyleTest.java ------------------------------------------------------------------------------ svn:keywords = Date Author Id Revision HeadURL