Skip to content

Commit 6ce968f

Browse files
committed
add random util
1 parent 6c741bc commit 6ce968f

File tree

4 files changed

+101
-42
lines changed

4 files changed

+101
-42
lines changed

lib/utils/DiffUtil.java

Lines changed: 43 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -5,48 +5,50 @@
55

66
public class DiffUtil {
77

8-
public static int getChangedLine(String diffFilePath) {
9-
try {
10-
List<String> diffFileStrList = FileUtil.readFileToStrList(diffFilePath);
11-
for (String line : diffFileStrList) {
12-
if (line.startsWith("@@")) {
13-
for (String tmp : line.split(" ")) {
14-
if (tmp.trim().startsWith("-")) {
15-
if (tmp.contains(",")) {
16-
return Integer.valueOf(tmp.trim().replace("-", "").split(",")[0]);
17-
} else {
18-
return Integer.valueOf(tmp.trim().replace("-", ""));
19-
}
20-
}
21-
}
22-
}
23-
}
24-
} catch (Exception e) {
25-
// TODO: handle exception
26-
System.out.println(diffFilePath);
27-
System.exit(0);
28-
}
29-
return -1;
30-
}
8+
public static List<Integer> getChangedLineNumList(String diffFilePath) {
319

32-
public static List<String> getModifiedFileList(String diffFilePath) {
33-
/**
34-
* can to be further improved in future
35-
*/
36-
List<String> modifiedFileRelPathList = new ArrayList<String>();
37-
for (String line : FileUtil.readFileToStrList(diffFilePath)) {
38-
if (line.startsWith("diff --git ")) {
39-
// Heuristic
40-
String fileRelPath = line.replace("diff --git ", "").split(" ")[0].replaceFirst("a/", "").trim();
41-
modifiedFileRelPathList.add(fileRelPath);
42-
}
43-
}
44-
return modifiedFileRelPathList;
45-
}
10+
List<Integer> changedLineNumList = new ArrayList<>();
11+
try {
12+
List<String> diffFileStrList = FileUtil.readFileToStrList(diffFilePath);
13+
for (String line : diffFileStrList) {
14+
if (line.startsWith("@@")) {
15+
for (String tmp : line.split(" ")) {
16+
if (tmp.trim().startsWith("-")) {
17+
if (tmp.contains(",")) {
18+
changedLineNumList.add(Integer.valueOf(tmp.trim().replace("-", "").split(",")[0]));
19+
} else {
20+
changedLineNumList.add(Integer.valueOf(tmp.trim().replace("-", "")));
21+
}
22+
}
23+
}
24+
}
25+
}
26+
} catch (Exception e) {
27+
// TODO: handle exception
28+
System.out.println(diffFilePath);
29+
System.exit(0);
30+
}
31+
return changedLineNumList;
32+
}
4633

47-
public static void main(String[] args) {
48-
String diffFilePath = "/media/sf__3_fix_groups/RCN_REDUNDANT_NULLCHECK_OF_NONNULL_VALUE/delete redundent null check/27769/diff.txt";
49-
getModifiedFileList(diffFilePath);
50-
}
34+
public static List<String> getModifiedFileList(String diffFilePath) {
35+
/**
36+
* can to be further improved in future
37+
*/
38+
List<String> modifiedFileRelPathList = new ArrayList<String>();
39+
for (String line : FileUtil.readFileToStrList(diffFilePath)) {
40+
if (line.startsWith("diff --git ")) {
41+
// Heuristic
42+
String fileRelPath = line.replace("diff --git ", "").split(" ")[0].replaceFirst("a/", "").trim();
43+
modifiedFileRelPathList.add(fileRelPath);
44+
}
45+
}
46+
return modifiedFileRelPathList;
47+
}
48+
49+
public static void main(String[] args) {
50+
String diffFilePath = "/media/sf__3_fix_groups/RCN_REDUNDANT_NULLCHECK_OF_NONNULL_VALUE/delete redundent null check/27769/diff.txt";
51+
getModifiedFileList(diffFilePath);
52+
}
5153

5254
}

lib/utils/FileUtil.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,20 @@
2121

2222
public class FileUtil {
2323

24+
public static String getFileContent(String filePath) throws IOException {
25+
26+
BufferedReader br=new BufferedReader(new FileReader(filePath));
27+
StringBuilder sb=new StringBuilder();
28+
String line=br.readLine();
29+
while(line!=null){
30+
sb.append(line);
31+
sb.append(System.lineSeparator());
32+
line=br.readLine();
33+
}
34+
return sb.toString();
35+
36+
}
37+
2438
public static String[] getSubDirs(String rootDir) {
2539

2640
File file = new File(rootDir);

lib/utils/JSONUtil.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
public class JSONUtil {
1111

1212
public static JSONObject parseJSONFromFile(String jsonFpath) {
13-
FileReader reader = null;
13+
FileReader reader;
1414
JSONObject object = null;
1515
try {
1616
reader = new FileReader(jsonFpath);

lib/utils/RandomUtil.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package javaToolkit.lib.utils;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.List;
6+
import java.util.Random;
7+
8+
public class RandomUtil {
9+
10+
public Object getARandomElement(List<?> givenList) {
11+
Random rand = new Random();
12+
Object randomElement = givenList.get(rand.nextInt(givenList.size()));
13+
return randomElement;
14+
}
15+
16+
17+
public List<Object> randomSelectWithRepeat(List<Object> givenList, int numberOfElements) {
18+
Random rand = new Random();
19+
List<Object> selectedEles = new ArrayList<>();
20+
21+
for (int i = 0; i < numberOfElements; i++) {
22+
int randomIndex = rand.nextInt(givenList.size());
23+
Object randomElement = givenList.get(randomIndex);
24+
selectedEles.add(randomElement);
25+
}
26+
27+
return selectedEles;
28+
}
29+
30+
public List<?> randomSelectWithoutRepeat(List<?> givenList, int numberOfElements) {
31+
Random rand = new Random();
32+
List<Object> selectedEles = new ArrayList<>();
33+
34+
for (int i = 0; i < numberOfElements; i++) {
35+
int randomIndex = rand.nextInt(givenList.size());
36+
Object randomElement = givenList.get(randomIndex);
37+
selectedEles.add(randomElement);
38+
givenList.remove(randomIndex);
39+
}
40+
41+
return selectedEles;
42+
}
43+
}

0 commit comments

Comments
 (0)