aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/com/healthmarketscience/jackcess/impl/CustomToStringStyle.java
diff options
context:
space:
mode:
authorJames Ahlborn <jtahlborn@yahoo.com>2013-09-09 03:06:40 +0000
committerJames Ahlborn <jtahlborn@yahoo.com>2013-09-09 03:06:40 +0000
commit613141a12aaca99045c6ac886ab1633190b9085d (patch)
tree5792dcaa52a92ac4ccbb2a2d36fb9867cd7d5a69 /src/main/java/com/healthmarketscience/jackcess/impl/CustomToStringStyle.java
parenta1940946b9c3538ac905ddf0fb60680e53ec98ee (diff)
downloadjackcess-613141a12aaca99045c6ac886ab1633190b9085d.tar.gz
jackcess-613141a12aaca99045c6ac886ab1633190b9085d.zip
revamp tostring output
git-svn-id: https://svn.code.sf.net/p/jackcess/code/jackcess/trunk@794 f203690c-595d-4dc9-a70b-905162fa7fd2
Diffstat (limited to 'src/main/java/com/healthmarketscience/jackcess/impl/CustomToStringStyle.java')
-rw-r--r--src/main/java/com/healthmarketscience/jackcess/impl/CustomToStringStyle.java194
1 files changed, 194 insertions, 0 deletions
diff --git a/src/main/java/com/healthmarketscience/jackcess/impl/CustomToStringStyle.java b/src/main/java/com/healthmarketscience/jackcess/impl/CustomToStringStyle.java
new file mode 100644
index 0000000..d909f96
--- /dev/null
+++ b/src/main/java/com/healthmarketscience/jackcess/impl/CustomToStringStyle.java
@@ -0,0 +1,194 @@
+/*
+Copyright (c) 2013 James Ahlborn
+
+This library is free software; you can redistribute it and/or
+modify it under the terms of the GNU Lesser General Public
+License as published by the Free Software Foundation; either
+version 2.1 of the License, or (at your option) any later version.
+
+This library is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+Lesser General Public License for more details.
+
+You should have received a copy of the GNU Lesser General Public
+License along with this library; if not, write to the Free Software
+Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+USA
+*/
+
+package com.healthmarketscience.jackcess.impl;
+
+import java.nio.ByteBuffer;
+import java.util.Collection;
+import java.util.Iterator;
+import java.util.Map;
+
+import org.apache.commons.lang.SystemUtils;
+import org.apache.commons.lang.builder.StandardToStringStyle;
+import org.apache.commons.lang.builder.ToStringBuilder;
+
+/**
+ * Custom ToStringStyle for use with ToStringBuilder.
+ *
+ * @author James Ahlborn
+ */
+public class CustomToStringStyle extends StandardToStringStyle
+{
+ private static final long serialVersionUID = 0L;
+
+ private static final String FIELD_SEP = SystemUtils.LINE_SEPARATOR + " ";
+ private static final String IMPL_SUFFIX = "Impl";
+ private static final int MAX_BYTE_DETAIL_LEN = 20;
+
+ public static final CustomToStringStyle INSTANCE = new CustomToStringStyle() {
+ private static final long serialVersionUID = 0L;
+ {
+ setContentStart("[");
+ setFieldSeparator(FIELD_SEP);
+ setFieldSeparatorAtStart(true);
+ setFieldNameValueSeparator(": ");
+ setContentEnd(SystemUtils.LINE_SEPARATOR + "]");
+ setUseShortClassName(true);
+ }
+ };
+
+ public static final CustomToStringStyle VALUE_INSTANCE = new CustomToStringStyle() {
+ private static final long serialVersionUID = 0L;
+ {
+ setUseShortClassName(true);
+ setUseIdentityHashCode(false);
+ }
+ };
+
+ private CustomToStringStyle() {
+ }
+
+ public static ToStringBuilder builder(Object obj) {
+ return new ToStringBuilder(obj, INSTANCE);
+ }
+
+ public static ToStringBuilder valueBuilder(Object obj) {
+ return new ToStringBuilder(obj, VALUE_INSTANCE);
+ }
+
+ @Override
+ protected void appendClassName(StringBuffer buffer, Object obj) {
+ if(obj instanceof String) {
+ // the caller gave an "explicit" class name
+ buffer.append(obj);
+ } else {
+ super.appendClassName(buffer, obj);
+ }
+ }
+
+ @Override
+ protected String getShortClassName(Class clss) {
+ String shortName = super.getShortClassName(clss);
+ if(shortName.endsWith(IMPL_SUFFIX)) {
+ shortName = shortName.substring(0,
+ shortName.length() - IMPL_SUFFIX.length());
+ }
+ int idx = shortName.lastIndexOf('.');
+ if(idx >= 0) {
+ shortName = shortName.substring(idx + 1);
+ }
+ return shortName;
+ }
+
+ @Override
+ protected void appendDetail(StringBuffer buffer, String fieldName,
+ Object value) {
+ if(value instanceof ByteBuffer) {
+ appendDetail(buffer, (ByteBuffer)value);
+ } else {
+ buffer.append(indent(value));
+ }
+ }
+
+ @Override
+ protected void appendDetail(StringBuffer buffer, String fieldName,
+ Collection value) {
+ buffer.append("[");
+
+ // gather contents of list in a new StringBuffer
+ StringBuffer sb = new StringBuffer();
+ Iterator<?> iter = value.iterator();
+ if(iter.hasNext()) {
+ if(isFieldSeparatorAtStart()) {
+ appendFieldSeparator(sb);
+ }
+ appendInternal(sb, fieldName, iter.next(), true);
+ }
+ while(iter.hasNext()) {
+ sb.append(",");
+ appendFieldSeparator(sb);
+ appendInternal(sb, fieldName, iter.next(), true);
+ }
+
+ // indent entire list contents another level
+ buffer.append(indent(sb));
+
+ if(isFieldSeparatorAtStart()) {
+ appendFieldSeparator(buffer);
+ }
+ buffer.append("]");
+ }
+
+
+ @Override
+ protected void appendDetail(StringBuffer buffer, String fieldName,
+ Map value) {
+ buffer.append("{");
+
+ // gather contents of map in a new StringBuffer
+ StringBuffer sb = new StringBuffer();
+ @SuppressWarnings("unchecked")
+ Iterator<Map.Entry<?,?>> iter = value.entrySet().iterator();
+ if(iter.hasNext()) {
+ if(isFieldSeparatorAtStart()) {
+ appendFieldSeparator(sb);
+ }
+ Map.Entry<?,?> e = iter.next();
+ sb.append(e.getKey()).append("=");
+ appendInternal(sb, fieldName, e.getValue(), true);
+ }
+ while(iter.hasNext()) {
+ sb.append(",");
+ appendFieldSeparator(sb);
+ Map.Entry<?,?> e = iter.next();
+ sb.append(e.getKey()).append("=");
+ appendInternal(sb, fieldName, e.getValue(), true);
+ }
+ if(isFieldSeparatorAtStart()) {
+ appendFieldSeparator(sb);
+ }
+
+ // indent entire map contents another level
+ buffer.append(indent(sb));
+
+ buffer.append("}");
+ }
+
+ @Override
+ protected void appendDetail(StringBuffer buffer, String fieldName,
+ byte[] array) {
+ appendDetail(buffer, PageChannel.wrap(array));
+ }
+
+ private static void appendDetail(StringBuffer buffer, ByteBuffer bb) {
+ int len = bb.remaining();
+ buffer.append("(").append(len).append(") ");
+ if(len > MAX_BYTE_DETAIL_LEN) {
+ buffer.append(ByteUtil.toHexString(bb, len, MAX_BYTE_DETAIL_LEN))
+ .append(" ...");
+ } else {
+ buffer.append(ByteUtil.toHexString(bb, len));
+ }
+ }
+
+ private static String indent(Object obj) {
+ return ((obj != null) ? obj.toString().replaceAll(
+ SystemUtils.LINE_SEPARATOR, FIELD_SEP) : null);
+ }
+}