Skip to content

Commit 77568ec

Browse files
committed
make reuse of CreationContext possible
Signed-off-by: Stefan Niederhauser <[email protected]>
1 parent f0fe39e commit 77568ec

File tree

4 files changed

+46
-14
lines changed

4 files changed

+46
-14
lines changed

graphviz-java/src/main/java/guru/nidi/graphviz/model/CreationContext.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,15 @@ public static <T> T use(@Nullable MutableGraph graph, ThrowingFunction<CreationC
4747
}
4848
}
4949

50+
public <T> T reuse(ThrowingFunction<CreationContext, T> actions) {
51+
CONTEXT.get().push(this);
52+
try {
53+
return actions.applyNotThrowing(this);
54+
} finally {
55+
end();
56+
}
57+
}
58+
5059
public static Optional<CreationContext> current() {
5160
final Stack<CreationContext> cs = CONTEXT.get();
5261
return cs.empty() ? Optional.empty() : Optional.of(cs.peek());

graphviz-java/src/main/java/guru/nidi/graphviz/model/MutableGraph.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525

2626
public class MutableGraph implements LinkSource, LinkTarget {
2727
private static final SafeRecursion<MutableGraph> RECURSION = new SafeRecursion<>();
28+
private static final String CONTEXT = "$context";
29+
2830
protected boolean strict;
2931
protected boolean directed;
3032
protected boolean cluster;
@@ -71,11 +73,23 @@ public Graph toImmutable() {
7173

7274
public MutableGraph use(ThrowingBiConsumer<MutableGraph, CreationContext> actions) {
7375
return CreationContext.use(this, ctx -> {
76+
graphAttrs.add(CONTEXT, ctx);
7477
actions.accept(this, ctx);
7578
return this;
7679
});
7780
}
7881

82+
public MutableGraph reuse(ThrowingBiConsumer<MutableGraph, CreationContext> actions) {
83+
final Object c = graphAttrs.get(CONTEXT);
84+
if (c instanceof CreationContext) {
85+
return ((CreationContext) c).reuse(ctx -> {
86+
actions.accept(this, ctx);
87+
return this;
88+
});
89+
}
90+
return use(actions);
91+
}
92+
7993
public MutableGraph setStrict(boolean strict) {
8094
this.strict = strict;
8195
return this;

graphviz-java/src/main/java/guru/nidi/graphviz/model/SerializerImpl.java

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,12 @@ private int indexOfName(List<MutableNode> nodes, Label name) {
124124
}
125125

126126
private void attributes(String name, Attributes<?> attributed, Scope scope, Location location) {
127-
if (!attributed.isEmpty()) {
128-
str.append(name);
129-
attrs(attributed, scope, location);
127+
final int len = str.length();
128+
str.append(name);
129+
attrs(attributed, scope, location);
130+
if (str.length() == len + name.length()) {
131+
str.delete(len, str.length());
132+
} else {
130133
str.append('\n');
131134
}
132135
}
@@ -235,19 +238,19 @@ private void port(ImmutablePortNode portNode) {
235238
}
236239

237240
private void attrs(Attributes<?> attrs, Scope scope, Location location) {
238-
if (!attrs.isEmpty()) {
239-
str.append(" [");
240-
boolean first = true;
241-
for (final Entry<String, Object> attr : attrs) {
242-
if (attr.getValue() != null) {
243-
if (first) {
244-
first = false;
245-
} else {
246-
str.append(',');
247-
}
248-
attr(attr.getKey(), attr.getValue(), scope, location);
241+
boolean first = true;
242+
for (final Entry<String, Object> attr : attrs) {
243+
if (!attr.getKey().startsWith("$") && attr.getValue() != null) {
244+
if (first) {
245+
str.append(" [");
246+
first = false;
247+
} else {
248+
str.append(',');
249249
}
250+
attr(attr.getKey(), attr.getValue(), scope, location);
250251
}
252+
}
253+
if (!first) {
251254
str.append(']');
252255
}
253256
}

graphviz-java/src/test/java/guru/nidi/graphviz/model/SerializerTest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,12 @@ void mixedDirected() {
214214
"digraph {\nedge ['dir'='none']\n'a' -> {\nedge ['dir'='forward']\n'b' -> 'c'\n}\n}");
215215
}
216216

217+
@Test
218+
void privateAttribute() {
219+
assertSerialize(graph().graphAttr().with("$a", "b").with(node("a").with("$b", "c")),
220+
"graph {\n'a'\n}");
221+
}
222+
217223
private void assertSerialize(Graph graph, String expectedString, ValidatorMessage... expectedMessages) {
218224
assertEquals(expectedString.replace("'", "\""), ser.serializer.serialize(graph));
219225
assertEquals(asList(expectedMessages), ser.messages);

0 commit comments

Comments
 (0)