Skip to content

Commit ed367c5

Browse files
committed
avoid nullable Rasterizer and GraphicsConfigurer parameters
Signed-off-by: Stefan Niederhauser <[email protected]>
1 parent cfbf64a commit ed367c5

File tree

15 files changed

+75
-45
lines changed

15 files changed

+75
-45
lines changed

graphviz-java/src/main/java/guru/nidi/graphviz/engine/AbstractJsGraphvizEngine.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ protected void doInit() {
5252
final JavascriptEngine engine = engine(true);
5353
engine.executeJavascript(vizJsCode());
5454
engine.executeJavascript(renderJsCode());
55-
execute("graph g { a -- b }", Options.create(), null);
55+
execute("graph g { a -- b }", Options.create(), Rasterizer.NONE);
5656
}
5757
}
5858

@@ -93,7 +93,7 @@ public void close() {
9393
}
9494

9595
@Override
96-
public EngineResult execute(String src, Options options, @Nullable Rasterizer rasterizer) {
96+
public EngineResult execute(String src, Options options, Rasterizer rasterizer) {
9797
if (rasterizer instanceof BuiltInRasterizer) {
9898
throw new GraphvizException("Built-in Rasterizer can only be used together with GraphvizCmdLineEngine.");
9999
}

graphviz-java/src/main/java/guru/nidi/graphviz/engine/BatikRasterizer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727

2828
class BatikRasterizer extends SvgRasterizer {
2929
@Override
30-
public BufferedImage doRasterize(Graphviz graphviz, @Nullable Consumer<Graphics2D> graphicsConfigurer, String svg) {
30+
public BufferedImage doRasterize(Graphviz graphviz, Consumer<Graphics2D> graphicsConfigurer, String svg) {
3131
final BufferedImage[] image = new BufferedImage[1];
3232
final TranscoderInput in = new TranscoderInput(new StringReader(svg));
3333
try {

graphviz-java/src/main/java/guru/nidi/graphviz/engine/BuiltInRasterizer.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ public Format format() {
3939
}
4040

4141
@Override
42-
public BufferedImage rasterize(Graphviz graphviz, @Nullable Consumer<Graphics2D> graphicsConfigurer, String input) {
42+
@SuppressWarnings("ConstantConditions")
43+
public BufferedImage rasterize(Graphviz graphviz, Consumer<Graphics2D> graphicsConfigurer, String input) {
4344
return null;
4445
}
4546
}

graphviz-java/src/main/java/guru/nidi/graphviz/engine/Graphviz.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import static guru.nidi.graphviz.attribute.validate.ValidatorFormat.UNKNOWN_FORMAT;
3333
import static guru.nidi.graphviz.attribute.validate.ValidatorMessage.POSITION_LOGGING_CONSUMER;
3434
import static guru.nidi.graphviz.engine.GraphvizLoader.readAsString;
35+
import static guru.nidi.graphviz.engine.Rasterizer.NONE;
3536
import static java.lang.Double.parseDouble;
3637
import static java.util.Arrays.asList;
3738
import static java.util.regex.Pattern.CASE_INSENSITIVE;
@@ -52,7 +53,6 @@ public final class Graphviz {
5253
@Nullable
5354
private final String src;
5455

55-
@Nullable
5656
final Rasterizer rasterizer;
5757
final ProcessOptions processOptions;
5858
private final Options options;
@@ -64,7 +64,7 @@ private Graphviz(@Nullable MutableGraph graph, @Nullable String src, ProcessOpti
6464
new ArrayList<>(), POSITION_LOGGING_CONSUMER);
6565
}
6666

67-
private Graphviz(@Nullable MutableGraph graph, @Nullable String src, @Nullable Rasterizer rasterizer,
67+
private Graphviz(@Nullable MutableGraph graph, @Nullable String src, Rasterizer rasterizer,
6868
ProcessOptions processOptions, Options options,
6969
List<GraphvizFilter> filters, Consumer<ValidatorMessage> messageConsumer) {
7070
this.graph = graph;
@@ -238,22 +238,21 @@ public Graphviz messageConsumer(Consumer<ValidatorMessage> messageConsumer) {
238238
return new Graphviz(graph, src, rasterizer, processOptions, options, filters, messageConsumer);
239239
}
240240

241-
// TODO avoid nullable
242-
public Renderer rasterize(@Nullable Rasterizer rasterizer) {
243-
if (rasterizer == null) {
241+
public Renderer rasterize(Rasterizer rasterizer) {
242+
if (rasterizer == NONE) {
244243
throw new IllegalArgumentException("The provided rasterizer implementation was not found."
245244
+ " Make sure that either 'guru.nidi.com.kitfox:svgSalamander' or"
246245
+ " 'org.apache.xmlgraphics:batik-rasterizer' is available on the classpath.");
247246
}
248247
final Options opts = options.format(rasterizer.format());
249248
final Graphviz graphviz = new Graphviz(graph, src, rasterizer, processOptions, opts, filters, messageConsumer);
250-
return new Renderer(graphviz, null, Format.PNG);
249+
return new Renderer(graphviz, Format.PNG);
251250
}
252251

253252
public Renderer render(Format format) {
254253
final Options opts = options.format(format);
255254
final Graphviz g = new Graphviz(graph, src, rasterizer, processOptions, opts, filters, messageConsumer);
256-
return new Renderer(g, null, format);
255+
return new Renderer(g, format);
257256
}
258257

259258
EngineResult execute() {
@@ -297,7 +296,7 @@ public void init(Consumer<GraphvizEngine> onOk, Consumer<GraphvizEngine> onError
297296
}
298297

299298
@Override
300-
public EngineResult execute(String src, Options options, @Nullable Rasterizer rasterizer) {
299+
public EngineResult execute(String src, Options options, Rasterizer rasterizer) {
301300
return EngineResult.fromString("");
302301
}
303302

graphviz-java/src/main/java/guru/nidi/graphviz/engine/GraphvizCmdLineEngine.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ protected void doInit() {
8080
}
8181

8282
@Override
83-
public EngineResult execute(String src, Options options, @Nullable Rasterizer rasterizer) {
83+
public EngineResult execute(String src, Options options, Rasterizer rasterizer) {
8484
try {
8585
final Path path = Files.createTempDirectory(getOrCreateTempDirectory().toPath(), "DotEngine");
8686
final File dotFile = getDotFile(path);
@@ -94,7 +94,7 @@ public EngineResult execute(String src, Options options, @Nullable Rasterizer ra
9494
}
9595
}
9696

97-
private EngineResult doExecute(Path path, File dotFile, Options options, @Nullable Rasterizer rasterizer)
97+
private EngineResult doExecute(Path path, File dotFile, Options options, Rasterizer rasterizer)
9898
throws IOException, InterruptedException {
9999
final String engine = getEngineExecutable(options.engine);
100100
final String format = getFormatName(options.format, rasterizer);
@@ -129,7 +129,7 @@ private String getEngineExecutable(@Nullable Engine engine) {
129129
throw e;
130130
}
131131

132-
private String getFormatName(@Nullable Format format, @Nullable Rasterizer rasterizer) {
132+
private String getFormatName(@Nullable Format format, Rasterizer rasterizer) {
133133
if (rasterizer instanceof BuiltInRasterizer) {
134134
final BuiltInRasterizer natRast = (BuiltInRasterizer) rasterizer;
135135
String f = natRast.format;

graphviz-java/src/main/java/guru/nidi/graphviz/engine/GraphvizEngine.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,5 @@
2525
public interface GraphvizEngine extends AutoCloseable {
2626
void init(Consumer<GraphvizEngine> onOk, Consumer<GraphvizEngine> onError);
2727

28-
EngineResult execute(String src, Options options, @Nullable Rasterizer rasterizer);
28+
EngineResult execute(String src, Options options, Rasterizer rasterizer);
2929
}

graphviz-java/src/main/java/guru/nidi/graphviz/engine/GraphvizServerEngine.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public GraphvizServerEngine timeout(int amount, TimeUnit unit) {
5353
}
5454

5555
@Override
56-
public EngineResult execute(String src, Options options, @Nullable Rasterizer rasterizer) {
56+
public EngineResult execute(String src, Options options, Rasterizer rasterizer) {
5757
try {
5858
return EngineResult.fromString(createSvg(src, options));
5959
} catch (SocketTimeoutException e) {
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Copyright © 2015 Stefan Niederhauser ([email protected])
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+
package guru.nidi.graphviz.engine;
17+
18+
import java.awt.*;
19+
import java.awt.image.BufferedImage;
20+
import java.util.function.Consumer;
21+
22+
@SuppressWarnings("ConstantConditions")
23+
class NopRasterizer implements Rasterizer {
24+
@Override
25+
public Format format() {
26+
return null;
27+
}
28+
29+
@Override
30+
public BufferedImage rasterize(Graphviz graphviz, Consumer<Graphics2D> graphicsConfigurer, String input) {
31+
return null;
32+
}
33+
}

graphviz-java/src/main/java/guru/nidi/graphviz/engine/Rasterizer.java

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,25 +17,21 @@
1717

1818
import org.slf4j.LoggerFactory;
1919

20-
import javax.annotation.Nullable;
2120
import java.awt.*;
2221
import java.awt.image.BufferedImage;
2322
import java.util.function.Consumer;
2423

2524
import static guru.nidi.graphviz.engine.GraphvizLoader.isOnClasspath;
2625

2726
public interface Rasterizer {
28-
@Nullable
29-
Rasterizer BATIK = isOnClasspath("org/apache/batik/transcoder/Transcoder.class") ? new BatikRasterizer() : null;
30-
@Nullable
31-
Rasterizer SALAMANDER = isOnClasspath("com/kitfox/svg/SVGDiagram.class") ? new SalamanderRasterizer() : null;
32-
@Nullable
27+
Rasterizer NONE = new NopRasterizer();
28+
Rasterizer BATIK = isOnClasspath("org/apache/batik/transcoder/Transcoder.class") ? new BatikRasterizer() : NONE;
29+
Rasterizer SALAMANDER = isOnClasspath("com/kitfox/svg/SVGDiagram.class") ? new SalamanderRasterizer() : NONE;
3330
Rasterizer DEFAULT = getDefault();
3431

35-
@Nullable
3632
static Rasterizer getDefault() {
37-
final Rasterizer r = BATIK != null ? BATIK : SALAMANDER;
38-
if (r == null) {
33+
final Rasterizer r = BATIK != NONE ? BATIK : SALAMANDER;
34+
if (r == NONE) {
3935
LoggerFactory.getLogger(Rasterizer.class).warn("Neither Batik nor Salamander found on classpath");
4036
}
4137
return r;
@@ -55,5 +51,5 @@ static Rasterizer builtIn(String format, String renderer, String formatter) {
5551

5652
Format format();
5753

58-
BufferedImage rasterize(Graphviz graphviz, @Nullable Consumer<Graphics2D> graphicsConfigurer, String input);
54+
BufferedImage rasterize(Graphviz graphviz, Consumer<Graphics2D> graphicsConfigurer, String input);
5955
}

graphviz-java/src/main/java/guru/nidi/graphviz/engine/Renderer.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
*/
1616
package guru.nidi.graphviz.engine;
1717

18-
import javax.annotation.Nullable;
1918
import javax.imageio.ImageIO;
2019
import java.awt.*;
2120
import java.awt.image.BufferedImage;
@@ -24,16 +23,22 @@
2423
import java.nio.file.StandardCopyOption;
2524
import java.util.function.Consumer;
2625

26+
import static guru.nidi.graphviz.engine.Rasterizer.NONE;
2727
import static java.nio.charset.StandardCharsets.UTF_8;
2828
import static java.util.Locale.ENGLISH;
2929

3030
public class Renderer {
31+
private static final Consumer<Graphics2D> NOP_GRAPHICS_CONFIGURER = Graphics2D -> {
32+
};
3133
private final Graphviz graphviz;
32-
@Nullable
3334
private final Consumer<Graphics2D> graphicsConfigurer;
3435
private final Format output;
3536

36-
Renderer(Graphviz graphviz, @Nullable Consumer<Graphics2D> graphicsConfigurer, Format output) {
37+
Renderer(Graphviz graphviz, Format output) {
38+
this(graphviz, NOP_GRAPHICS_CONFIGURER, output);
39+
}
40+
41+
Renderer(Graphviz graphviz, Consumer<Graphics2D> graphicsConfigurer, Format output) {
3742
this.graphviz = graphviz;
3843
this.graphicsConfigurer = graphicsConfigurer;
3944
this.output = output;
@@ -110,7 +115,7 @@ private BufferedImage toImage(EngineResult result) {
110115
}
111116

112117
private BufferedImage toImage(String content) {
113-
if (graphviz.rasterizer == null) {
118+
if (graphviz.rasterizer == NONE) {
114119
throw new IllegalStateException("- Rasterizer explicitly set no null or\n"
115120
+ "- neither 'guru.nidi.com.kitfox:svgSalamander' nor 'org.apache.xmlgraphics:batik-rasterizer'"
116121
+ " found on classpath.");

0 commit comments

Comments
 (0)