forked from reposense/RepoSense
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommitHash.java
More file actions
89 lines (74 loc) · 2.65 KB
/
Copy pathCommitHash.java
File metadata and controls
89 lines (74 loc) · 2.65 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
package reposense.model;
import java.util.List;
import java.util.stream.Collectors;
/**
* Represents a git commit hash in {@code RepoConfiguration}.
*/
public class CommitHash {
private static final String COMMIT_HASH_REGEX = "^[0-9a-f]+$";
private static final String INVALID_COMMIT_HASH_MESSAGE =
"The provided commit hash, %s, contains illegal characters.";
private String commit;
public CommitHash(String commit) {
validateCommit(commit);
this.commit = commit;
}
@Override
public String toString() {
return commit;
}
@Override
public boolean equals(Object other) {
// short circuit if same object
if (this == other) {
return true;
}
// instanceof handles null
if (!(other instanceof CommitHash)) {
return false;
}
CommitHash otherCommit = (CommitHash) other;
return this.commit.equals(otherCommit.commit);
}
@Override
public int hashCode() {
return commit.hashCode();
}
/**
* Converts all the strings in {@code commits} into {@code CommitHash} objects.
* Returns null if {@code commits} is null.
* @throws IllegalArgumentException if any of the strings are in invalid formats.
*/
public static List<CommitHash> convertStringsToCommits(List<String> commits) throws IllegalArgumentException {
if (commits == null) {
return null;
}
return commits.stream()
.map(CommitHash::new)
.collect(Collectors.toList());
}
/**
* Checks if {@code commitList} contains {@code commitHash}
*/
public static boolean isInsideCommitList(String commitHash, List<CommitHash> commitList) {
return commitList.stream().map(CommitHash::toString).anyMatch(commitHash::startsWith);
}
/**
* Checks that all the strings in the {@code ignoreCommitList} are in valid formats.
* @throws IllegalArgumentException if any of the values do not meet the criteria.
*/
public static void validateCommits(List<String> commits) throws IllegalArgumentException {
for (String commitHash : commits) {
validateCommit(commitHash);
}
}
/**
* Checks that {@code commitHash} is in a valid format.
* @throws IllegalArgumentException if {@code commitHash} does not meet the criteria.
*/
private static void validateCommit(String commitHash) throws IllegalArgumentException {
if (!commitHash.matches(COMMIT_HASH_REGEX)) {
throw new IllegalArgumentException(String.format(INVALID_COMMIT_HASH_MESSAGE, commitHash));
}
}
}