Skip to content

Commit

Permalink
Review toString methods
Browse files Browse the repository at this point in the history
- replace all usage of reflectionToString
  • Loading branch information
spannm committed Feb 15, 2024
1 parent d064f6a commit 67863cf
Show file tree
Hide file tree
Showing 20 changed files with 104 additions and 160 deletions.
11 changes: 4 additions & 7 deletions src/main/java/io/github/spannm/jackcess/expr/Identifier.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package io.github.spannm.jackcess.expr;

import java.util.Objects;
import java.util.Optional;

/**
* identifies a database entity (e.g. the name of a database field). An Identify must have an object name, but the
Expand Down Expand Up @@ -72,13 +73,9 @@ public boolean equals(Object o) {
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
if (_collectionName != null) {
sb.append("[").append(_collectionName).append("].");
}
sb.append("[").append(_objectName).append("]");
if (_propertyName != null) {
sb.append(".[").append(_propertyName).append("]");
}
Optional.ofNullable(_collectionName).ifPresent(x -> sb.append('[').append(x).append("]."));
sb.append('[').append(_objectName).append(']');
Optional.ofNullable(_propertyName).ifPresent(x -> sb.append('[').append(x).append("]."));
return sb.toString();
}

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/io/github/spannm/jackcess/impl/ByteUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -473,10 +473,10 @@ public static String toHexString(ByteBuffer buffer,

} else {

rtn.append(" ");
rtn.append(' ');

if (next % NUM_BYTES_PER_BLOCK == 0) {
rtn.append(" ");
rtn.append(' ');
}
}
}
Expand Down
12 changes: 6 additions & 6 deletions src/main/java/io/github/spannm/jackcess/impl/ColumnImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -1221,22 +1221,22 @@ private static String readGUIDValue(ByteBuffer buffer, ByteOrder order) {
}

StringBuilder sb = new StringBuilder(22);
sb.append("{");
sb.append('{');
sb.append(ByteUtil.toHexString(buffer, 0, 4,
false));
sb.append("-");
sb.append('-');
sb.append(ByteUtil.toHexString(buffer, 4, 2,
false));
sb.append("-");
sb.append('-');
sb.append(ByteUtil.toHexString(buffer, 6, 2,
false));
sb.append("-");
sb.append('-');
sb.append(ByteUtil.toHexString(buffer, 8, 2,
false));
sb.append("-");
sb.append('-');
sb.append(ByteUtil.toHexString(buffer, 10, 6,
false));
sb.append("}");
sb.append('}');
return sb.toString();
}

Expand Down
6 changes: 4 additions & 2 deletions src/main/java/io/github/spannm/jackcess/impl/CursorImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -721,7 +721,8 @@ protected boolean isCurrentRowValid() throws IOException {

@Override
public String toString() {
return getClass().getSimpleName() + " CurPosition " + _curPos + ", PrevPosition " + _prevPos;
return String.format("%s[id=%s, table=%s, prevPos=%s, curPos=%s]",
getClass().getSimpleName(), _id, _table, _prevPos, _curPos);
}

/**
Expand Down Expand Up @@ -983,7 +984,8 @@ private PositionImpl getPreviousPosition() {

@Override
public String toString() {
return getClass().getSimpleName() + " " + _cursorId + " CurPosition " + _curPos + ", PrevPosition " + _prevPos;
return String.format("%s[cursorId=%s, curPos=%s, prevPos=%s]",
getClass().getSimpleName(), _cursorId, _curPos, _prevPos);
}
}

Expand Down
13 changes: 12 additions & 1 deletion src/main/java/io/github/spannm/jackcess/impl/DatabaseImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -1864,7 +1864,18 @@ private static void validateName(String name, int maxLength, String nameType) {

@Override
public String toString() {
return StringUtil.reflectionToString(this);
return String.format(
"%s[file=%s, name=%s, readOnly=%s, tableParentId=%s, format=%s, tableNames=%s, pageChannel=%s, "
+ "systemCatalog=%s, tableFinder=%s, accessControlEntries=%s, relParentId=%s, relationships=%s, queries=%s, "
+ "complexCols=%s, newTableSIDs=%s, dbErrorHandler=%s, fileFormat=%s, charset=%s, timeZone=%s, zoneId=%s, defaultSortOrder=%s, "
+ "defaultCodePage=%s, columnOrder=%s, enforceForeignKeys=%s, allowAutoNumInsert=%s, evaluateExpressions=%s, validatorFactory=%s, "
+ "tableCache=%s, propsHandler=%s, dbParentId=%s, newObjOwner=%s, dbPropMaps=%s, summaryPropMaps=%s, userDefPropMaps=%s, "
+ "linkResolver=%s, linkedDbs=%s, fkEnforcerSharedState=%s, evalCtx=%s, dtf=%s]",
getClass().getSimpleName(),
_file, _name, _readOnly, _tableParentId, _format, _tableNames, _pageChannel, _systemCatalog, _tableFinder, _accessControlEntries, _relParentId,
_relationships, _queries, _complexCols, _newTableSIDs, _dbErrorHandler, _fileFormat, _charset, _timeZone, _zoneId, _defaultSortOrder, _defaultCodePage, _columnOrder, _enforceForeignKeys,
_allowAutoNumInsert, _evaluateExpressions, _validatorFactory, _tableCache, _propsHandler, _dbParentId, Arrays.toString(_newObjOwner), _dbPropMaps, _summaryPropMaps, _userDefPropMaps,
_linkResolver, _linkedDbs, _fkEnforcerSharedState, _evalCtx, _dtf);
}

/**
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/io/github/spannm/jackcess/impl/IndexData.java
Original file line number Diff line number Diff line change
Expand Up @@ -1456,7 +1456,10 @@ protected final void writeValue(Object value, ByteStream bout) throws IOExceptio

@Override
public String toString() {
return ToStringBuilder.builder(this).append("column", getColumn()).append("flags", getFlags() + " " + (isAscending() ? "(ASC)" : "(DSC)")).toString();
return ToStringBuilder.builder(this)
.append("column", getColumn())
.append("flags", getFlags() + " " + (isAscending() ? "(ASC)" : "(DSC)"))
.toString();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,18 +59,18 @@ public final Object validate(Column col, Object val) throws IOException {

@Override
public String toString() {
StringBuilder sb = new StringBuilder().append("{");
StringBuilder sb = new StringBuilder('{');
if (_delegate instanceof InternalColumnValidator) {
((InternalColumnValidator) _delegate).appendToString(sb);
} else if (_delegate != SimpleColumnValidator.INSTANCE) {
sb.append("custom=").append(_delegate);
}
if (sb.length() > 1) {
sb.append(";");
sb.append(';');
}
appendToString(sb);
sb.append("}");
return sb.toString();
return sb.append('}')
.toString();
}

protected abstract void appendToString(StringBuilder sb);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,17 +169,16 @@ public String toString() {
}

public static String toString(PropertyMap map) {
StringBuilder sb = new StringBuilder();
sb.append(PropertyMaps.DEFAULT_NAME.equals(map.getName()) ? "<DEFAULT>" : map.getName())
StringBuilder sb = new StringBuilder(PropertyMaps.DEFAULT_NAME.equals(map.getName()) ? "<DEFAULT>" : map.getName())
.append(" {");
for (Iterator<Property> iter = map.iterator(); iter.hasNext();) {
sb.append(iter.next());
if (iter.hasNext()) {
sb.append(",");
sb.append(',');
}
}
sb.append("}");
return sb.toString();
return sb.append('}')
.toString();
}

public static Property createProperty(String name, DataType type, Object value) {
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/io/github/spannm/jackcess/impl/UsageMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -998,7 +998,8 @@ private int updatePosition(int pageNumber) {

@Override
public String toString() {
return getClass().getSimpleName() + " CurPosition " + _curPageNumber + ", PrevPosition " + _prevPageNumber;
return String.format("%s[curPageNumber=%s, prevPageNumber=%s]",
getClass().getSimpleName(), _curPageNumber, _prevPageNumber);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -348,16 +348,15 @@ public void delete() throws IOException {

@Override
public String toString() {

String dataStr = null;
try {
dataStr = ByteUtil.toHexString(getFileData());
} catch (IOException e) {
dataStr = e.toString();
}

return "Attachment(" + getComplexValueForeignKey() + "," + getId()
+ ") " + getFileUrl() + ", " + getFileName() + ", " + getFileType()
return "Attachment(" + getComplexValueForeignKey() + "," + getId() + ") "
+ getFileUrl() + ", " + getFileName() + ", " + getFileType()
+ ", " + getFileTimeStampObject() + ", " + getFileFlags() + ", " + dataStr;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1044,7 +1044,7 @@ private static StringBuilder appendLeadingExpr(
expr.toString(ctx, sb, isDebug);
if (sb.length() > len) {
// only add space if the leading expr added some text
sb.append(" ");
sb.append(' ');
}
return sb;
}
Expand Down Expand Up @@ -1159,22 +1159,23 @@ public String toString() {
Map.Entry<Integer, List<Token>> e = getTopPos();

// TODO actually format expression?
StringBuilder sb = new StringBuilder()
.append("[token ").append(e.getKey()).append("] (");
StringBuilder sb = new StringBuilder("[token ")
.append(e.getKey()).append("] (");

for (Iterator<Token> iter = e.getValue().iterator(); iter.hasNext();) {
Token t = iter.next();
sb.append("'").append(t.getValueStr()).append("'");
sb.append('\'').append(t.getValueStr()).append('\'');
if (iter.hasNext()) {
sb.append(",");
sb.append(',');
}
}

sb.append(")");
sb.append(')');

if (_pendingExpr != null) {
sb.append(" [pending '").append(_pendingExpr.toDebugString(_ctx))
.append("']");
sb.append(" [pending '")
.append(_pendingExpr.toDebugString(_ctx))
.append("']");
}

return sb.toString();
Expand Down Expand Up @@ -1413,11 +1414,11 @@ protected boolean isValidationExpr() {
protected StringBuilder toString(
LocaleContext ctx, StringBuilder sb, boolean isDebug) {
if (isDebug) {
sb.append("<").append(getClass().getSimpleName()).append(">{");
sb.append('<').append(getClass().getSimpleName()).append(">{");
}
toExprString(ctx, sb, isDebug);
if (isDebug) {
sb.append("}");
sb.append('}');
}
return sb;
}
Expand Down Expand Up @@ -1571,7 +1572,7 @@ protected void toExprString(
if (_val.getType() == Value.Type.STRING) {
literalStrToString((String) _val.get(), sb);
} else if (_val.getType().isTemporal()) {
sb.append("#").append(_val.getAsString(ctx)).append("#");
sb.append('#').append(_val.getAsString(ctx)).append('#');
} else {
sb.append(_val.get());
}
Expand Down Expand Up @@ -1637,9 +1638,9 @@ public void collectIdentifiers(Collection<Identifier> identifiers) {
@Override
protected void toExprString(
LocaleContext ctx, StringBuilder sb, boolean isDebug) {
sb.append("(");
sb.append('(');
_expr.toString(ctx, sb, isDebug);
sb.append(")");
sb.append(')');
}
}

Expand Down Expand Up @@ -1672,13 +1673,13 @@ public void collectIdentifiers(Collection<Identifier> identifiers) {
@Override
protected void toExprString(
LocaleContext ctx, StringBuilder sb, boolean isDebug) {
sb.append(_func.getName()).append("(");
sb.append(_func.getName()).append('(');

if (!_params.isEmpty()) {
exprListToString(_params, ",", ctx, sb, isDebug);
}

sb.append(")");
sb.append(')');
}
}

Expand Down Expand Up @@ -1734,7 +1735,7 @@ public void collectIdentifiers(Collection<Identifier> identifiers) {
protected void toExprString(
LocaleContext ctx, StringBuilder sb, boolean isDebug) {
appendLeadingExpr(_left, ctx, sb, isDebug)
.append(_op).append(" ");
.append(_op).append(' ');
_right.toString(ctx, sb, isDebug);
}
}
Expand Down Expand Up @@ -1795,7 +1796,7 @@ protected void toExprString(
LocaleContext ctx, StringBuilder sb, boolean isDebug) {
sb.append(_op);
if (isDebug || ((UnaryOp) _op).needsSpace()) {
sb.append(" ");
sb.append(' ');
}
_expr.toString(ctx, sb, isDebug);
}
Expand Down Expand Up @@ -1939,10 +1940,10 @@ public Value eval(EvalContext ctx) {
protected void toExprString(
LocaleContext ctx, StringBuilder sb, boolean isDebug) {
appendLeadingExpr(_expr, ctx, sb, isDebug)
.append(_op).append(" ");
.append(_op).append(' ');
literalStrToString(_patternStr, sb);
if (isDebug) {
sb.append("(").append(getPattern()).append(")");
sb.append('(').append(getPattern()).append(')');
}
}
}
Expand Down Expand Up @@ -1979,7 +1980,7 @@ protected void toExprString(
appendLeadingExpr(_expr, ctx, sb, isDebug)
.append(_op).append(" (");
exprListToString(_exprs, ",", ctx, sb, isDebug);
sb.append(")");
sb.append(')');
}
}

Expand Down Expand Up @@ -2028,7 +2029,7 @@ public void collectIdentifiers(Collection<Identifier> identifiers) {
protected void toExprString(
LocaleContext ctx, StringBuilder sb, boolean isDebug) {
appendLeadingExpr(_expr, ctx, sb, isDebug)
.append(_op).append(" ");
.append(_op).append(' ');
_startRangeExpr.toString(ctx, sb, isDebug);
sb.append(" And ");
_endRangeExpr.toString(ctx, sb, isDebug);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -752,8 +752,7 @@ private static Fmt parseCustomNumberFormat(ExprBuf buf, Args args) {
String[] fmtStrs = new String[NUM_NF_FMTS];
int fmtIdx = 0;
StringBuilder pendingLiteral = new StringBuilder();
NumberFormatter.NotationType[] expTypes =
new NumberFormatter.NotationType[NUM_NF_FMTS];
NumberFormatter.NotationType[] expTypes = new NumberFormatter.NotationType[NUM_NF_FMTS];
boolean[] hasFmts = new boolean[NUM_NF_FMTS];
boolean[] hasReqDigit = new boolean[NUM_NF_FMTS];

Expand Down Expand Up @@ -1130,7 +1129,7 @@ public static String createNumberFormatPattern(

fmt.append(incLeadDigit ? "0" : "#");
if (numDecDigits > 0) {
fmt.append(".");
fmt.append('.');
fmt.append("0".repeat(Math.max(0, numDecDigits)));
}

Expand All @@ -1140,7 +1139,7 @@ public static String createNumberFormatPattern(
// the javadocs claim the second pattern does not need to be fully
// defined, but it doesn't seem to work that way
String mainPat = fmt.toString();
fmt.append(";(").append(mainPat).append(")");
fmt.append(";(").append(mainPat).append(')');
}

return fmt.toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@
*/
public class AppendQueryImpl extends BaseSelectQueryImpl implements AppendQuery {

public AppendQueryImpl(String name, List<Row> rows, int objectId,
int objectFlag) {
public AppendQueryImpl(String name, List<Row> rows, int objectId, int objectFlag) {
super(name, rows, objectId, objectFlag, Type.APPEND);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@
public abstract class BaseSelectQueryImpl extends QueryImpl
implements BaseSelectQuery {

protected BaseSelectQueryImpl(String name, List<Row> rows, int objectId,
int objectFlag, Type type) {
protected BaseSelectQueryImpl(String name, List<Row> rows, int objectId, int objectFlag, Type type) {
super(name, rows, objectId, objectFlag, type);
}

Expand Down Expand Up @@ -87,8 +86,8 @@ public String getSelectType() {
}

if (hasFlag(TOP_SELECT_TYPE)) {
StringBuilder builder = new StringBuilder();
builder.append("TOP ").append(getFlagRow()._name1);
StringBuilder builder = new StringBuilder("TOP ")
.append(getFlagRow()._name1);
if (hasFlag(PERCENT_SELECT_TYPE)) {
builder.append(" PERCENT");
}
Expand Down
Loading

0 comments on commit 67863cf

Please sign in to comment.