forked from fishsticks89/hw-topics-git-project
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDirectoryFile.java
More file actions
144 lines (125 loc) · 3.84 KB
/
DirectoryFile.java
File metadata and controls
144 lines (125 loc) · 3.84 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.BufferedReader;
import java.util.ArrayList;
import util.Terminate;
import java.io.BufferedWriter;
import java.io.IOException;
class DirectoryLine {
boolean isTree;
String hash;
String name;
DirectoryLine(boolean isTree, String hash, String name) {
this.isTree = isTree;
this.hash = hash;
this.name = name;
}
static DirectoryLine fromString(String line) {
String[] parts = line.split(" ");
return new DirectoryLine(parts[0].equals("tree"), parts[1], parts[2]);
}
boolean equals(DirectoryLine other) {
return this.isTree == other.isTree && this.name.equals(other.name) && this.hash.equals(other.hash);
}
public String toString() {
final var str = (isTree ? "tree " : "blob ") + hash + " " + name;
if (!fromString(str).equals(this))
throw new Error("String encoding failed");
return str;
}
}
public class DirectoryFile {
private File file;
public File getFile() {
return file;
}
DirectoryFile(String path) {
file = new File(path);
if (!file.exists()) {
DirectoryFile.generateDirectoryFile(path, new ArrayList<>());
}
}
public int lineWithHash(String sha) {
var lines = getLines();
for (int i = 0; i < lines.size(); i++) {
if (lines.get(i).hash.equals(sha)) {
return i;
}
}
return -1;
}
public void setLine(int index, DirectoryLine line) {
var lines = getLines();
lines.set(index, line);
generateDirectoryFile(file.getPath(), lines);
}
public int findLine(DirectoryLine line) {
var lines = getLines();
for (int i = 0; i < lines.size(); i++) {
if (lines.get(i).equals(line))
return i;
}
return -1;
}
public void addLine(DirectoryLine line) {
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));
bw.write(line.toString());
bw.newLine();
bw.close();
} catch (Exception e) {
Terminate.exception(e);
}
}
public void addIfNotExist(DirectoryLine line) {
if (findLine(line) == -1)
addLine(line);
}
public int lineWithNameAndType(String name, boolean isBlob) {
var lines = getLines();
for (int i = 0; i < lines.size(); i++) {
if (lines.get(i).name.equals(name) && lines.get(i).isTree == !isBlob) {
return i;
}
}
return -1;
}
static DirectoryFile generateDirectoryFile(String path, ArrayList<DirectoryLine> lines) {
var file = new File(path);
if (file.exists())
file.delete();
try {
file.createNewFile();
} catch (Exception e) {
Terminate.exception(e);
}
DirectoryFile dir = new DirectoryFile(path);
try {
file.createNewFile();
for (var l : lines)
dir.addLine(l);
} catch (IOException e) {
Terminate.exception(e);
}
return dir;
}
public DirectoryLine getLine(int i) {
return getLines().get(i);
}
public ArrayList<DirectoryLine> getLines() {
try {
BufferedReader br = new BufferedReader(new FileReader(file));
ArrayList<DirectoryLine> lines = new ArrayList<DirectoryLine>();
while (br.ready()) {
lines.add(DirectoryLine.fromString(br.readLine()));
}
br.close();
return lines;
} catch (Exception e) {
Terminate.exception(e);
}
Terminate.exception(new Exception("How did we get here"));
return null;
}
}