Restructures demos.
This commit is contained in:
9
demos/java/.classpath
Normal file
9
demos/java/.classpath
Normal file
@@ -0,0 +1,9 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<classpath>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-11"/>
|
||||
<classpathentry kind="src" path="src"/>
|
||||
<classpathentry kind="output" path="bin"/>
|
||||
<classpathentry exported="true" kind="lib" path="lib/jackson-annotations-2.9.8.jar"/>
|
||||
<classpathentry exported="true" kind="lib" path="lib/jackson-core-2.9.8.jar"/>
|
||||
<classpathentry exported="true" kind="lib" path="lib/jackson-databind-2.9.8.jar"/>
|
||||
</classpath>
|
||||
25
demos/java/.gitignore
vendored
Normal file
25
demos/java/.gitignore
vendored
Normal file
@@ -0,0 +1,25 @@
|
||||
# Compiled class file
|
||||
*.class
|
||||
|
||||
# Log file
|
||||
*.log
|
||||
|
||||
# BlueJ files
|
||||
*.ctxt
|
||||
|
||||
# Mobile Tools for Java (J2ME)
|
||||
.mtj.tmp/
|
||||
|
||||
# Package Files #
|
||||
*.jar
|
||||
*.war
|
||||
*.nar
|
||||
*.ear
|
||||
*.zip
|
||||
*.tar.gz
|
||||
*.rar
|
||||
|
||||
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
|
||||
hs_err_pid*
|
||||
|
||||
!lib/**
|
||||
17
demos/java/.project
Normal file
17
demos/java/.project
Normal file
@@ -0,0 +1,17 @@
|
||||
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
|
||||
<projectDescription>
|
||||
<name>java</name>
|
||||
<comment/>
|
||||
<projects>
|
||||
</projects>
|
||||
<buildSpec>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.jdt.core.javabuilder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
</buildSpec>
|
||||
<natures>
|
||||
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||
</natures>
|
||||
</projectDescription>
|
||||
11
demos/java/.settings/org.eclipse.jdt.core.prefs
Normal file
11
demos/java/.settings/org.eclipse.jdt.core.prefs
Normal file
@@ -0,0 +1,11 @@
|
||||
eclipse.preferences.version=1
|
||||
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
|
||||
org.eclipse.jdt.core.compiler.codegen.targetPlatform=11
|
||||
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
|
||||
org.eclipse.jdt.core.compiler.compliance=10
|
||||
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
|
||||
org.eclipse.jdt.core.compiler.debug.localVariable=generate
|
||||
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
|
||||
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
|
||||
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
|
||||
org.eclipse.jdt.core.compiler.source=11
|
||||
20
demos/java/.vscode/launch.json
vendored
Normal file
20
demos/java/.vscode/launch.json
vendored
Normal file
@@ -0,0 +1,20 @@
|
||||
{
|
||||
// Use IntelliSense to find out which attributes exist for C# debugging
|
||||
// Use hover for the description of the existing attributes
|
||||
// For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md
|
||||
"version": "0.2.0",
|
||||
"configurations": [
|
||||
{
|
||||
"type": "java",
|
||||
"name": "CodeLens (Launch) - DemoLinkedLists",
|
||||
"request": "launch",
|
||||
"mainClass": "DemoLinkedLists"
|
||||
},
|
||||
{
|
||||
"name": "Java - Demo",
|
||||
"type": "java",
|
||||
"request": "launch",
|
||||
"projectName": "java"
|
||||
}
|
||||
]
|
||||
}
|
||||
5
demos/java/.vscode/settings.json
vendored
Normal file
5
demos/java/.vscode/settings.json
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
{
|
||||
"java.dependency.packagePresentation": "flat",
|
||||
"java.dependency.syncWithFolderExplorer": true,
|
||||
"debugVisualizer.debugAdapterConfigurations": {}
|
||||
}
|
||||
BIN
demos/java/lib/jackson-annotations-2.9.8.jar
Normal file
BIN
demos/java/lib/jackson-annotations-2.9.8.jar
Normal file
Binary file not shown.
BIN
demos/java/lib/jackson-core-2.9.8.jar
Normal file
BIN
demos/java/lib/jackson-core-2.9.8.jar
Normal file
Binary file not shown.
BIN
demos/java/lib/jackson-databind-2.9.8.jar
Normal file
BIN
demos/java/lib/jackson-databind-2.9.8.jar
Normal file
Binary file not shown.
58
demos/java/src/app/App.java
Normal file
58
demos/java/src/app/App.java
Normal file
@@ -0,0 +1,58 @@
|
||||
package app;
|
||||
|
||||
import app.GraphData.EdgeInfo;
|
||||
|
||||
public class App {
|
||||
|
||||
public static void main(String[] args) {
|
||||
// watch `list.visualize()`
|
||||
LinkedList<String> list = new LinkedList<String>();
|
||||
list.append("1");
|
||||
list.append("2");
|
||||
list.append("3");
|
||||
list.append("4");
|
||||
}
|
||||
}
|
||||
|
||||
class LinkedList<T> {
|
||||
private Node<T> head;
|
||||
|
||||
public void append(T value) {
|
||||
if (head == null) {
|
||||
head = new Node<T>(value);
|
||||
} else {
|
||||
Node<T> m = head;
|
||||
while (m.next != null) {
|
||||
m = m.next;
|
||||
}
|
||||
m.next = new Node<T>(value);
|
||||
}
|
||||
}
|
||||
|
||||
public String visualize() {
|
||||
Node<T> list = new Node<T>(null);
|
||||
list.next = this.head;
|
||||
|
||||
return GraphData.from(list, (item, info) -> {
|
||||
if (item != list) {
|
||||
info.id = item.value.toString();
|
||||
} else {
|
||||
info.label = "list";
|
||||
}
|
||||
if (item.next != null) {
|
||||
EdgeInfo<LinkedList.Node<T>> ei = info.addEdge(item.next);
|
||||
ei.label = "next";
|
||||
}
|
||||
return info;
|
||||
}).toString();
|
||||
}
|
||||
|
||||
static class Node<T> {
|
||||
public T value;
|
||||
public Node<T> next;
|
||||
|
||||
public Node(T value) {
|
||||
this.value = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
31
demos/java/src/app/ExtractedData.java
Normal file
31
demos/java/src/app/ExtractedData.java
Normal file
@@ -0,0 +1,31 @@
|
||||
package app;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
public abstract class ExtractedData {
|
||||
public HashMap<String, Boolean> getKind() {
|
||||
HashMap<String, Boolean> m = new HashMap<>();
|
||||
for (String s : getTags()) {
|
||||
m.put(s, true);
|
||||
}
|
||||
return m;
|
||||
}
|
||||
|
||||
@JsonIgnore
|
||||
protected abstract String[] getTags();
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
try {
|
||||
String val = objectMapper.writeValueAsString(this);
|
||||
return val;
|
||||
} catch (JsonProcessingException e) {
|
||||
return "JsonProcessingException";
|
||||
}
|
||||
}
|
||||
}
|
||||
217
demos/java/src/app/GraphData.java
Normal file
217
demos/java/src/app/GraphData.java
Normal file
@@ -0,0 +1,217 @@
|
||||
package app;
|
||||
|
||||
import java.util.ArrayDeque;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||
import com.fasterxml.jackson.annotation.JsonInclude.Include;
|
||||
|
||||
public class GraphData extends ExtractedData {
|
||||
private List<NodeData> nodes = new ArrayList<>();
|
||||
private List<EdgeData> edges = new ArrayList<>();
|
||||
|
||||
@Override
|
||||
protected String[] getTags() {
|
||||
return new String[] { "graph" };
|
||||
}
|
||||
|
||||
public List<NodeData> getNodes() {
|
||||
return this.nodes;
|
||||
}
|
||||
|
||||
public List<EdgeData> getEdges() {
|
||||
return this.edges;
|
||||
}
|
||||
|
||||
@JsonInclude(Include.NON_NULL)
|
||||
public static class NodeData {
|
||||
private String id;
|
||||
private String label; // can be null
|
||||
private String color; // can be null
|
||||
private String shape; // can be null
|
||||
|
||||
public NodeData(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getId() {
|
||||
return this.id;
|
||||
}
|
||||
|
||||
public void setId(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getLabel() {
|
||||
return this.label;
|
||||
}
|
||||
|
||||
public void setLabel(String label) {
|
||||
this.label = label;
|
||||
}
|
||||
|
||||
public String getColor() {
|
||||
return this.color;
|
||||
}
|
||||
|
||||
public void setColor(String color) {
|
||||
this.color = color;
|
||||
}
|
||||
|
||||
public String getShape() {
|
||||
return this.shape;
|
||||
}
|
||||
|
||||
public void setShape(String shape) {
|
||||
this.shape = shape;
|
||||
}
|
||||
}
|
||||
|
||||
@JsonInclude(Include.NON_NULL)
|
||||
public static class EdgeData {
|
||||
private String from;
|
||||
private String to;
|
||||
private String label; // Can be null
|
||||
private String id; // Can be null
|
||||
private String color; // Can be null
|
||||
private Boolean dashes; // Can be null
|
||||
|
||||
public EdgeData(String from, String to) {
|
||||
this.from = from;
|
||||
this.to = to;
|
||||
}
|
||||
|
||||
public String getFrom() {
|
||||
return this.from;
|
||||
}
|
||||
|
||||
public void setFrom(String from) {
|
||||
this.from = from;
|
||||
}
|
||||
|
||||
public String getTo() {
|
||||
return this.to;
|
||||
}
|
||||
|
||||
public void setTo(String to) {
|
||||
this.to = to;
|
||||
}
|
||||
|
||||
public String getLabel() {
|
||||
return this.label;
|
||||
}
|
||||
|
||||
public void setLabel(String label) {
|
||||
this.label = label;
|
||||
}
|
||||
|
||||
public String getId() {
|
||||
return this.id;
|
||||
}
|
||||
|
||||
public void setId(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getColor() {
|
||||
return this.color;
|
||||
}
|
||||
|
||||
public void setColor(String color) {
|
||||
this.color = color;
|
||||
}
|
||||
|
||||
public Boolean getDashes() {
|
||||
return this.dashes;
|
||||
}
|
||||
|
||||
public void setDashes(Boolean dashes) {
|
||||
this.dashes = dashes;
|
||||
}
|
||||
}
|
||||
|
||||
public interface NodeInfoProvider<T> {
|
||||
public NodeInfo<T> getNodeInfo(T item, NodeInfo<T> nodeInfo);
|
||||
}
|
||||
|
||||
public static <T> GraphData from(T item, NodeInfoProvider<T> f) {
|
||||
GraphData d = new GraphData();
|
||||
ArrayDeque<T> q = new ArrayDeque<>();
|
||||
q.add(item);
|
||||
|
||||
FromState<T> s = new FromState<>(f);
|
||||
|
||||
while (q.size() > 0) {
|
||||
NodeInfo<T> nodeInfo = s.getNodeInfo(q.removeFirst());
|
||||
NodeData nd = new NodeData(s.getId(nodeInfo));
|
||||
d.nodes.add(nd);
|
||||
|
||||
nd.setLabel(nodeInfo.label);
|
||||
nd.setColor(nodeInfo.color);
|
||||
|
||||
for (EdgeInfo<T> e : nodeInfo.edges) {
|
||||
EdgeData ed = new EdgeData(nd.id, s.getId(s.getNodeInfo(e.to)));
|
||||
d.edges.add(ed);
|
||||
ed.setLabel(e.label);
|
||||
ed.setId(e.id);
|
||||
|
||||
q.add(e.to);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return d;
|
||||
}
|
||||
|
||||
static class FromState<T> {
|
||||
private int i = 0;
|
||||
private NodeInfoProvider<T> provider;
|
||||
private HashMap<T, NodeInfo<T>> infos = new HashMap<>();
|
||||
|
||||
public FromState(NodeInfoProvider<T> provider) {
|
||||
this.provider = provider;
|
||||
}
|
||||
|
||||
public String getId(NodeInfo<T> nodeInfo) {
|
||||
if (nodeInfo.id == null) {
|
||||
nodeInfo.id = "hediet.de/" + (i++);
|
||||
}
|
||||
return nodeInfo.id;
|
||||
}
|
||||
|
||||
public NodeInfo<T> getNodeInfo(T item) {
|
||||
if (infos.containsKey(item)) {
|
||||
return infos.get(item);
|
||||
}
|
||||
|
||||
NodeInfo<T> info = this.provider.getNodeInfo(item, new NodeInfo<>());
|
||||
infos.put(item, info);
|
||||
return info;
|
||||
}
|
||||
}
|
||||
|
||||
public static class NodeInfo<T> {
|
||||
public ArrayList<EdgeInfo<T>> edges = new ArrayList<EdgeInfo<T>>();
|
||||
public String label; // Can be null.
|
||||
public String id; // Can be null.
|
||||
public String color; // Can be null.
|
||||
|
||||
public EdgeInfo<T> addEdge(T to) {
|
||||
EdgeInfo<T> i = new EdgeInfo<>(to);
|
||||
this.edges.add(i);
|
||||
return i;
|
||||
}
|
||||
}
|
||||
|
||||
public static class EdgeInfo<T> {
|
||||
public T to;
|
||||
public String label; // Can be null.
|
||||
public String id; // Can be null.
|
||||
|
||||
public EdgeInfo(T to) {
|
||||
this.to = to;
|
||||
}
|
||||
}
|
||||
}
|
||||
18
demos/java/src/app/TextData.java
Normal file
18
demos/java/src/app/TextData.java
Normal file
@@ -0,0 +1,18 @@
|
||||
package app;
|
||||
|
||||
public class TextData extends ExtractedData {
|
||||
private final String textValue;
|
||||
|
||||
public TextData(String text) {
|
||||
this.textValue = text;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String[] getTags() {
|
||||
return new String[] { "text" };
|
||||
}
|
||||
|
||||
public String getText() {
|
||||
return this.textValue;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user