您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

CustomToStringStyle.java 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*
  2. Copyright (c) 2013 James Ahlborn
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package com.healthmarketscience.jackcess.impl;
  14. import java.nio.ByteBuffer;
  15. import java.util.Collection;
  16. import java.util.Iterator;
  17. import java.util.Map;
  18. import org.apache.commons.lang3.builder.StandardToStringStyle;
  19. import org.apache.commons.lang3.builder.ToStringBuilder;
  20. /**
  21. * Custom ToStringStyle for use with ToStringBuilder.
  22. *
  23. * @author James Ahlborn
  24. */
  25. public class CustomToStringStyle extends StandardToStringStyle
  26. {
  27. private static final long serialVersionUID = 0L;
  28. private static final String ML_FIELD_SEP = System.lineSeparator() + " ";
  29. private static final String IMPL_SUFFIX = "Impl";
  30. private static final int MAX_BYTE_DETAIL_LEN = 20;
  31. private static final Object IGNORE_ME = new Object();
  32. public static final CustomToStringStyle INSTANCE = new CustomToStringStyle() {
  33. private static final long serialVersionUID = 0L;
  34. {
  35. setContentStart("[");
  36. setFieldSeparator(ML_FIELD_SEP);
  37. setFieldSeparatorAtStart(true);
  38. setFieldNameValueSeparator(": ");
  39. setArraySeparator("," + ML_FIELD_SEP);
  40. setContentEnd(System.lineSeparator() + "]");
  41. setUseShortClassName(true);
  42. }
  43. };
  44. public static final CustomToStringStyle VALUE_INSTANCE = new CustomToStringStyle() {
  45. private static final long serialVersionUID = 0L;
  46. {
  47. setUseShortClassName(true);
  48. setUseIdentityHashCode(false);
  49. }
  50. };
  51. private CustomToStringStyle() {
  52. }
  53. public static ToStringBuilder builder(Object obj) {
  54. return new ToStringBuilder(obj, INSTANCE);
  55. }
  56. public static ToStringBuilder valueBuilder(Object obj) {
  57. return new ToStringBuilder(obj, VALUE_INSTANCE);
  58. }
  59. @Override
  60. public void append(StringBuffer buffer, String fieldName, Object value,
  61. Boolean fullDetail) {
  62. if(value == IGNORE_ME) {
  63. return;
  64. }
  65. super.append(buffer, fieldName, value, fullDetail);
  66. }
  67. @Override
  68. protected void appendClassName(StringBuffer buffer, Object obj) {
  69. if(obj instanceof String) {
  70. // the caller gave an "explicit" class name
  71. buffer.append(obj);
  72. } else {
  73. super.appendClassName(buffer, obj);
  74. }
  75. }
  76. @Override
  77. protected String getShortClassName(Class<?> clss) {
  78. String shortName = super.getShortClassName(clss);
  79. if(shortName.endsWith(IMPL_SUFFIX)) {
  80. shortName = shortName.substring(0,
  81. shortName.length() - IMPL_SUFFIX.length());
  82. }
  83. int idx = shortName.lastIndexOf('.');
  84. if(idx >= 0) {
  85. shortName = shortName.substring(idx + 1);
  86. }
  87. return shortName;
  88. }
  89. @Override
  90. protected void appendDetail(StringBuffer buffer, String fieldName,
  91. Object value) {
  92. if(value instanceof ByteBuffer) {
  93. appendDetail(buffer, (ByteBuffer)value);
  94. } else {
  95. buffer.append(indent(value));
  96. }
  97. }
  98. @Override
  99. protected void appendDetail(StringBuffer buffer, String fieldName,
  100. Collection<?> value) {
  101. buffer.append("[");
  102. // gather contents of list in a new StringBuffer
  103. StringBuffer sb = new StringBuffer();
  104. Iterator<?> iter = value.iterator();
  105. if(iter.hasNext()) {
  106. if(isFieldSeparatorAtStart()) {
  107. appendFieldSeparator(sb);
  108. }
  109. appendValueDetail(sb, fieldName, iter.next());
  110. }
  111. while(iter.hasNext()) {
  112. sb.append(getArraySeparator());
  113. appendValueDetail(sb, fieldName, iter.next());
  114. }
  115. // indent entire list contents another level
  116. buffer.append(indent(sb));
  117. if(isFieldSeparatorAtStart()) {
  118. appendFieldSeparator(buffer);
  119. }
  120. buffer.append("]");
  121. }
  122. @Override
  123. protected void appendDetail(StringBuffer buffer, String fieldName,
  124. Map<?,?> value) {
  125. buffer.append("{");
  126. // gather contents of map in a new StringBuffer
  127. StringBuffer sb = new StringBuffer();
  128. Iterator<? extends Map.Entry<?,?>> iter = value.entrySet().iterator();
  129. if(iter.hasNext()) {
  130. if(isFieldSeparatorAtStart()) {
  131. appendFieldSeparator(sb);
  132. }
  133. Map.Entry<?,?> e = iter.next();
  134. sb.append(e.getKey()).append("=");
  135. appendValueDetail(sb, fieldName, e.getValue());
  136. }
  137. while(iter.hasNext()) {
  138. sb.append(getArraySeparator());
  139. Map.Entry<?,?> e = iter.next();
  140. sb.append(e.getKey()).append("=");
  141. appendValueDetail(sb, fieldName, e.getValue());
  142. }
  143. // indent entire map contents another level
  144. buffer.append(indent(sb));
  145. if(isFieldSeparatorAtStart()) {
  146. appendFieldSeparator(buffer);
  147. }
  148. buffer.append("}");
  149. }
  150. @Override
  151. protected void appendDetail(StringBuffer buffer, String fieldName,
  152. byte[] array) {
  153. appendDetail(buffer, PageChannel.wrap(array));
  154. }
  155. private void appendValueDetail(StringBuffer buffer, String fieldName,
  156. Object value) {
  157. if (value == null) {
  158. appendNullText(buffer, fieldName);
  159. } else {
  160. appendInternal(buffer, fieldName, value, true);
  161. }
  162. }
  163. private static void appendDetail(StringBuffer buffer, ByteBuffer bb) {
  164. int len = bb.remaining();
  165. buffer.append("(").append(len).append(") ");
  166. buffer.append(ByteUtil.toHexString(bb, bb.position(),
  167. Math.min(len, MAX_BYTE_DETAIL_LEN)));
  168. if(len > MAX_BYTE_DETAIL_LEN) {
  169. buffer.append(" ...");
  170. }
  171. }
  172. private static String indent(Object obj) {
  173. return ((obj != null) ? obj.toString().replaceAll(
  174. System.lineSeparator(), ML_FIELD_SEP) : null);
  175. }
  176. public static Object ignoreNull(Object obj) {
  177. return ((obj != null) ? obj : IGNORE_ME);
  178. }
  179. }