Repository navigation
Expand file tree
/
Copy pathTextReader.java
More file actions
85 lines (74 loc) · 2.72 KB
/
Copy pathTextReader.java
File metadata and controls
85 lines (74 loc) · 2.72 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
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.HashMap;
public class TextReader {
private String fileLocation;
private ArrayList<Node> nodes;
private HashMap<Integer, ArrayList<Integer>> rawData;
private HashMap<Integer, Integer> idToIndex;
public TextReader(String fileLocation) {
this.idToIndex = new HashMap<>();
this.fileLocation = fileLocation;
this.nodes = new ArrayList<>();
this.rawData = new HashMap<>();
}
public void readNodes() throws IOException {
BufferedReader bufRead = Files.newBufferedReader(Paths.get(fileLocation));
String myLine;
Boolean start = false;
while ((myLine = bufRead.readLine()) != null) {
if (myLine.equals("]nodes")) {
break;
}
if (start) {
String[] value = myLine.split("\t");
nodes.add(new Node(Integer.valueOf(value[0]), value[1]));
idToIndex.put(Integer.valueOf(value[0]), nodes.size() - 1);
}
if (myLine.equals("nodes[")) {
start = true;
}
}
}
public void readEdges() throws IOException {
FileReader input = new FileReader(new File(fileLocation));
BufferedReader bufRead = new BufferedReader(input);
String myLine;
Boolean start = false;
while ((myLine = bufRead.readLine()) != null) {
if (myLine.equals("]edges")) {
break;
}
if (start) {
String[] value = myLine.split("\t");
if (idToIndex.containsKey(Integer.parseInt(value[0])) && idToIndex.containsKey(Integer.parseInt(value[1]))){
if (rawData.containsKey(Integer.parseInt(value[0]))) {
ArrayList<Integer> linksTo = rawData.get(Integer.parseInt(value[0]));
linksTo.add(Integer.parseInt(value[1]));
} else {
ArrayList<Integer> linksTo = new ArrayList<>();
linksTo.add(Integer.parseInt(value[1]));
rawData.put(Integer.valueOf(value[0]), linksTo);
}
}
}
if (myLine.equals("edges[")) {
start = true;
}
}
}
public ArrayList<Node> getCompleteNodes() {
for (Node node : nodes) {
if (rawData.containsKey(node.id)) {
node.linksTo = (rawData.get(node.id));
}
}
return nodes;
}
public HashMap<Integer, Integer> getIdToIndex() {
return idToIndex;
}
}