From 7d9f665d07f7b99d159a5079105ee66edd458aec Mon Sep 17 00:00:00 2001 From: Erich Schubert Date: Sun, 21 Oct 2018 17:23:43 +0200 Subject: [PATCH] Refactor shape into enum pattern. --- .../java/org/umlgraph/doclet/Options.java | 6 +- src/main/java/org/umlgraph/doclet/Shape.java | 84 ++++++++++--------- .../java/org/umlgraph/doclet/UmlGraph.java | 2 +- 3 files changed, 48 insertions(+), 44 deletions(-) diff --git a/src/main/java/org/umlgraph/doclet/Options.java b/src/main/java/org/umlgraph/doclet/Options.java index 8412964..2aa0f4b 100644 --- a/src/main/java/org/umlgraph/doclet/Options.java +++ b/src/main/java/org/umlgraph/doclet/Options.java @@ -98,7 +98,7 @@ public class Options implements Cloneable, OptionProvider { String nodeFontTagName = null; double nodeFontPackageSize = -1; String nodeFontPackageName = null; - Shape shape = new Shape(); + Shape shape = Shape.CLASS; String bgColor = null; public String outputFileName = "graph.dot"; String outputEncoding = "ISO-8859-1"; // TODO: default to UTF-8 now? @@ -367,9 +367,9 @@ public class Options implements Cloneable, OptionProvider { } else if (opt[0].equals("-!nodefillcolor")) { nodeFillColor = null; } else if(opt[0].equals("-shape")) { - shape = new Shape(opt[1]); + shape = Shape.of(opt[1]); } else if (opt[0].equals("-!shape")) { - shape = new Shape(); + shape = Shape.CLASS; } else if(opt[0].equals("-output")) { outputFileName = opt[1]; } else if (opt[0].equals("-!output")) { diff --git a/src/main/java/org/umlgraph/doclet/Shape.java b/src/main/java/org/umlgraph/doclet/Shape.java index e5db2eb..a3c0484 100644 --- a/src/main/java/org/umlgraph/doclet/Shape.java +++ b/src/main/java/org/umlgraph/doclet/Shape.java @@ -18,67 +18,71 @@ package org.umlgraph.doclet; +import java.util.HashMap; +import java.util.Locale; + /** * Properties of node shapes * * @version $Revision$ - * @author Diomidis Spinellis + * @author Erich Schubert */ -public class Shape { +public enum Shape { + CLASS(""), // + NOTE(", shape=note"), // + NODE(", shape=box3d"), // + COMPONENT(", shape=component"), // + PACKAGE(", shape=tab"), // + COLLABORATION(", shape=ellipse, style=dashed"), // + USECASE(", shape=ellipse"), // + ACTIVECLASS(""); - /** Shape's UMLGraph name */ - private String name; + /** Graphviz style */ + public final String style; - /** Construct a default (class) Shape */ - public Shape() { - name = "class"; - } + /** Map for valid shape names */ + private static final HashMap index = new HashMap(16); - /** Construct a Shape through the specified UMLGraph name */ - public Shape(String n) { - name = n; - if (graphvizAttribute() == null) { - System.err.println("Ignoring invalid shape " + n); - name = "class"; + /** Initialize the lookup index */ + static { + for (Shape s : Shape.values()) { + index.put(s.name(), s); + index.put(s.name().toLowerCase(Locale.ROOT), s); } } /** - * Return the GraphViz shape name corresponding to the shape + * Get the shape from a string. This allows both the uppercase and the lowercase + * name. Prefer this to {{@link #valueOf(String)} which only accepts uppercase. + * + * @param s String + * @return Shape */ - public String graphvizAttribute() { - if (name.equals("class")) - return ""; // Default; plaintext - else if (name.equals("note")) - return ", shape=note"; - else if (name.equals("node")) - return ", shape=box3d"; - else if (name.equals("component")) - return ", shape=component"; - else if (name.equals("package")) - return ", shape=tab"; - else if (name.equals("collaboration")) - return ", shape=ellipse, style=dashed"; - else if (name.equals("usecase")) - return ", shape=ellipse"; - else if (name.equals("activeclass")) - return ""; // Default; plaintext - else - return null; + public static Shape of(String s) { + Shape shp = index.get(s); + if (shp != null) + return shp; + System.err.println("Ignoring invalid shape: " + s); + return CLASS; + } + + /** Enum constructor, must be private! */ + private Shape(String style) { + this.style = style; } /** Return the shape's GraphViz landing port */ - String landingPort() { - return (name.equals("class") || name.equals("activeclass")) ? ":p" : ""; + public String landingPort() { + return this == CLASS || this == ACTIVECLASS ? ":p" : ""; } /** Return the table border required for the shape */ - String extraColumn() { - return name.equals("activeclass") ? ("") : ""; + public String extraColumn() { + return this == Shape.ACTIVECLASS ? ("") : ""; } /** Return the cell border required for the shape */ - String cellBorder() { - return (name.equals("class") || name.equals("activeclass")) ? "1" : "0"; + public String cellBorder() { + return this == CLASS || this == ACTIVECLASS ? "1" : "0"; } } diff --git a/src/main/java/org/umlgraph/doclet/UmlGraph.java b/src/main/java/org/umlgraph/doclet/UmlGraph.java index 221eebc..edd3825 100644 --- a/src/main/java/org/umlgraph/doclet/UmlGraph.java +++ b/src/main/java/org/umlgraph/doclet/UmlGraph.java @@ -84,7 +84,7 @@ public class UmlGraph { commentOptions = new Options(); commentOptions.setOptions(root.options()); commentOptions.setOptions(findClass(root, "UMLNoteOptions")); - commentOptions.shape = new Shape("note"); + commentOptions.shape = Shape.NOTE; Options opt = new Options(); opt.setOptions(root.options());