From 613141a12aaca99045c6ac886ab1633190b9085d Mon Sep 17 00:00:00 2001 From: James Ahlborn Date: Mon, 9 Sep 2013 03:06:40 +0000 Subject: [PATCH] revamp tostring output git-svn-id: https://svn.code.sf.net/p/jackcess/code/jackcess/trunk@794 f203690c-595d-4dc9-a70b-905162fa7fd2 --- .../jackcess/impl/ByteUtil.java | 1 + .../jackcess/impl/ColumnImpl.java | 40 ++-- .../jackcess/impl/CustomToStringStyle.java | 194 ++++++++++++++++++ .../jackcess/impl/IndexData.java | 86 +++++--- .../jackcess/impl/IndexImpl.java | 32 ++- .../jackcess/impl/IndexPageCache.java | 32 +-- .../jackcess/impl/PropertyMaps.java | 11 +- .../jackcess/impl/RelationshipImpl.java | 17 +- .../jackcess/impl/TableImpl.java | 39 ++-- .../jackcess/impl/UsageMap.java | 26 +-- .../impl/complex/ComplexColumnInfoImpl.java | 9 +- .../jackcess/util/MemFileChannel.java | 2 +- 12 files changed, 353 insertions(+), 136 deletions(-) create mode 100644 src/main/java/com/healthmarketscience/jackcess/impl/CustomToStringStyle.java diff --git a/src/main/java/com/healthmarketscience/jackcess/impl/ByteUtil.java b/src/main/java/com/healthmarketscience/jackcess/impl/ByteUtil.java index 857c631..f851788 100644 --- a/src/main/java/com/healthmarketscience/jackcess/impl/ByteUtil.java +++ b/src/main/java/com/healthmarketscience/jackcess/impl/ByteUtil.java @@ -451,6 +451,7 @@ public final class ByteUtil { StringBuilder rtn = new StringBuilder(); int position = buffer.position(); buffer.position(offset); + size = Math.min(size, buffer.remaining()); for (int i = 0; i < size; i++) { byte b = buffer.get(); diff --git a/src/main/java/com/healthmarketscience/jackcess/impl/ColumnImpl.java b/src/main/java/com/healthmarketscience/jackcess/impl/ColumnImpl.java index 13eb370..1a71d95 100644 --- a/src/main/java/com/healthmarketscience/jackcess/impl/ColumnImpl.java +++ b/src/main/java/com/healthmarketscience/jackcess/impl/ColumnImpl.java @@ -65,6 +65,7 @@ import com.healthmarketscience.jackcess.impl.scsu.Compress; import com.healthmarketscience.jackcess.impl.scsu.EndOfInputException; import com.healthmarketscience.jackcess.impl.scsu.Expand; import com.healthmarketscience.jackcess.impl.scsu.IllegalInputException; +import org.apache.commons.lang.builder.ToStringBuilder; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -1524,38 +1525,37 @@ public class ColumnImpl implements Column, Comparable { @Override public String toString() { - StringBuilder rtn = new StringBuilder(); - rtn.append("\tName: (" + _table.getName() + ") " + _name); + ToStringBuilder sb = CustomToStringStyle.builder(this) + .append("name", "(" + _table.getName() + ") " + _name); byte typeValue = _type.getValue(); if(_type.isUnsupported()) { typeValue = getUnknownDataType(); } - rtn.append("\n\tType: 0x" + Integer.toHexString(typeValue) + - " (" + _type + ")"); - rtn.append("\n\tNumber: " + _columnNumber); - rtn.append("\n\tLength: " + _columnLength); - rtn.append("\n\tVariable length: " + _variableLength); + sb.append("type", "0x" + Integer.toHexString(typeValue) + + " (" + _type + ")") + .append("number", _columnNumber) + .append("length", _columnLength) + .append("variableLength", _variableLength); if(_type.isTextual()) { - rtn.append("\n\tCompressed Unicode: " + _textInfo._compressedUnicode); - rtn.append("\n\tText Sort order: " + _textInfo._sortOrder); + sb.append("compressedUnicode", _textInfo._compressedUnicode) + .append("textSortOrder", _textInfo._sortOrder); if(_textInfo._codePage > 0) { - rtn.append("\n\tText Code Page: " + _textInfo._codePage); + sb.append("textCodePage", _textInfo._codePage); } if(isAppendOnly()) { - rtn.append("\n\tAppend only: " + isAppendOnly()); + sb.append("appendOnly", isAppendOnly()); } if(isHyperlink()) { - rtn.append("\n\tHyperlink: " + isHyperlink()); + sb.append("hyperlink", isHyperlink()); } } if(_autoNumber) { - rtn.append("\n\tLast AutoNumber: " + _autoNumberGenerator.getLast()); + sb.append("lastAutoNumber", _autoNumberGenerator.getLast()); } if(_complexInfo != null) { - rtn.append("\n\tComplexInfo: " + _complexInfo); + sb.append("complexInfo", _complexInfo); } - rtn.append("\n\n"); - return rtn.toString(); + return sb.toString(); } /** @@ -1943,7 +1943,9 @@ public class ColumnImpl implements Column, Comparable { @Override public String toString() { - return "RawData: " + ByteUtil.toHexString(getBytes()); + return CustomToStringStyle.valueBuilder(this) + .append(null, getBytes()) + .toString(); } private Object writeReplace() throws ObjectStreamException { @@ -2116,7 +2118,9 @@ public class ColumnImpl implements Column, Comparable { @Override public String toString() { - return _value + "(" + _version + ")"; + return CustomToStringStyle.valueBuilder(this) + .append(null, _value + "(" + _version + ")") + .toString(); } } 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> 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); + } +} diff --git a/src/main/java/com/healthmarketscience/jackcess/impl/IndexData.java b/src/main/java/com/healthmarketscience/jackcess/impl/IndexData.java index a1e945b..0479395 100644 --- a/src/main/java/com/healthmarketscience/jackcess/impl/IndexData.java +++ b/src/main/java/com/healthmarketscience/jackcess/impl/IndexData.java @@ -42,6 +42,7 @@ import com.healthmarketscience.jackcess.Index; import com.healthmarketscience.jackcess.IndexBuilder; import static com.healthmarketscience.jackcess.impl.ByteUtil.ByteStream; import static com.healthmarketscience.jackcess.impl.IndexCodes.*; +import org.apache.commons.lang.builder.ToStringBuilder; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -850,23 +851,23 @@ public class IndexData { @Override public String toString() { - StringBuilder rtn = new StringBuilder(); - rtn.append("\n\tData number: ").append(_number); - rtn.append("\n\tPage number: ").append(_rootPageNumber); - rtn.append("\n\tIs Backing Primary Key: ").append(isBackingPrimaryKey()); - rtn.append("\n\tIs Unique: ").append(isUnique()); - rtn.append("\n\tIgnore Nulls: ").append(shouldIgnoreNulls()); - rtn.append("\n\tColumns: ").append(_columns); - rtn.append("\n\tInitialized: ").append(_initialized); + ToStringBuilder sb = CustomToStringStyle.builder(this) + .append("dataNumber", _number) + .append("pageNumber", _rootPageNumber) + .append("isBackingPrimaryKey", isBackingPrimaryKey()) + .append("isUnique", isUnique()) + .append("ignoreNulls", shouldIgnoreNulls()) + .append("columns", _columns) + .append("initialized", _initialized); if(_initialized) { try { - rtn.append("\n\tEntryCount: ").append(getEntryCount()); + sb.append("entryCount", getEntryCount()); } catch(IOException e) { throw new RuntimeException(e); } } - rtn.append("\n").append(_pageCache.toString()); - return rtn.toString(); + sb.append("pageCache", _pageCache); + return sb.toString(); } /** @@ -1328,7 +1329,10 @@ public class IndexData { @Override public String toString() { - return "ColumnDescriptor " + getColumn() + "\nflags: " + getFlags(); + return CustomToStringStyle.builder(this) + .append("column", getColumn()) + .append("flags", getFlags()) + .toString(); } } @@ -1770,15 +1774,20 @@ public class IndexData { buffer.put((byte)getRowId().getRowNumber()); } - protected final String entryBytesToString() { - return (isValid() ? ", Bytes = " + ByteUtil.toHexString( - ByteBuffer.wrap(_entryBytes), _entryBytes.length) : - ""); + protected final ToStringBuilder entryBytesToStringBuilder( + ToStringBuilder sb) { + if(isValid()) { + sb.append("bytes", _entryBytes); + } + return sb; } @Override public String toString() { - return "RowId = " + _rowId + entryBytesToString() + "\n"; + return entryBytesToStringBuilder( + CustomToStringStyle.valueBuilder(this) + .append("rowId", _rowId)) + .toString(); } @Override @@ -1904,10 +1913,12 @@ public class IndexData { @Override public String toString() { - return ("Node RowId = " + getRowId() + - ", SubPage = " + _subPageNumber + entryBytesToString() + "\n"); - } - + return entryBytesToStringBuilder( + CustomToStringStyle.valueBuilder(this) + .append("rowId", getRowId()) + .append("subPage", _subPageNumber)) + .toString(); + } } /** @@ -2140,8 +2151,10 @@ public class IndexData { @Override public String toString() { - return getClass().getSimpleName() + " CurPosition " + _curPos + - ", PrevPosition " + _prevPos; + return CustomToStringStyle.valueBuilder(this) + .append("curPosition", _curPos) + .append("prevPosition", _prevPos) + .toString(); } /** @@ -2300,8 +2313,12 @@ public class IndexData { @Override public String toString() { - return "Page = " + _dataPage.getPageNumber() + ", Idx = " + _idx + - ", Entry = " + _entry + ", Between = " + _between; + return CustomToStringStyle.valueBuilder(this) + .append("page", _dataPage.getPageNumber()) + .append("idx", _idx) + .append("entry", _entry) + .append("between", _between) + .toString(); } } @@ -2366,13 +2383,20 @@ public class IndexData { @Override public final String toString() { List entries = getEntries(); - return (isLeaf() ? "Leaf" : "Node") + "DataPage[" + getPageNumber() + + + String objName = + (isLeaf() ? "Leaf" : "Node") + "DataPage[" + getPageNumber() + "] " + getPrevPageNumber() + ", " + getNextPageNumber() + ", (" + - getChildTailPageNumber() + "), " + - ((isLeaf() && !entries.isEmpty()) ? - ("[" + entries.get(0) + ", " + - entries.get(entries.size() - 1) + "]") : - entries); + getChildTailPageNumber() + ")"; + ToStringBuilder sb = CustomToStringStyle.valueBuilder(objName); + + if((isLeaf() && !entries.isEmpty())) { + sb.append("entryRange", "[" + entries.get(0) + ", " + + entries.get(entries.size() - 1) + "]"); + } else { + sb.append("entries", entries); + } + return sb.toString(); } } diff --git a/src/main/java/com/healthmarketscience/jackcess/impl/IndexImpl.java b/src/main/java/com/healthmarketscience/jackcess/impl/IndexImpl.java index db76294..083fd5a 100644 --- a/src/main/java/com/healthmarketscience/jackcess/impl/IndexImpl.java +++ b/src/main/java/com/healthmarketscience/jackcess/impl/IndexImpl.java @@ -36,7 +36,7 @@ import java.util.Map; import com.healthmarketscience.jackcess.CursorBuilder; import com.healthmarketscience.jackcess.Index; import com.healthmarketscience.jackcess.IndexBuilder; -import com.healthmarketscience.jackcess.RowId; +import org.apache.commons.lang.builder.ToStringBuilder; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -348,18 +348,16 @@ public class IndexImpl implements Index, Comparable @Override public String toString() { - StringBuilder rtn = new StringBuilder(); - rtn.append("\tName: (").append(getTable().getName()).append(") ") - .append(_name); - rtn.append("\n\tNumber: ").append(_indexNumber); - rtn.append("\n\tIs Primary Key: ").append(isPrimaryKey()); - rtn.append("\n\tIs Foreign Key: ").append(isForeignKey()); + ToStringBuilder sb = CustomToStringStyle.builder(this) + .append("name", "(" + getTable().getName() + ") " + _name) + .append("number", _indexNumber) + .append("isPrimaryKey", isPrimaryKey()) + .append("isForeignKey", isForeignKey()); if(_reference != null) { - rtn.append("\n\tForeignKeyReference: ").append(_reference); + sb.append("foreignKeyReference", _reference); } - rtn.append(_data.toString()); - rtn.append("\n\n"); - return rtn.toString(); + sb.append("data", _data); + return sb.toString(); } public int compareTo(IndexImpl other) { @@ -451,12 +449,12 @@ public class IndexImpl implements Index, Comparable @Override public String toString() { - return new StringBuilder() - .append("\n\t\tOther Index Number: ").append(_otherIndexNumber) - .append("\n\t\tOther Table Page Num: ").append(_otherTablePageNumber) - .append("\n\t\tIs Primary Table: ").append(isPrimaryTable()) - .append("\n\t\tIs Cascade Updates: ").append(isCascadeUpdates()) - .append("\n\t\tIs Cascade Deletes: ").append(isCascadeDeletes()) + return CustomToStringStyle.builder(this) + .append("otherIndexNumber", _otherIndexNumber) + .append("otherTablePageNum", _otherTablePageNumber) + .append("isPrimaryTable", isPrimaryTable()) + .append("isCascadeUpdates", isCascadeUpdates()) + .append("isCascadeDeletes", isCascadeDeletes()) .toString(); } } diff --git a/src/main/java/com/healthmarketscience/jackcess/impl/IndexPageCache.java b/src/main/java/com/healthmarketscience/jackcess/impl/IndexPageCache.java index 325e178..f6b4ade 100644 --- a/src/main/java/com/healthmarketscience/jackcess/impl/IndexPageCache.java +++ b/src/main/java/com/healthmarketscience/jackcess/impl/IndexPageCache.java @@ -40,6 +40,7 @@ import java.util.Map; import java.util.RandomAccess; import static com.healthmarketscience.jackcess.impl.IndexData.*; +import org.apache.commons.lang.builder.ToStringBuilder; /** * Manager of the index pages for a IndexData. @@ -1108,24 +1109,25 @@ public class IndexPageCache } /** - * Dumps the given index page to a StringBuilder + * Collects all the cache pages in the cache. * - * @param rtn the StringBuilder to update - * @param dpMain the index page to dump + * @param paages the List to update + * @param dpMain the index page to collect */ - private void dumpPage(StringBuilder rtn, DataPageMain dpMain) { + private List collectPages(List pages, DataPageMain dpMain) { try { CacheDataPage cacheDataPage = new CacheDataPage(dpMain); - rtn.append(cacheDataPage).append("\n"); + pages.add(cacheDataPage); if(!dpMain._leaf) { for(Entry e : cacheDataPage._extra._entryView) { DataPageMain childMain = dpMain.getChildPage(e); - dumpPage(rtn, childMain); + collectPages(pages, childMain); } } } catch(IOException e) { - rtn.append("Page[" + dpMain._pageNumber + "]: " + e); + pages.add("DataPage[" + dpMain._pageNumber + "]: " + e); } + return pages; } /** @@ -1148,13 +1150,13 @@ public class IndexPageCache @Override public String toString() { + ToStringBuilder sb = CustomToStringStyle.builder(this); if(_rootPage == null) { - return "Cache: (uninitialized)"; + sb.append("pages", "(uninitialized)"); + } else { + sb.append("pages", collectPages(new ArrayList(), _rootPage)); } - - StringBuilder rtn = new StringBuilder("Cache: \n"); - dumpPage(rtn, _rootPage); - return rtn.toString(); + return sb.toString(); } /** @@ -1320,8 +1322,10 @@ public class IndexPageCache @Override public String toString() { - return "DPExtra: " + _entryView; - } + return CustomToStringStyle.builder("DPExtra") + .append(null, _entryView) + .toString(); + } } /** diff --git a/src/main/java/com/healthmarketscience/jackcess/impl/PropertyMaps.java b/src/main/java/com/healthmarketscience/jackcess/impl/PropertyMaps.java index adc556b..b0d5567 100644 --- a/src/main/java/com/healthmarketscience/jackcess/impl/PropertyMaps.java +++ b/src/main/java/com/healthmarketscience/jackcess/impl/PropertyMaps.java @@ -111,14 +111,9 @@ public class PropertyMaps implements Iterable @Override public String toString() { - StringBuilder sb = new StringBuilder(); - for(Iterator iter = iterator(); iter.hasNext(); ) { - sb.append(iter.next()); - if(iter.hasNext()) { - sb.append("\n"); - } - } - return sb.toString(); + return CustomToStringStyle.builder(this) + .append(null, _maps.values()) + .toString(); } /** diff --git a/src/main/java/com/healthmarketscience/jackcess/impl/RelationshipImpl.java b/src/main/java/com/healthmarketscience/jackcess/impl/RelationshipImpl.java index 8424610..cdf9700 100644 --- a/src/main/java/com/healthmarketscience/jackcess/impl/RelationshipImpl.java +++ b/src/main/java/com/healthmarketscience/jackcess/impl/RelationshipImpl.java @@ -138,15 +138,14 @@ public class RelationshipImpl implements Relationship @Override public String toString() { - StringBuilder rtn = new StringBuilder(); - rtn.append("\tName: " + _name); - rtn.append("\n\tFromTable: " + _fromTable.getName()); - rtn.append("\n\tFromColumns: " + _fromColumns); - rtn.append("\n\tToTable: " + _toTable.getName()); - rtn.append("\n\tToColumns: " + _toColumns); - rtn.append("\n\tFlags: " + Integer.toHexString(_flags)); - rtn.append("\n\n"); - return rtn.toString(); + return CustomToStringStyle.builder(this) + .append("name", _name) + .append("fromTable", _fromTable.getName()) + .append("fromColumns", _fromColumns) + .append("toTable", _toTable.getName()) + .append("toColumns", _toColumns) + .append("flags", Integer.toHexString(_flags)) + .toString(); } } diff --git a/src/main/java/com/healthmarketscience/jackcess/impl/TableImpl.java b/src/main/java/com/healthmarketscience/jackcess/impl/TableImpl.java index 125508e..4cd3365 100644 --- a/src/main/java/com/healthmarketscience/jackcess/impl/TableImpl.java +++ b/src/main/java/com/healthmarketscience/jackcess/impl/TableImpl.java @@ -2085,24 +2085,18 @@ public class TableImpl implements Table @Override public String toString() { - StringBuilder rtn = new StringBuilder(); - rtn.append("Type: " + _tableType + - ((_tableType == TYPE_USER) ? " (USER)" : " (SYSTEM)")); - rtn.append("\nName: " + _name); - rtn.append("\nRow count: " + _rowCount); - rtn.append("\nColumn count: " + _columns.size()); - rtn.append("\nIndex (data) count: " + _indexCount); - rtn.append("\nLogical Index count: " + _logicalIndexCount); - rtn.append("\nColumns:\n"); - for(ColumnImpl col : _columns) { - rtn.append(col); - } - rtn.append("\nIndexes:\n"); - for(Index index : _indexes) { - rtn.append(index); - } - rtn.append("\nOwned pages: " + _ownedPages + "\n"); - return rtn.toString(); + return CustomToStringStyle.builder(this) + .append("type", (_tableType + + ((_tableType == TYPE_USER) ? " (USER)" : " (SYSTEM)"))) + .append("name", _name) + .append("rowCount", _rowCount) + .append("columnCount", _columns.size()) + .append("indexCount(data)", _indexCount) + .append("logicalIndexCount", _logicalIndexCount) + .append("columns", _columns) + .append("indexes", _indexes) + .append("ownedPages", _ownedPages) + .toString(); } /** @@ -2553,10 +2547,11 @@ public class TableImpl implements Table } @Override - public String toString() - { - return "RowState: headerRowId = " + _headerRowId + ", finalRowId = " + - _finalRowId; + public String toString() { + return CustomToStringStyle.valueBuilder(this) + .append("headerRowId", _headerRowId) + .append("finalRowId", _finalRowId) + .toString(); } } diff --git a/src/main/java/com/healthmarketscience/jackcess/impl/UsageMap.java b/src/main/java/com/healthmarketscience/jackcess/impl/UsageMap.java index 6a80e04..9169582 100644 --- a/src/main/java/com/healthmarketscience/jackcess/impl/UsageMap.java +++ b/src/main/java/com/healthmarketscience/jackcess/impl/UsageMap.java @@ -29,11 +29,12 @@ package com.healthmarketscience.jackcess.impl; import java.io.IOException; import java.nio.ByteBuffer; +import java.util.ArrayList; import java.util.BitSet; +import java.util.List; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import com.healthmarketscience.jackcess.RowId; /** * Describes which database pages a particular table uses @@ -413,10 +414,8 @@ public class UsageMap @Override public String toString() { - StringBuilder builder = new StringBuilder( - "(" + _handler.getClass().getSimpleName() + - ") page numbers (range " + _startPage + " " + _endPage + "): ["); + List ranges = new ArrayList(); PageCursor pCursor = cursor(); int curRangeStart = Integer.MIN_VALUE; int prevPage = Integer.MIN_VALUE; @@ -428,28 +427,31 @@ public class UsageMap if(nextPage != (prevPage + 1)) { if(prevPage >= 0) { - rangeToString(builder, curRangeStart, prevPage); + rangeToString(ranges, curRangeStart, prevPage); } curRangeStart = nextPage; } prevPage = nextPage; } if(prevPage >= 0) { - rangeToString(builder, curRangeStart, prevPage); + rangeToString(ranges, curRangeStart, prevPage); } - builder.append("]"); - return builder.toString(); + return CustomToStringStyle.valueBuilder( + _handler.getClass().getSimpleName()) + .append("range", "(" + _startPage + "-" + _endPage + ")") + .append("pageNumbers", ranges) + .toString(); } - private static void rangeToString(StringBuilder builder, int rangeStart, + private static void rangeToString(List ranges, int rangeStart, int rangeEnd) { - builder.append(rangeStart); if(rangeEnd > rangeStart) { - builder.append("-").append(rangeEnd); + ranges.add(rangeStart + "-" + rangeEnd); + } else { + ranges.add(String.valueOf(rangeStart)); } - builder.append(", "); } private abstract class Handler diff --git a/src/main/java/com/healthmarketscience/jackcess/impl/complex/ComplexColumnInfoImpl.java b/src/main/java/com/healthmarketscience/jackcess/impl/complex/ComplexColumnInfoImpl.java index 83e86a2..ecfc485 100644 --- a/src/main/java/com/healthmarketscience/jackcess/impl/complex/ComplexColumnInfoImpl.java +++ b/src/main/java/com/healthmarketscience/jackcess/impl/complex/ComplexColumnInfoImpl.java @@ -40,6 +40,7 @@ import com.healthmarketscience.jackcess.complex.ComplexDataType; import com.healthmarketscience.jackcess.complex.ComplexValue; import com.healthmarketscience.jackcess.complex.ComplexValueForeignKey; import com.healthmarketscience.jackcess.impl.ColumnImpl; +import com.healthmarketscience.jackcess.impl.CustomToStringStyle; import com.healthmarketscience.jackcess.impl.TableImpl; /** @@ -302,10 +303,10 @@ public abstract class ComplexColumnInfoImpl @Override public String toString() { - StringBuilder rtn = new StringBuilder(); - rtn.append("\n\t\tComplexType: " + getType()); - rtn.append("\n\t\tComplexTypeId: " + _complexTypeId); - return rtn.toString(); + return CustomToStringStyle.valueBuilder(this) + .append("complexType", getType()) + .append("complexTypeId", _complexTypeId) + .toString(); } protected static void diffFlatColumns(Table typeObjTable, diff --git a/src/main/java/com/healthmarketscience/jackcess/util/MemFileChannel.java b/src/main/java/com/healthmarketscience/jackcess/util/MemFileChannel.java index 3a583e5..1c65766 100644 --- a/src/main/java/com/healthmarketscience/jackcess/util/MemFileChannel.java +++ b/src/main/java/com/healthmarketscience/jackcess/util/MemFileChannel.java @@ -286,7 +286,7 @@ public class MemFileChannel extends FileChannel int chunkIndex = getChunkIndex(position); int chunkOffset = getChunkOffset(position); - long numBytes = 0; + long numBytes = 0L; while(count > 0L) { int chunkBytes = (int)Math.min(count, CHUNK_SIZE - chunkOffset); -- 2.39.5