这个包的说明是:Support for styling values as Strings, with ToStringCreator as central class.
这个包简单来说就是提供一个pretty-printing功能的辅助类,而ToStringCreator就是用于产生一个可以输出经过美化的value信息的toString()方法。使用方法参照spring的Test可以看到是这样:
<!----> int[] integers = new int[] { 0, 1, 2, 3, 4 };
String str = new ToStringCreator(integers).toString();
assertEquals("[@" + ObjectUtils.getIdentityHexString(integers) + " array<integer>[0, 1, 2, 3, 4]]</integer>", str);
或者写个简单例子感受下:
<!---->int [] a={1,2,3,4,5,6,7,8,9};
System.out.println(new ToStringCreator(a).toString());
输出:
<!---->[@18558d2 array<Integer>[1, 2, 3, 4, 5, 6, 7, 8, 9]]
如果你接触过ruby,你应该很熟悉Object.inpsect这个功能,这里通过ToStringCreator包装的toString()方法也是产生类似的能够清晰显示对象内部结构信息的方法。spring应该是使用这些辅助类来报告清晰的错误信息或者提示信息。
看看这个包的UML类图:
首先,你需要理解ToStringStyler和ValueStyle两个接口,ToStringStyler定义了描述一个输入的Value信息的基本模板方法:
<!---->public interface ToStringStyler {
/**
* Style a toString()
'ed object before its fields are styled.
* @param buffer the buffer to print to
* @param obj the object to style
*/
void styleStart(StringBuffer buffer, Object obj);
/**
* Style a toString()
'ed object after it's fields are styled.
* @param buffer the buffer to print to
* @param obj the object to style
*/
void styleEnd(StringBuffer buffer, Object obj);
/**
* Style a field value as a string.
* @param buffer the buffer to print to
* @param fieldName the he name of the field
* @param value the field value
*/
void styleField(StringBuffer buffer, String fieldName, Object value);
/**
* Style the given value.
* @param buffer the buffer to print to
* @param value the field value
*/
void styleValue(StringBuffer buffer, Object value);
/**
* Style the field separator.
* @param buffer buffer to print to
*/
void styleFieldSeparator(StringBuffer buffer);
}
这是典型的Template Method模式,而两个接口ToStringStyler、ValueStyler和它们的相应实现DefaultToStringStyler、DefaultValueStyler又是策略模式(Strategy)的应用体现。ValueStyler和DefaultValueStyler之间不仅仅是策略模式,同时也是visitor模式,请看DefaultValueStyler中一系列重载的visit方法,这些visit方法访问不同类型Value的内部结构并构造pretty格式的String返回,提供给ToStringStyler使用。
ToStringCreator是ToStringStyler的客户,它使用ToStringStyler调用产生优美格式打印,而ToStringStyler 其实又是使用ValueStyler是访问每个不同类型的子元素并返回优美格式的String。实现的相当精巧和灵活:
<!---->
public ToStringCreator(Object obj, ToStringStyler styler) {
Assert.notNull(obj, "The object to be styled is required");
this.object = obj;
this.styler = (styler != null ? styler : DEFAULT_TO_STRING_STYLER);
//开始 this.styler.styleStart(this.buffer, this.object);
}
public ToStringCreator append(String fieldName, byte value) {
return append(fieldName, new Byte(value));
}
一系列不同类型的append方法
public String toString() {
//结束,并返回优美格式的String this.styler.styleEnd(this.buffer, this.object);
return this.buffer.toString();
}
分享到:
相关推荐
资源来自pypi官网。 资源全名:styler_rest_framework-1.4.2.tar.gz
资源来自pypi官网。 资源全名:styler_rest_framework-1.8.1.tar.gz
资源来自pypi官网。 资源全名:styler_rest_framework-1.1.0-py2.py3-none-any.whl
Laravel开发-styler Laravel的CSS样式器包。实时编译更少的/scs,并支持为缓存内容提供服务。
python库,解压后可用。 资源全名:styler_rest_framework-1.1.0-py2.py3-none-any.whl
资源来自pypi官网。 资源全名:styler_rest_framework-1.3.0-py2.py3-none-any.whl
使用以下方法扩展pandas.io.formats.style.Styler类: dc_table dc_headers dc_peak dc_sign dc_row_divider dc_col_divider dc_heatmap dc_even_uneven dc_add_title dc_make_col_int dc_render
geoserver的styler 样式选择器插件
UGOPENAPI二次开发之UIStyler开发[整理].pdf
matlab中云滴代码时间序列的数学 这个库的目标是包含一堆主要用于金融的常见时间序列分析工具和指标。 内容一览 构建说明 您可以使用 Maven 或 Gradle ...org.knowm.xchart.style.Styler import or
资源分类:Python库 所属语言:Python 资源全名:styler_identity-0.5.1.tar.gz 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059
styler安装版,里面集成了部分漂亮的工具栏·~希望大家喜欢~!1!
UGOPENAPI二次开发之UIStyler开发.pdf
2、美化包包含:系统主题(vs)、IP图标包、壁纸、styler工具栏样式、登陆界面、鼠标指针、系统字体、阴影工具。 3、美化包内采用DIY式安装,让你在玩美化的时候还能系统的学习美化知识,不像那些一键安装傻瓜美化包...
xp系统完美仿win7窗口 皮肤资源收集 其中wxvista 绿(宋体)推荐使用 配合qtaddressbar 非常好
3D-calcite-maps-styler-template.zip,用于创建、设置样式和共享现代二维和三维地图应用程序的Arcgis可配置应用程序。使用arcgis为javascript api v4、方解石地图和引导程序构建。,3D建模使用专门的软件来创建物理...
资源分类:Python库 所属语言:Python 资源全名:styler_rest_framework-0.3.0-py2.py3-none-any.whl 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059
利用UIStyler开发UG中对应的菜单栏工具栏
Devtools Styler可以更改chrome devtools字体和字体大小。 问题:如果在设置devtools字体后,devtools选项卡的标题中带有省略号,请放大和缩小devtools以正确恢复选项卡的详细信息安装前的步骤:1.打开新选项卡,...