-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathGraphSaveObject.java
More file actions
executable file
·98 lines (84 loc) · 3.03 KB
/
GraphSaveObject.java
File metadata and controls
executable file
·98 lines (84 loc) · 3.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package graphtea.extensions.io;
import graphtea.platform.core.exception.ExceptionHandler;
import graphtea.graph.graph.Edge;
import graphtea.graph.graph.GraphModel;
import graphtea.graph.graph.Vertex;
import java.io.*;
import java.util.ArrayList;
import java.util.Base64;
/**
* Created by rostam on 18.12.15.
*
*/
public class GraphSaveObject implements Serializable {
@Override
public boolean equals(Object obj) {
if (obj instanceof GraphSaveObject){
GraphSaveObject t = (GraphSaveObject) obj;
return this.vs.equals(t.vs) && this.es.equals(t.es);
} else return false;
}
public ArrayList<VertexSaveObject> vs = new ArrayList<>();
ArrayList<EdgeSaveObject> es = new ArrayList<>();
boolean directed = false;
String label = "";
public GraphSaveObject(GraphModel g) {
directed = g.isDirected();
label = g.getLabel();
for(Vertex v: g) vs.add(new VertexSaveObject(v));
for(Edge e : g.edges()) es.add(new EdgeSaveObject(e));
}
public GraphModel getG() {
GraphModel g = new GraphModel();
insertIntoGraph(g);
return g;
}
public void insertIntoGraph(GraphModel g){
for(VertexSaveObject v: vs) {
g.addVertex(v.getVertex());
}
for(EdgeSaveObject e : es) {
e.addEdge(g);
}
g.setDirected(directed);
}
public static byte[] getBytesOfGraph(GraphModel g) {
ByteArrayOutputStream bout = new ByteArrayOutputStream();
try (ObjectOutputStream oop = new ObjectOutputStream(bout)) {
oop.writeObject(new GraphSaveObject(g));
} catch (IOException e) {
ExceptionHandler.catchException(e);
}
return bout.toByteArray();
}
public static byte[] getBytesOfGraphSaveObject(GraphSaveObject gso) {
ByteArrayOutputStream bout = new ByteArrayOutputStream();
try (ObjectOutputStream oop = new ObjectOutputStream(bout)) {
oop.writeObject(gso);
} catch (IOException e) {
ExceptionHandler.catchException(e);
}
return bout.toByteArray();
}
public static String graph2String(GraphModel g){
return Base64.getMimeEncoder().encodeToString(getBytesOfGraph(g));
}
public static GraphModel String2Graph(String s){
return getGraphFromBytes(Base64.getMimeDecoder().decode(s));
}
public static GraphSaveObject string2GraphSaveObject(String s){
return getGraphSaveOobjectfromBytes(Base64.getMimeDecoder().decode(s));
}
public static GraphSaveObject getGraphSaveOobjectfromBytes(byte[] b){
try (ObjectInputStream ois = new ObjectInputStream(
new ByteArrayInputStream(b))) {
return (GraphSaveObject) ois.readObject();
} catch (IOException | ClassNotFoundException e) {
ExceptionHandler.catchException(e);
}
return null;
}
public static GraphModel getGraphFromBytes(byte[] b) {
return getGraphSaveOobjectfromBytes(b).getG();
}
}