Skip to content

Commit f327828

Browse files
authored
Merge pull request #16 from wavesoftware/feature/pretty-print
Support for pretty print
2 parents e3f7c45 + d80c1f5 commit f327828

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+1128
-313
lines changed

README.adoc

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
== Stringify Object for Java
1+
= Stringify Object for Java
22

33
https://travis-ci.org/wavesoftware/java-stringify-object[image:https://travis-ci.org/wavesoftware/java-stringify-object.svg?branch=master[Build
44
Status]]
@@ -22,20 +22,20 @@ inspected.
2222
This library has proper support for object graph cycles, and JPA
2323
(Hibernate) lazy loaded elements.
2424

25-
=== Usage
25+
== Usage
2626

27-
==== In Promiscuous mode
27+
=== In Promiscuous mode
2828

2929
[source,java]
3030
----
3131
// In PROMISCUOUS mode define fields to exclude
3232
class Person {
3333
private int id;
34-
@DisplayNull
34+
@DisplayNull // <1>
3535
private Person parent;
3636
private List<Person> childs;
3737
private Account account;
38-
@Inspect(conditionally = IsInDevelopment.class)
38+
@Inspect(conditionally = IsInDevelopment.class) // <2>
3939
private String password;
4040
@DoNotInspect
4141
private String ignored;
@@ -46,12 +46,15 @@ List<Person> people = query.getResultList();
4646
Stringify stringify = Stringify.of(people);
4747
stringify.mode(Mode.PROMISCUOUS);
4848
// stringify.beanFactory(...);
49-
assert "<Person id=15, parent=<Person id=16, parent=null, "
49+
assert "[<Person id=15, parent=<Person id=16, parent=null, "
5050
+ "childs=[(↻)], account=⁂Lazy>, childs=[], "
51-
+ "account=⁂Lazy>".equals(stringify.toString());
51+
+ "account=⁂Lazy>]".equals(stringify.toString());
5252
----
5353

54-
==== In Quiet mode
54+
<1> Configures a field inspection to show null values
55+
<2> Inspects a field, but only if given predicate returns `true`
56+
57+
=== In Quiet mode
5558

5659
[source,java]
5760
----
@@ -68,12 +71,12 @@ class Person {
6871
List<Person> people = query.getResultList();
6972
Stringify stringify = Stringify.of(people);
7073
stringify.mode(Mode.QUIET);
71-
assert "<Person id=15, parent=<Person id=16, parent=null, "
74+
assert "[<Person id=15, parent=<Person id=16, parent=null, "
7275
+ "childs=[(↻)], account=⁂Lazy>, childs=[], "
73-
+ "account=⁂Lazy>".equals(stringify.toString());
76+
+ "account=⁂Lazy>]".equals(stringify.toString());
7477
----
7578

76-
=== Features
79+
== Features
7780

7881
* String representation of any Java class in two modes `+PROMISCUOUS+`
7982
and `+QUIET+`
@@ -83,7 +86,7 @@ and `+QUIET+`
8386
instead
8487

8588
[[vs-lombok-tostring]]
86-
=== vs. Lombok @ToString
89+
== vs. Lombok @ToString
8790

8891
Stringify Object for Java is designed for *slightly different* use case
8992
then Lombok.
@@ -95,13 +98,13 @@ Stringify Object for Java is designed to inspect complex objects that
9598
can have cycles and can be managed by JPA provider like Hibernate
9699
(introducing Lazy Loading problems).
97100

98-
==== Pros of Lombok vs Stringify Object
101+
=== Pros of Lombok vs Stringify Object
99102

100103
* Lombok is *fast* - it's statically generated code without using
101104
Reflection API.
102105
* Lombok is *easy* - it's zero configuration in most cases.
103106

104-
==== Cons of Lombok vs Stringify Object
107+
=== Cons of Lombok vs Stringify Object
105108

106109
* Lombok can't *detect cycles* is object graph, which implies
107110
`+StackOverflowException+` being thrown in that case
@@ -110,12 +113,12 @@ loading it from JPA by invoking SQL statements. It's typical *n+1
110113
problem*, but with nasty consequences - your `+toString()+` method is
111114
invoking SQL without your knowledge!!
112115

113-
=== Configuration
116+
== Configuration
114117

115118
Configuration is done in two ways: declarative - using Java's service
116119
loader mechanism, and programmatic.
117120

118-
==== Configuration using Service Loader
121+
=== Configuration using Service Loader
119122

120123
A `+Configurator+` interface is intended to be implemented in user code,
121124
and assigned to https://www.baeldung.com/java-spi[Service Loader]
@@ -186,7 +189,7 @@ class SpringBeanFactory implements BeanFactory, BootingAware {
186189
}
187190
----
188191

189-
==== Programmatic configuration
192+
=== Programmatic configuration
190193

191194
You can also fine tune you configuration on instance level - using
192195
methods available at `+Stringify+` interface:
@@ -205,13 +208,13 @@ stringifier
205208
.stringify();
206209
----
207210

208-
=== Dependencies
211+
== Dependencies
209212

210213
* Java >= 8
211214
* https://github.com/wavesoftware/java-eid-exceptions[EID Exceptions]
212215
library
213216

214-
==== Contributing
217+
=== Contributing
215218

216219
Contributions are welcome!
217220

src/main/java/pl/wavesoftware/utils/stringify/Stringify.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,13 @@
1919
import pl.wavesoftware.utils.stringify.api.Configuration;
2020
import pl.wavesoftware.utils.stringify.api.Inspect;
2121
import pl.wavesoftware.utils.stringify.api.Mode;
22+
import pl.wavesoftware.utils.stringify.api.Namespace;
23+
import pl.wavesoftware.utils.stringify.api.Store;
2224
import pl.wavesoftware.utils.stringify.impl.DefaultStringify;
2325
import pl.wavesoftware.utils.stringify.spi.BeanFactory;
26+
import pl.wavesoftware.utils.stringify.spi.theme.Theme;
27+
28+
import java.util.function.Consumer;
2429

2530
/**
2631
* <h1>Stringify Object for Java</h1>
@@ -129,4 +134,10 @@ static Stringify of(Object object) {
129134

130135
@Override
131136
Stringify beanFactory(BeanFactory beanFactory);
137+
138+
@Override
139+
Stringify theme(Theme theme);
140+
141+
@Override
142+
Stringify store(Namespace namespace, Consumer<Store> storeConsumer);
132143
}

src/main/java/pl/wavesoftware/utils/stringify/api/Configuration.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
import pl.wavesoftware.utils.stringify.spi.Configurator;
2121
import pl.wavesoftware.utils.stringify.spi.theme.Theme;
2222

23+
import java.util.function.Consumer;
24+
2325
/**
2426
* A interface that represents a configuration options for this library
2527
* <p>
@@ -39,10 +41,10 @@
3941
* {@link BeanFactory} interface can be used to create instances of classes, declared
4042
* in annotations used to define inspection rules.
4143
*
44+
* @author <a href="mailto:krzysztof.suszynski@wavesoftware.pl">Krzysztof Suszynski</a>
4245
* @see Configurator
4346
* @see BeanFactory
4447
* @see pl.wavesoftware.utils.stringify.Stringify Stringify
45-
* @author <a href="mailto:krzysztof.suszynski@wavesoftware.pl">Krzysztof Suszynski</a>
4648
* @since 2.0.0
4749
*/
4850
public interface Configuration {
@@ -69,4 +71,13 @@ public interface Configuration {
6971
* @return a self reference
7072
*/
7173
Configuration theme(Theme theme);
74+
75+
/**
76+
* A named general purpose configuration
77+
*
78+
* @param namespace a namespace oa a store to configure
79+
* @param storeConsumer a consumer that can modify a named store
80+
* @return a self reference
81+
*/
82+
Configuration store(Namespace namespace, Consumer<Store> storeConsumer);
7283
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Copyright 2018-2019 Wave Software
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package pl.wavesoftware.utils.stringify.api;
18+
19+
/**
20+
* An indentation of output that can be used by
21+
* {@link pl.wavesoftware.utils.stringify.spi.theme.Theme Theme} elements to prepare
22+
* pretty print.
23+
*
24+
* @author <a href="mailto:krzysztof.suszynski@wavesoftware.pl">Krzysztof Suszynski</a>
25+
* @since 2.0.0
26+
*/
27+
public interface IndentationControl {
28+
/**
29+
* Current level of indentation.
30+
*
31+
* @return an indentation level
32+
*/
33+
int current();
34+
35+
/**
36+
* Increments current level of indentation.
37+
*/
38+
void increment();
39+
40+
/**
41+
* Decrements current level of indentation.
42+
*/
43+
void decrement();
44+
45+
/**
46+
* Calculates a indent text based on a indent value and current level
47+
*
48+
* @param indent a text representation of indent
49+
* @return a complete indent
50+
*/
51+
default CharSequence indent(CharSequence indent) {
52+
StringBuilder builder = new StringBuilder();
53+
for (int i = 0; i < current(); i++) {
54+
builder.append(indent);
55+
}
56+
return builder;
57+
}
58+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright 2018-2019 Wave Software
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package pl.wavesoftware.utils.stringify.api;
18+
19+
/**
20+
* An context of current inspection.
21+
*
22+
* @author <a href="mailto:krzysztof.suszynski@wavesoftware.pl">Krzysztof Suszynski</a>
23+
* @since 2.0.0
24+
*/
25+
public interface InspectionContext {
26+
/**
27+
* Control of indentation.
28+
*
29+
* @return a control of indentation
30+
*/
31+
IndentationControl indentationControl();
32+
33+
/**
34+
* A named, general purpose store
35+
*
36+
* @param namespace a namespace of a store
37+
* @return a store
38+
*/
39+
Store store(Namespace namespace);
40+
}

src/main/java/pl/wavesoftware/utils/stringify/api/InspectionPoint.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,32 +16,32 @@
1616

1717
package pl.wavesoftware.utils.stringify.api;
1818

19-
import java.lang.reflect.Field;
2019
import java.util.function.Supplier;
2120

2221
/**
23-
* This interface represents a inspection point in some object.
22+
* Represents a inspection point in some object, that is a field with value.
2423
*
2524
* @author <a href="mailto:krzysztof.suszynski@coi.gov.pl">Krzysztof Suszynski</a>
2625
* @since 1.0.0
2726
*/
2827
public interface InspectionPoint {
2928
/**
30-
* Get field representation of inspection point
31-
* @return a field
29+
* Get a field value supplier
30+
*
31+
* @return a supplier of a field value
3232
*/
33-
Field getField();
33+
Supplier<Object> getValue();
3434

3535
/**
36-
* Get object that contains this inspection point
37-
* @return an object
36+
* Get a type of a field
37+
* @return a type of a field
3838
*/
39-
Object getContainingObject();
39+
Supplier<Class<?>> getType();
4040

4141
/**
42-
* Get a field value supplier
42+
* Gets a context object associated with this inspection point
4343
*
44-
* @return a supplier of a field value
44+
* @return a context object
4545
*/
46-
Supplier<Object> getValueSupplier();
46+
InspectionContext getContext();
4747
}

0 commit comments

Comments
 (0)