forked from YoussefAhmed256/gitlet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBranchStore.java
More file actions
45 lines (37 loc) · 1.2 KB
/
BranchStore.java
File metadata and controls
45 lines (37 loc) · 1.2 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
package gitlet;
import java.io.File;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import static gitlet.Utils.*;
public class BranchStore {
private final File BRANCH_DIR;
public BranchStore(File branchDirectory) {
this.BRANCH_DIR = branchDirectory;
}
public Branch getBranch(String branchName) {
File branchFile = join(BRANCH_DIR , branchName);
if (branchFile.exists()) {
return readObject(branchFile , Branch.class);
}
return null;
}
public void saveBranch(Branch branch) {
File branchFile = join(BRANCH_DIR , branch.getName());
writeObject(branchFile , branch);
}
public void removeBranch(Branch branch) {
File branchFile = join(BRANCH_DIR , branch.getName());
if (branchFile.exists()) {
branchFile.delete();
}
}
public boolean branchExists(String branchName) {
return (getBranch(branchName) != null);
}
public List<Branch> getBranches() {
return Objects.requireNonNull(plainFilenamesIn(BRANCH_DIR)).stream()
.map(this::getBranch)
.collect(Collectors.toList());
}
}