- 浏览: 7135 次
- 性别:
- 来自: 北京
最新评论
-
iminto:
我在本地测试不成功。http://www.iteye.com/ ...
org.apache.commons.lang.builder——ReflectionToStringBuilder类 -
sywxy:
先顶再看。
浅析Javascript原型继承
//本示例来自Jakarta Commons Cookbook public class PoliticalCandidate { private String lastName; private String firstName; private Date dateOfBirth; private BigDecimal moneyRaised; private State homeState; // get/set methods 省略 public void toString( ) { return new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE) .append("lastName", lastName) .append("firstName", firstName) .toString( ); } } //结果输出 com.discursive.jccook.lang.builders.PoliticalCandidate@1cd2e5f[ lastName=Jefferson firstName=Thomas ] //下面是几个默认的style的打印结果 ToStringStyle.DEFAULT_STYLE com.discursive.jccook.lang.builders. PoliticalCandidate@1cd2e5f[lastName=Jefferson,firstName=Thomas] ToStringStyle.MULTI_LINE_STYLE com.discursive.jccook.lang.builders.PoliticalCandidate@1cd2e5f[ lastName=Jefferson firstName=Thomas ] ToStringStyle.NO_FIELD_NAMES_STYLE com.discursive.jccook.lang.builders.PoliticalCandidate@1cd2e5f[Jefferson,Thomas] ToStringStyle.SIMPLE_STYLE Jefferson,Thomas
像NO_FIELD_NAMES_STYLE,SIMPLE_STYLE这些都是ToStringTyle的内部类,继承自ToStringStyle,重写了一些属性。
下面看一下ToStringBuilder的源码:
package org.apache.commons.lang.builder; import org.apache.commons.lang.BooleanUtils; import org.apache.commons.lang.ObjectUtils; public class ToStringBuilder { //默认的样式 private static ToStringStyle defaultStyle = ToStringStyle.DEFAULT_STYLE; public static ToStringStyle getDefaultStyle() { return defaultStyle; } //下面几个方法都是简单的调用ReflectionToStringBuilder的toString方法 public static String reflectionToString(Object object) { return ReflectionToStringBuilder.toString(object); } public static String reflectionToString(Object object, ToStringStyle style) { return ReflectionToStringBuilder.toString(object, style); } public static String reflectionToString(Object object, ToStringStyle style, boolean outputTransients) { return ReflectionToStringBuilder.toString(object, style, outputTransients, false, null); } public static String reflectionToString( Object object, ToStringStyle style, boolean outputTransients, Class reflectUpToClass) { return ReflectionToStringBuilder.toString(object, style, outputTransients, false, reflectUpToClass); } public static void setDefaultStyle(ToStringStyle style) { if (style == null) { throw new IllegalArgumentException("The style must not be null"); } defaultStyle = style; } //最后输出的字符串缓存 private final StringBuffer buffer; private final Object object; private final ToStringStyle style; public ToStringBuilder(Object object) { this(object, getDefaultStyle(), null); } public ToStringBuilder(Object object, ToStringStyle style) { this(object, style, null); } public ToStringBuilder(Object object, ToStringStyle style, StringBuffer buffer) { if (style == null) { style = getDefaultStyle(); } if (buffer == null) { buffer = new StringBuffer(512); } this.buffer = buffer; this.style = style; this.object = object; //这里append上开始字符 style.appendStart(buffer, object); } //ToStringBuilder中的append方法都是委托给ToStringStyle进行处理的 //并且返回一个ToStringBuilder实例,这样就可以进行链式操作了。 //例如:new ToStringBuilder().append().append().append()...... public ToStringBuilder append(boolean value) { style.append(buffer, null, value); return this; } //---------------------------------------------------------------------------- public ToStringBuilder append(boolean[] array) { style.append(buffer, null, array, null); return this; } //---------------------------------------------------------------------------- public ToStringBuilder append(byte value) { style.append(buffer, null, value); return this; } //---------------------------------------------------------------------------- public ToStringBuilder append(byte[] array) { style.append(buffer, null, array, null); return this; } //---------------------------------------------------------------------------- public ToStringBuilder append(char value) { style.append(buffer, null, value); return this; } //---------------------------------------------------------------------------- public ToStringBuilder append(char[] array) { style.append(buffer, null, array, null); return this; } //---------------------------------------------------------------------------- public ToStringBuilder append(double value) { style.append(buffer, null, value); return this; } //---------------------------------------------------------------------------- public ToStringBuilder append(double[] array) { style.append(buffer, null, array, null); return this; } //---------------------------------------------------------------------------- public ToStringBuilder append(float value) { style.append(buffer, null, value); return this; } //---------------------------------------------------------------------------- public ToStringBuilder append(float[] array) { style.append(buffer, null, array, null); return this; } //---------------------------------------------------------------------------- public ToStringBuilder append(int value) { style.append(buffer, null, value); return this; } //---------------------------------------------------------------------------- public ToStringBuilder append(int[] array) { style.append(buffer, null, array, null); return this; } //---------------------------------------------------------------------------- public ToStringBuilder append(long value) { style.append(buffer, null, value); return this; } //---------------------------------------------------------------------------- public ToStringBuilder append(long[] array) { style.append(buffer, null, array, null); return this; } //---------------------------------------------------------------------------- public ToStringBuilder append(Object obj) { style.append(buffer, null, obj, null); return this; } //---------------------------------------------------------------------------- public ToStringBuilder append(Object[] array) { style.append(buffer, null, array, null); return this; } //---------------------------------------------------------------------------- public ToStringBuilder append(short value) { style.append(buffer, null, value); return this; } //---------------------------------------------------------------------------- public ToStringBuilder append(short[] array) { style.append(buffer, null, array, null); return this; } public ToStringBuilder append(String fieldName, boolean value) { style.append(buffer, fieldName, value); return this; } public ToStringBuilder append(String fieldName, boolean[] array) { style.append(buffer, fieldName, array, null); return this; } public ToStringBuilder append(String fieldName, boolean[] array, boolean fullDetail) { style.append(buffer, fieldName, array, BooleanUtils.toBooleanObject(fullDetail)); return this; } public ToStringBuilder append(String fieldName, byte value) { style.append(buffer, fieldName, value); return this; } public ToStringBuilder append(String fieldName, byte[] array) { style.append(buffer, fieldName, array, null); return this; } public ToStringBuilder append(String fieldName, byte[] array, boolean fullDetail) { style.append(buffer, fieldName, array, BooleanUtils.toBooleanObject(fullDetail)); return this; } public ToStringBuilder append(String fieldName, char value) { style.append(buffer, fieldName, value); return this; } public ToStringBuilder append(String fieldName, char[] array) { style.append(buffer, fieldName, array, null); return this; } public ToStringBuilder append(String fieldName, char[] array, boolean fullDetail) { style.append(buffer, fieldName, array, BooleanUtils.toBooleanObject(fullDetail)); return this; } public ToStringBuilder append(String fieldName, double value) { style.append(buffer, fieldName, value); return this; } public ToStringBuilder append(String fieldName, double[] array) { style.append(buffer, fieldName, array, null); return this; } public ToStringBuilder append(String fieldName, double[] array, boolean fullDetail) { style.append(buffer, fieldName, array, BooleanUtils.toBooleanObject(fullDetail)); return this; } public ToStringBuilder append(String fieldName, float value) { style.append(buffer, fieldName, value); return this; } public ToStringBuilder append(String fieldName, float[] array) { style.append(buffer, fieldName, array, null); return this; } public ToStringBuilder append(String fieldName, float[] array, boolean fullDetail) { style.append(buffer, fieldName, array, BooleanUtils.toBooleanObject(fullDetail)); return this; } public ToStringBuilder append(String fieldName, int value) { style.append(buffer, fieldName, value); return this; } public ToStringBuilder append(String fieldName, int[] array) { style.append(buffer, fieldName, array, null); return this; } public ToStringBuilder append(String fieldName, int[] array, boolean fullDetail) { style.append(buffer, fieldName, array, BooleanUtils.toBooleanObject(fullDetail)); return this; } public ToStringBuilder append(String fieldName, long value) { style.append(buffer, fieldName, value); return this; } public ToStringBuilder append(String fieldName, long[] array) { style.append(buffer, fieldName, array, null); return this; } public ToStringBuilder append(String fieldName, long[] array, boolean fullDetail) { style.append(buffer, fieldName, array, BooleanUtils.toBooleanObject(fullDetail)); return this; } public ToStringBuilder append(String fieldName, Object obj) { style.append(buffer, fieldName, obj, null); return this; } public ToStringBuilder append(String fieldName, Object obj, boolean fullDetail) { style.append(buffer, fieldName, obj, BooleanUtils.toBooleanObject(fullDetail)); return this; } public ToStringBuilder append(String fieldName, Object[] array) { style.append(buffer, fieldName, array, null); return this; } public ToStringBuilder append(String fieldName, Object[] array, boolean fullDetail) { style.append(buffer, fieldName, array, BooleanUtils.toBooleanObject(fullDetail)); return this; } public ToStringBuilder append(String fieldName, short value) { style.append(buffer, fieldName, value); return this; } public ToStringBuilder append(String fieldName, short[] array) { style.append(buffer, fieldName, array, null); return this; } public ToStringBuilder append(String fieldName, short[] array, boolean fullDetail) { style.append(buffer, fieldName, array, BooleanUtils.toBooleanObject(fullDetail)); return this; } //appendIdentityToString是一个被deprecated的方法,它的作用就是返回一个对象的类型+hashcode的字符串 public ToStringBuilder appendAsObjectToString(Object object) { ObjectUtils.appendIdentityToString(this.getStringBuffer(), object); return this; } //---------------------------------------------------------------------------- public ToStringBuilder appendSuper(String superToString) { if (superToString != null) { style.appendSuper(buffer, superToString); } return this; } public ToStringBuilder appendToString(String toString) { if (toString != null) { style.appendToString(buffer, toString); } return this; } public Object getObject() { return object; } public StringBuffer getStringBuffer() { return buffer; } //---------------------------------------------------------------------------- public ToStringStyle getStyle() { return style; } //最后append上结束字符,并返回最后的格式字符串 public String toString() { if (this.getObject() == null) { this.getStringBuffer().append(this.getStyle().getNullText()); } else { style.appendEnd(this.getStringBuffer(), this.getObject()); } return this.getStringBuffer().toString(); } }
我们可以看到非常清楚的SRP(单一职责原则)的应用。ToStringBuilder就是负责存储字符串,至于应该以什么格式存储字符串,则交给ToStringStyle去完成。所以这个类的大部分工作都委托给了ToStringStyle
相关推荐
org.apache.commons.lang.builder.ToStringBuilder.class org.apache.commons.lang.builder.ToStringStyle$DefaultToStringStyle.class org.apache.commons.lang.builder.ToStringStyle$MultiLineToStringStyle....
org.apache.commons.lang.builder.ToStringBuilder.class org.apache.commons.lang.builder.ToStringStyle$DefaultToStringStyle.class org.apache.commons.lang.builder.ToStringStyle$MultiLineToStringStyle....
org.apache.commons.lang.builder.ToStringBuilder.class org.apache.commons.lang.builder.ToStringStyle$DefaultToStringStyle.class org.apache.commons.lang.builder.ToStringStyle$MultiLineToStringStyle....
2.1.1.3 org.apache.commons.lang.builder 这个包主要是为了提供对象构建和比较的工具,如ToStringBuilder、HashCodeBuilder和EqualsBuilder,它们帮助开发者生成对象的toString、hashCode和equals方法,避免了重复...
Apache Commons Lang 3.7 包含多个子包,如 `org.apache.commons.lang3`, `org.apache.commons.lang3.builder`, `org.apache.commons.lang3.text` 等,每个子包都专注于特定的功能领域。例如,`builder` 包提供了...
描述中的"hashCodeBuilder EqualsBuilder ToStringBuilder"提到了三个关键的类,它们都是Apache Commons Lang中用于构建对象哈希码、比较平等性和生成字符串表示的重要工具。 1. **HashCodeBuilder**: 在Java中,`...
### Apache Commons Jar包常用类详解 #### 概述 Apache Commons项目是一系列为Java开发者提供的免费、可重用的开源库。这些库旨在提供解决常见编程任务的实用工具类,帮助开发者减少编码工作量,提高代码质量。...
4. **Apache Commons Lang**: Apache Commons Lang库虽然不是专门处理JSON的,但它的`org.apache.commons.lang3.builder.ToStringBuilder`和`ReflectionToStringBuilder`可以帮助生成对象的JSON表示,对于调试和日志...
***mons Lang还包含commons.lang.builder包,其中提供了一些用于构建对象的辅助工具类,如: - CompareToBuilder:用于实现Comparable接口时快速构建比较逻辑。 - EqualsBuilder:用于快速构建equals()方法逻辑。...
import org.apache.commons.lang3.builder.ToStringBuilder; public class MyClass { private int id; private String name; @Override public String toString() { return new ToStringBuilder(this) ....
ToStringBuilder是包`org.apache.commons.lang3.builder`中的一个类,它的主要功能是帮助我们构建对象的toString方法,以优雅地展示对象的状态。在没有ToStringBuilder的情况下,我们通常需要手动拼接字符串,这不仅...
import org.apache.commons.lang3.builder.ToStringBuilder; @Override public String toString() { return ToStringBuilder.reflectionToString(this); } ``` 4. **Java 8及以后的默认实现** 从Java 8开始,`...
6. **org.apache.commons.lang3.builder.ToStringBuilder**:虽然Apache Commons Lang主要不是为了解析JSON,但它可以帮助你生成人类可读的字符串,包括JSON格式的表示,可以作为辅助工具。 在这些库中,`...
接着,利用`JSONArray.fromObject()`方法(注意:这里的描述有误,应使用`JSONArray`构造函数或`JSONArray.fromObject()`方法,但`fromObject()`是`org.apache.commons.lang3.builder.ToStringBuilder`的方法,而...
再者,`org.apache.commons.lang3.builder.ToStringBuilder`可能用于生成JSON友好的对象表示,这在调试和日志记录中非常有用,尤其是在大型对象层次结构中。 另外两个可能包含的包是`javax.json`和`...
##### commons.lang.builder包 此包提供了构建Java Bean的工具类,使得可以轻松创建具有正确equals()、hashCode()和toString()实现的类。 ##### CompareToBuilder **CompareToBuilder** 类用于构建可比较对象的...
import org.apache.commons.lang3.builder.ToStringBuilder; import org.apache.commons.lang3.builder.ToStringStyle; public class User1 implements Serializable { private String name; private int age; ...