You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CustomToStringStyle.java 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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.lang.SystemUtils;
  19. import org.apache.commons.lang.builder.StandardToStringStyle;
  20. import org.apache.commons.lang.builder.ToStringBuilder;
  21. /**
  22. * Custom ToStringStyle for use with ToStringBuilder.
  23. *
  24. * @author James Ahlborn
  25. */
  26. public class CustomToStringStyle extends StandardToStringStyle
  27. {
  28. private static final long serialVersionUID = 0L;
  29. private static final String ML_FIELD_SEP = SystemUtils.LINE_SEPARATOR + " ";
  30. private static final String IMPL_SUFFIX = "Impl";
  31. private static final int MAX_BYTE_DETAIL_LEN = 20;
  32. private static final Object IGNORE_ME = new Object();
  33. public static final CustomToStringStyle INSTANCE = new CustomToStringStyle() {
  34. private static final long serialVersionUID = 0L;
  35. {
  36. setContentStart("[");
  37. setFieldSeparator(ML_FIELD_SEP);
  38. setFieldSeparatorAtStart(true);
  39. setFieldNameValueSeparator(": ");
  40. setArraySeparator("," + ML_FIELD_SEP);
  41. setContentEnd(SystemUtils.LINE_SEPARATOR + "]");
  42. setUseShortClassName(true);
  43. }
  44. };
  45. public static final CustomToStringStyle VALUE_INSTANCE = new CustomToStringStyle() {
  46. private static final long serialVersionUID = 0L;
  47. {
  48. setUseShortClassName(true);
  49. setUseIdentityHashCode(false);
  50. }
  51. };
  52. private CustomToStringStyle() {
  53. }
  54. public static ToStringBuilder builder(Object obj) {
  55. return new ToStringBuilder(obj, INSTANCE);
  56. }
  57. public static ToStringBuilder valueBuilder(Object obj) {
  58. return new ToStringBuilder(obj, VALUE_INSTANCE);
  59. }
  60. @Override
  61. public void append(StringBuffer buffer, String fieldName, Object value,
  62. Boolean fullDetail) {
  63. if(value == IGNORE_ME) {
  64. return;
  65. }
  66. super.append(buffer, fieldName, value, fullDetail);
  67. }
  68. @Override
  69. protected void appendClassName(StringBuffer buffer, Object obj) {
  70. if(obj instanceof String) {
  71. // the caller gave an "explicit" class name
  72. buffer.append(obj);
  73. } else {
  74. super.appendClassName(buffer, obj);
  75. }
  76. }
  77. @Override
  78. protected String getShortClassName(Class clss) {
  79. String shortName = super.getShortClassName(clss);
  80. if(shortName.endsWith(IMPL_SUFFIX)) {
  81. shortName = shortName.substring(0,
  82. shortName.length() - IMPL_SUFFIX.length());
  83. }
  84. int idx = shortName.lastIndexOf('.');
  85. if(idx >= 0) {
  86. shortName = shortName.substring(idx + 1);
  87. }
  88. return shortName;
  89. }
  90. @Override
  91. protected void appendDetail(StringBuffer buffer, String fieldName,
  92. Object value) {
  93. if(value instanceof ByteBuffer) {
  94. appendDetail(buffer, (ByteBuffer)value);
  95. } else {
  96. buffer.append(indent(value));
  97. }
  98. }
  99. @Override
  100. protected void appendDetail(StringBuffer buffer, String fieldName,
  101. Collection value) {
  102. buffer.append("[");
  103. // gather contents of list in a new StringBuffer
  104. StringBuffer sb = new StringBuffer();
  105. Iterator<?> iter = value.iterator();
  106. if(iter.hasNext()) {
  107. if(isFieldSeparatorAtStart()) {
  108. appendFieldSeparator(sb);
  109. }
  110. appendValueDetail(sb, fieldName, iter.next());
  111. }
  112. while(iter.hasNext()) {
  113. sb.append(getArraySeparator());
  114. appendValueDetail(sb, fieldName, iter.next());
  115. }
  116. // indent entire list contents another level
  117. buffer.append(indent(sb));
  118. if(isFieldSeparatorAtStart()) {
  119. appendFieldSeparator(buffer);
  120. }
  121. buffer.append("]");
  122. }
  123. @Override
  124. protected void appendDetail(StringBuffer buffer, String fieldName,
  125. Map value) {
  126. buffer.append("{");
  127. // gather contents of map in a new StringBuffer
  128. StringBuffer sb = new StringBuffer();
  129. @SuppressWarnings("unchecked")
  130. Iterator<Map.Entry<?,?>> iter = value.entrySet().iterator();
  131. if(iter.hasNext()) {
  132. if(isFieldSeparatorAtStart()) {
  133. appendFieldSeparator(sb);
  134. }
  135. Map.Entry<?,?> e = iter.next();
  136. sb.append(e.getKey()).append("=");
  137. appendValueDetail(sb, fieldName, e.getValue());
  138. }
  139. while(iter.hasNext()) {
  140. sb.append(getArraySeparator());
  141. Map.Entry<?,?> e = iter.next();
  142. sb.append(e.getKey()).append("=");
  143. appendValueDetail(sb, fieldName, e.getValue());
  144. }
  145. // indent entire map contents another level
  146. buffer.append(indent(sb));
  147. if(isFieldSeparatorAtStart()) {
  148. appendFieldSeparator(buffer);
  149. }
  150. buffer.append("}");
  151. }
  152. @Override
  153. protected void appendDetail(StringBuffer buffer, String fieldName,
  154. byte[] array) {
  155. appendDetail(buffer, PageChannel.wrap(array));
  156. }
  157. private void appendValueDetail(StringBuffer buffer, String fieldName,
  158. Object value) {
  159. if (value == null) {
  160. appendNullText(buffer, fieldName);
  161. } else {
  162. appendInternal(buffer, fieldName, value, true);
  163. }
  164. }
  165. private static void appendDetail(StringBuffer buffer, ByteBuffer bb) {
  166. int len = bb.remaining();
  167. buffer.append("(").append(len).append(") ");
  168. buffer.append(ByteUtil.toHexString(bb, bb.position(),
  169. Math.min(len, MAX_BYTE_DETAIL_LEN)));
  170. if(len > MAX_BYTE_DETAIL_LEN) {
  171. buffer.append(" ...");
  172. }
  173. }
  174. private static String indent(Object obj) {
  175. return ((obj != null) ? obj.toString().replaceAll(
  176. SystemUtils.LINE_SEPARATOR, ML_FIELD_SEP) : null);
  177. }
  178. public static Object ignoreNull(Object obj) {
  179. return ((obj != null) ? obj : IGNORE_ME);
  180. }
  181. }