Skip to content

Commit 792bf8a

Browse files
committed
Fixes #1: multiple calls to vardump() now just prints dumps from this call
1 parent 5cbcb4e commit 792bf8a

12 files changed

Lines changed: 123 additions & 22 deletions

build.gradle

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
group 'net.workingdeveloper.java'
2-
version '0.0.1-SNAPSHOT'
2+
version '0.2.0'
33

44
apply plugin: 'java'
55

66
sourceCompatibility = 1.8
77

8+
89
repositories {
910
mavenCentral()
1011
}
@@ -13,6 +14,10 @@ dependencies {
1314
compile 'org.apache.commons:commons-lang3:3+'
1415
compile 'org.slf4j:slf4j-api:1.7+'
1516
testCompile group: 'junit', name: 'junit', version: '4.+'
17+
testCompile "org.hamcrest:hamcrest-core:1.+"
18+
testCompile "org.hamcrest:hamcrest-library:1.+"
19+
testCompile group: 'com.tngtech.java', name: 'junit-dataprovider', version: '1+'
20+
testCompile "org.mockito:mockito-core:1.+"
1621
}
1722

1823
task wrapper(type: Wrapper) {
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package net.workingdeveloper.java.vardump;
2+
3+
/**
4+
* Created by Christoph Graupner on 2016-12-04.
5+
*
6+
* @author Christoph Graupner <ch.graupner@workingdeveloper.net>
7+
*/
8+
public interface AppendableFactory {
9+
/**
10+
* Every time it is called it gives a <b>new</b> instance of an Appendable implementation
11+
*
12+
* @return a new instance of an implementation of Appendable
13+
*/
14+
Appendable createAppendable();
15+
}

src/main/java/net/workingdeveloper/java/vardump/IVarDumperFormatter.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ public interface IVarDumperFormatter {
3939

4040
IVarDumperFormatter openObject(Object aName);
4141

42-
IVarDumperFormatter setStringBuffer(Appendable aBuffer);
42+
IVarDumperFormatter reset();
43+
44+
IVarDumperFormatter setAppendableFactory(AppendableFactory aAppendableFactory);
4345

4446
String toString();
4547
}

src/main/java/net/workingdeveloper/java/vardump/VarDumpFactory.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public Predicate<Field> createFieldAcceptPredicate(String[] aExcludeFieldNames,
2727
return new FieldAcceptorPredicate(aExcludeFieldNames, aAppendStatics, aAppendTransients);
2828
}
2929

30-
public IVarDumperFormatter createIndentFormatter(Appendable aAppendable, int aIndent, boolean aShortClassNames) {
30+
public IVarDumperFormatter createIndentFormatter(AppendableFactory aAppendable, int aIndent, boolean aShortClassNames) {
3131
return new VarDumperIndentFormatterImpl(aAppendable, aIndent, aShortClassNames);
3232
}
3333

@@ -48,6 +48,10 @@ public IVarDumper createRecursiveDumper() {
4848
getDefaultFormatter(), getDefaultFieldPredicate(), getDefaultCyclicRegistry());
4949
}
5050

51+
public AppendableFactory getDefaultAppendableFactory() {
52+
return StringBuilder::new;
53+
}
54+
5155
public IVarDumperCyclicRegistry getDefaultCyclicRegistry() {
5256
return new VarDumperCyclicRegistryImpl();
5357
}
@@ -57,6 +61,10 @@ public Predicate<Field> getDefaultFieldPredicate() {
5761
}
5862

5963
public IVarDumperFormatter getDefaultFormatter() {
60-
return new VarDumperIndentFormatterImpl(new StringBuilder(), 2, false);
64+
return new VarDumperIndentFormatterImpl(
65+
getDefaultAppendableFactory(),
66+
2,
67+
false
68+
);
6169
}
6270
}

src/main/java/net/workingdeveloper/java/vardump/impl/AbstractVarDumperFormatter.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package net.workingdeveloper.java.vardump.impl;
22

3+
import net.workingdeveloper.java.vardump.AppendableFactory;
34
import net.workingdeveloper.java.vardump.IVarDumperFormatter;
45

56
import java.io.IOException;
@@ -12,8 +13,12 @@
1213
*/
1314
abstract public class AbstractVarDumperFormatter extends BasicVarDumperFormatter implements IVarDumperFormatter {
1415

15-
public AbstractVarDumperFormatter(Appendable aBuffer, boolean aShortClassName) {
16-
super(aBuffer, aShortClassName);
16+
/**
17+
* @param aAppendableFactory Could be a lamba like <code>StringBuilder::new</code>.
18+
* @param aShortClassName <em>true</em> if the outputted class name should be just the class itself without the package name.
19+
*/
20+
public AbstractVarDumperFormatter(AppendableFactory aAppendableFactory, boolean aShortClassName) {
21+
super(aAppendableFactory, aShortClassName);
1722
}
1823

1924
@Override

src/main/java/net/workingdeveloper/java/vardump/impl/BasicVarDumperFormatter.java

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package net.workingdeveloper.java.vardump.impl;
22

3+
import net.workingdeveloper.java.vardump.AppendableFactory;
34
import net.workingdeveloper.java.vardump.IVarDumperFormatter;
45
import org.slf4j.Logger;
56
import org.slf4j.LoggerFactory;
@@ -21,12 +22,17 @@ enum State {MAP, OBJECT, ARRAY, FIELD}
2122
protected Appendable fBuffer;
2223
Stack<State> fContextStack = new Stack<>();
2324
boolean fShortClassName = false;
25+
private AppendableFactory fBufferFactory;
2426
private String fRefString = "REF>>";
2527
private Logger logger = LoggerFactory.getLogger(VarDumperFormatterImpl.class);
2628

27-
public BasicVarDumperFormatter(Appendable aBuffer, boolean aShortClassName) {
28-
fBuffer = aBuffer;
29+
/**
30+
* @param aAppendableFactory Could be a lamba like <code>StringBuilder::new</code>.
31+
* @param aShortClassName <em>true</em> if the outputted class name should be just the class itself without the package name.
32+
*/
33+
public BasicVarDumperFormatter(AppendableFactory aAppendableFactory, boolean aShortClassName) {
2934
fShortClassName = aShortClassName;
35+
setAppendableFactory(aAppendableFactory);
3036
}
3137

3238
@Override
@@ -48,8 +54,15 @@ public IVarDumperFormatter appendString(String aName) {
4854
}
4955

5056
@Override
51-
public IVarDumperFormatter setStringBuffer(Appendable aBuffer) {
52-
fBuffer = aBuffer;
57+
public IVarDumperFormatter reset() {
58+
fBuffer = fBufferFactory.createAppendable();
59+
return this;
60+
}
61+
62+
@Override
63+
public IVarDumperFormatter setAppendableFactory(AppendableFactory aAppendableFactory) {
64+
fBufferFactory = aAppendableFactory;
65+
fBuffer = aAppendableFactory.createAppendable();
5366
return this;
5467
}
5568

src/main/java/net/workingdeveloper/java/vardump/impl/RecursiveVarDumperImpl.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ public boolean isAppendTransients() {
6565

6666
@Override
6767
public String vardump(final Object aObject, final IVarDumperFormatter aFormatter) {
68+
reset();
6869
setFormatter(aFormatter);
6970
reflectionSwitch(aObject);
7071
return getFormatter().toString();
@@ -120,6 +121,10 @@ protected void reflectionSwitch(final Object aObject) {
120121
}
121122
}
122123

124+
private void reset() {
125+
getFormatter().reset();
126+
}
127+
123128
private void reflectMap(Object aObject) {
124129
Map<?, ?> lMapObject = (Map<?, ?>) aObject;
125130
Set<? extends Map.Entry<?, ?>> lEntrySet = lMapObject.entrySet();

src/main/java/net/workingdeveloper/java/vardump/impl/VarDumperFormatterImpl.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package net.workingdeveloper.java.vardump.impl;
22

3+
import net.workingdeveloper.java.vardump.AppendableFactory;
34
import net.workingdeveloper.java.vardump.IVarDumperFormatter;
45

56
/**
@@ -11,7 +12,11 @@ public class VarDumperFormatterImpl
1112
extends AbstractVarDumperFormatter
1213
implements IVarDumperFormatter {
1314

14-
public VarDumperFormatterImpl(Appendable aBuffer, boolean aShortClassName) {
15-
super(aBuffer, aShortClassName);
15+
/**
16+
* @param aAppendableFactory Could be a lamba like <code>StringBuilder::new</code>.
17+
* @param aShortClassName <em>true</em> if the outputted class name should be just the class itself without the package name.
18+
*/
19+
public VarDumperFormatterImpl(AppendableFactory aAppendableFactory, boolean aShortClassName) {
20+
super(aAppendableFactory, aShortClassName);
1621
}
1722
}

src/main/java/net/workingdeveloper/java/vardump/impl/VarDumperFormatterSimpleImpl.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package net.workingdeveloper.java.vardump.impl;
22

3+
import net.workingdeveloper.java.vardump.AppendableFactory;
34
import net.workingdeveloper.java.vardump.IVarDumperFormatter;
45

56
/**
@@ -10,7 +11,7 @@
1011
public class VarDumperFormatterSimpleImpl
1112
extends AbstractVarDumperFormatter
1213
implements IVarDumperFormatter {
13-
public VarDumperFormatterSimpleImpl(Appendable aBuffer, boolean aShortClassName) {
14-
super(aBuffer, aShortClassName);
14+
public VarDumperFormatterSimpleImpl(AppendableFactory aAppendableFactory, boolean aShortClassName) {
15+
super(aAppendableFactory, aShortClassName);
1516
}
1617
}

src/main/java/net/workingdeveloper/java/vardump/impl/VarDumperIndentFormatterImpl.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package net.workingdeveloper.java.vardump.impl;
22

3+
import net.workingdeveloper.java.vardump.AppendableFactory;
34
import net.workingdeveloper.java.vardump.IVarDumperFormatter;
45

56
/**
@@ -11,7 +12,7 @@ public class VarDumperIndentFormatterImpl extends AbstractVarDumperFormatter imp
1112
private final int fIndentLevel;
1213
private int fCurrentIndention;
1314

14-
public VarDumperIndentFormatterImpl(Appendable aBuffer, int aIndentLevel, boolean aShortClassName) {
15+
public VarDumperIndentFormatterImpl(AppendableFactory aBuffer, int aIndentLevel, boolean aShortClassName) {
1516
super(aBuffer, aShortClassName);
1617
fIndentLevel = aIndentLevel;
1718
}

0 commit comments

Comments
 (0)