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 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. 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(SystemUtils.LINE_SEPARATOR + "]");
  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. protected void appendClassName(StringBuffer buffer, Object obj) {
  61. if(obj instanceof String) {
  62. // the caller gave an "explicit" class name
  63. buffer.append(obj);
  64. } else {
  65. super.appendClassName(buffer, obj);
  66. }
  67. }
  68. @Override
  69. protected String getShortClassName(Class clss) {
  70. String shortName = super.getShortClassName(clss);
  71. if(shortName.endsWith(IMPL_SUFFIX)) {
  72. shortName = shortName.substring(0,
  73. shortName.length() - IMPL_SUFFIX.length());
  74. }
  75. int idx = shortName.lastIndexOf('.');
  76. if(idx >= 0) {
  77. shortName = shortName.substring(idx + 1);
  78. }
  79. return shortName;
  80. }
  81. @Override
  82. protected void appendDetail(StringBuffer buffer, String fieldName,
  83. Object value) {
  84. if(value instanceof ByteBuffer) {
  85. appendDetail(buffer, (ByteBuffer)value);
  86. } else {
  87. buffer.append(indent(value));
  88. }
  89. }
  90. @Override
  91. protected void appendDetail(StringBuffer buffer, String fieldName,
  92. Collection value) {
  93. buffer.append("[");
  94. // gather contents of list in a new StringBuffer
  95. StringBuffer sb = new StringBuffer();
  96. Iterator<?> iter = value.iterator();
  97. if(iter.hasNext()) {
  98. if(isFieldSeparatorAtStart()) {
  99. appendFieldSeparator(sb);
  100. }
  101. appendValueDetail(sb, fieldName, iter.next());
  102. }
  103. while(iter.hasNext()) {
  104. sb.append(getArraySeparator());
  105. appendValueDetail(sb, fieldName, iter.next());
  106. }
  107. // indent entire list contents another level
  108. buffer.append(indent(sb));
  109. if(isFieldSeparatorAtStart()) {
  110. appendFieldSeparator(buffer);
  111. }
  112. buffer.append("]");
  113. }
  114. @Override
  115. protected void appendDetail(StringBuffer buffer, String fieldName,
  116. Map value) {
  117. buffer.append("{");
  118. // gather contents of map in a new StringBuffer
  119. StringBuffer sb = new StringBuffer();
  120. @SuppressWarnings("unchecked")
  121. Iterator<Map.Entry<?,?>> iter = value.entrySet().iterator();
  122. if(iter.hasNext()) {
  123. if(isFieldSeparatorAtStart()) {
  124. appendFieldSeparator(sb);
  125. }
  126. Map.Entry<?,?> e = iter.next();
  127. sb.append(e.getKey()).append("=");
  128. appendValueDetail(sb, fieldName, e.getValue());
  129. }
  130. while(iter.hasNext()) {
  131. sb.append(getArraySeparator());
  132. Map.Entry<?,?> e = iter.next();
  133. sb.append(e.getKey()).append("=");
  134. appendValueDetail(sb, fieldName, e.getValue());
  135. }
  136. // indent entire map contents another level
  137. buffer.append(indent(sb));
  138. if(isFieldSeparatorAtStart()) {
  139. appendFieldSeparator(buffer);
  140. }
  141. buffer.append("}");
  142. }
  143. @Override
  144. protected void appendDetail(StringBuffer buffer, String fieldName,
  145. byte[] array) {
  146. appendDetail(buffer, PageChannel.wrap(array));
  147. }
  148. private void appendValueDetail(StringBuffer buffer, String fieldName,
  149. Object value) {
  150. if (value == null) {
  151. appendNullText(buffer, fieldName);
  152. } else {
  153. appendInternal(buffer, fieldName, value, true);
  154. }
  155. }
  156. private static void appendDetail(StringBuffer buffer, ByteBuffer bb) {
  157. int len = bb.remaining();
  158. buffer.append("(").append(len).append(") ");
  159. buffer.append(ByteUtil.toHexString(bb, bb.position(),
  160. Math.min(len, MAX_BYTE_DETAIL_LEN)));
  161. if(len > MAX_BYTE_DETAIL_LEN) {
  162. buffer.append(" ...");
  163. }
  164. }
  165. private static String indent(Object obj) {
  166. return ((obj != null) ? obj.toString().replaceAll(
  167. SystemUtils.LINE_SEPARATOR, ML_FIELD_SEP) : null);
  168. }
  169. }