Skip to content

Commit 078da0d

Browse files
committed
2 parents 4209b3d + c0bf743 commit 078da0d

22 files changed

+146
-140
lines changed

Diff for: .gitignore_java

100644100755
File mode changed.

Diff for: README.md

100644100755
File mode changed.

Diff for: dependency/com.fasterxml.jackson.databind-2.10.0.LIFERAY-PATCHED-1.jar

100644100755
File mode changed.

Diff for: dependency/commons-io-2.6.jar

100644100755
File mode changed.

Diff for: dependency/gson-2.8.6.jar

100644100755
File mode changed.

Diff for: dependency/jackson-core-2.10.3.jar

100644100755
File mode changed.

Diff for: dependency/javax.annotation-api-1.3.2.jar

100644100755
File mode changed.

Diff for: dependency/json-simple-1.1.1.jar

100644100755
File mode changed.

Diff for: lib/utils/CSVUtil.java

100644100755
File mode changed.

Diff for: lib/utils/CollectionUtil.java

100644100755
File mode changed.

Diff for: lib/utils/DiffUtil.java

100644100755
+10-14
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
package javaToolkit.lib.utils;
22

33
import java.nio.file.Path;
4-
import java.nio.file.Paths;
54
import java.util.ArrayList;
5+
import java.util.HashMap;
66
import java.util.List;
7+
import java.util.Map;
78

89
public class DiffUtil {
910

@@ -84,25 +85,20 @@ public static List<Integer> getChangedLineNumListInNewVersion(String diffStr) {
8485
return changedLineNumList;
8586
}
8687

87-
public static List<String> getModifiedFileList(String diffFilePathString) {
88-
/**
89-
* can to be further improved in future
90-
*/
91-
Path diffFilePath = Paths.get(diffFilePathString);
92-
List<String> modifiedFileRelPathList = new ArrayList<String>();
88+
public static List<Map<String, String>> getModifiedFileList(Path diffFilePath) {
89+
List<Map<String, String>> modifiedFileRelPathList = new ArrayList<Map<String, String>>();
9390
for (String line : FileUtil.readFileToLineList(diffFilePath)) {
9491
if (line.startsWith("diff --git ")) {
9592
// Heuristic
96-
String fileRelPath = line.replace("diff --git ", "").split(" ")[0].replaceFirst("a/", "").trim();
97-
modifiedFileRelPathList.add(fileRelPath);
93+
String oldFileRelPath = line.replace("diff --git ", "").split(" ")[0].replaceFirst("a/", "").trim();
94+
String newFileRelPath = line.replace("diff --git ", "").split(" ")[1].replaceFirst("b/", "").trim();
95+
Map<String, String> oldNnewMap = new HashMap<String, String>();
96+
oldNnewMap.put("old", oldFileRelPath);
97+
oldNnewMap.put("new", newFileRelPath);
98+
modifiedFileRelPathList.add(oldNnewMap);
9899
}
99100
}
100101
return modifiedFileRelPathList;
101102
}
102103

103-
public static void main(String[] args) {
104-
String diffFilePath = "/media/sf__3_fix_groups/RCN_REDUNDANT_NULLCHECK_OF_NONNULL_VALUE/delete redundent null check/27769/diff.txt";
105-
getModifiedFileList(diffFilePath);
106-
}
107-
108104
}

Diff for: lib/utils/EclipseUtil.java

100644100755
File mode changed.

Diff for: lib/utils/FileUtil.java

100644100755
-32
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@
22

33
import java.io.BufferedReader;
44
import java.io.File;
5-
import java.io.FileInputStream;
65
import java.io.FileNotFoundException;
76
import java.io.FileReader;
87
import java.io.FilenameFilter;
98
import java.io.IOException;
10-
import java.io.InputStreamReader;
119
import java.nio.file.Files;
1210
import java.nio.file.Path;
1311
import java.nio.file.Paths;
@@ -173,36 +171,6 @@ public static void copyDirectory(Path srcDirPath, Path destDirPath) {
173171
}
174172
}
175173

176-
public static String readStringFromFile(String filePath) {
177-
try {
178-
File file = new File(filePath);
179-
if (file.isFile() && file.exists()) {
180-
InputStreamReader read = new InputStreamReader(new FileInputStream(file));
181-
BufferedReader bufferedReader = new BufferedReader(read);
182-
StringBuilder stringBuilder = new StringBuilder();
183-
String line = null;
184-
String ls = System.getProperty("line.separator");
185-
while ((line = bufferedReader.readLine()) != null) {
186-
stringBuilder.append(line);
187-
stringBuilder.append(ls);
188-
}
189-
190-
// delete the last new line separator
191-
stringBuilder.deleteCharAt(stringBuilder.length() - 1);
192-
bufferedReader.close();
193-
194-
String content = stringBuilder.toString();
195-
return content;
196-
} else {
197-
System.out.printf("File not found! %s\n", filePath);
198-
}
199-
} catch (Exception e) {
200-
System.out.println("Read file error!");
201-
e.printStackTrace();
202-
}
203-
return null;
204-
}
205-
206174
/**
207175
* there is no \newline at the end of each line
208176
*

Diff for: lib/utils/GitUtil.java

100644100755
File mode changed.

Diff for: lib/utils/JSONUtil.java

100644100755
+32-34
Original file line numberDiff line numberDiff line change
@@ -13,39 +13,37 @@
1313

1414
public class JSONUtil {
1515

16-
public static JSONObject parseJSONFromFile(String jsonFpath) {
17-
FileReader reader;
18-
JSONObject object = null;
19-
try {
20-
reader = new FileReader(jsonFpath);
21-
JSONParser jsonParser = new JSONParser();
22-
23-
object = (JSONObject) jsonParser.parse(reader);
24-
} catch (IOException | ParseException e) {
25-
// TODO Auto-generated catch block
26-
e.printStackTrace();
27-
}
28-
29-
return object;
30-
31-
}
32-
33-
34-
public static void writeMapAsJson(Map<String, Integer> map, String jsonFilePath) {
35-
Map<String, String> newMap = new HashMap<String, String>();
36-
for (String key : map.keySet()) {
37-
newMap.put(key, String.valueOf(map.get(key)));
38-
}
39-
40-
ObjectMapper mapper = new ObjectMapper();
41-
String json = null;
42-
try {
43-
json = mapper.writeValueAsString(newMap);
44-
} catch (JsonProcessingException e) {
45-
e.printStackTrace();
46-
}
47-
FileUtil.writeStr2File(json, jsonFilePath);
48-
}
49-
16+
public static JSONObject parseJSONFromFile(String jsonFpath) {
17+
FileReader reader;
18+
JSONObject object = null;
19+
try {
20+
reader = new FileReader(jsonFpath);
21+
JSONParser jsonParser = new JSONParser();
22+
23+
object = (JSONObject) jsonParser.parse(reader);
24+
} catch (IOException | ParseException e) {
25+
// TODO Auto-generated catch block
26+
e.printStackTrace();
27+
}
28+
29+
return object;
30+
31+
}
32+
33+
public static void writeMapAsJson(Map<String, Integer> map, String jsonFilePath) {
34+
Map<String, String> newMap = new HashMap<String, String>();
35+
for (String key : map.keySet()) {
36+
newMap.put(key, String.valueOf(map.get(key)));
37+
}
38+
39+
ObjectMapper mapper = new ObjectMapper();
40+
String json = null;
41+
try {
42+
json = mapper.writeValueAsString(newMap);
43+
} catch (JsonProcessingException e) {
44+
e.printStackTrace();
45+
}
46+
FileUtil.writeStr2File(json, jsonFilePath);
47+
}
5048

5149
}

Diff for: lib/utils/MathUtil.java

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package javaToolkit.lib.utils;
2+
3+
import java.math.BigInteger;
4+
5+
public class MathUtil {
6+
7+
public static BigInteger binomial(int N, int K) {
8+
BigInteger ret = BigInteger.ONE;
9+
for (int k = 0; k < K; k++) {
10+
ret = ret.multiply(BigInteger.valueOf(N - k)).divide(BigInteger.valueOf(k + 1));
11+
}
12+
return ret;
13+
}
14+
15+
}

Diff for: lib/utils/ObjectUtil.java

100644100755
File mode changed.

Diff for: lib/utils/ProcessUtil.java

100644100755
File mode changed.

Diff for: lib/utils/RandomUtil.java

100644100755
+81-26
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,97 @@
11
package javaToolkit.lib.utils;
22

33
import java.util.ArrayList;
4+
import java.util.HashSet;
45
import java.util.List;
56
import java.util.Random;
7+
import java.util.Set;
68

79
public class RandomUtil {
810

9-
public Object getARandomElement(List<?> givenList) {
10-
Random rand = new Random();
11-
Object randomElement = givenList.get(rand.nextInt(givenList.size()));
12-
return randomElement;
13-
}
11+
public Object getARandomElement(List<?> givenList) {
12+
Random rand = new Random();
13+
Object randomElement = givenList.get(rand.nextInt(givenList.size()));
14+
return randomElement;
15+
}
1416

17+
public List<Object> randomSelectWithRepeat(List<Object> givenList, int numberOfElements) {
18+
Random rand = new Random();
19+
List<Object> selectedEles = new ArrayList<>();
1520

16-
public List<Object> randomSelectWithRepeat(List<Object> givenList, int numberOfElements) {
17-
Random rand = new Random();
18-
List<Object> selectedEles = new ArrayList<>();
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+
}
1926

20-
for (int i = 0; i < numberOfElements; i++) {
21-
int randomIndex = rand.nextInt(givenList.size());
22-
Object randomElement = givenList.get(randomIndex);
23-
selectedEles.add(randomElement);
24-
}
27+
return selectedEles;
28+
}
2529

26-
return selectedEles;
27-
}
30+
public List<?> randomSelectWithoutRepeat(List<?> givenList, int numberOfElements) {
31+
Random rand = new Random();
32+
List<Object> selectedEles = new ArrayList<>();
2833

29-
public List<?> randomSelectWithoutRepeat(List<?> givenList, int numberOfElements) {
30-
Random rand = new Random();
31-
List<Object> selectedEles = new ArrayList<>();
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+
}
3240

33-
for (int i = 0; i < numberOfElements; i++) {
34-
int randomIndex = rand.nextInt(givenList.size());
35-
Object randomElement = givenList.get(randomIndex);
36-
selectedEles.add(randomElement);
37-
givenList.remove(randomIndex);
38-
}
41+
return selectedEles;
42+
}
43+
44+
/**
45+
* include max and min
46+
*
47+
* @param min
48+
* @param max
49+
* @param k
50+
* @return
51+
*/
52+
public static Set<Integer> randomGenerateKDistinctNumbers(int min, int max, int k) {
53+
Random rand = new Random();
54+
Set<Integer> selectedEles = new HashSet<>();
55+
56+
if (max - min < k) {
57+
System.out.printf("Cannot generate %s between %s and %s\n", k, min, max);
58+
System.exit(0);
59+
}
60+
61+
while (selectedEles.size() < k) {
62+
int randomNum = rand.nextInt((max - min) + 1) + min;
63+
selectedEles.add(randomNum);
64+
}
65+
66+
return selectedEles;
67+
}
68+
69+
/**
70+
* include max and min, not contain
71+
*
72+
* @param min
73+
* @param max
74+
* @param k
75+
* @return
76+
* @throws Exception
77+
*/
78+
public static Set<Integer> randomKDistinctNumsWithoutSpecificNum(int min, int max, int k, int withoutNum) throws Exception {
79+
Random rand = new Random();
80+
Set<Integer> selectedEles = new HashSet<>();
81+
82+
if (max - min < k) {
83+
System.out.printf("Cannot generate %s between %s and %s\n", k, min, max);
84+
throw new Exception("Cannot generate " + k + " between " + min + " and " + max + "\n");
85+
}
86+
87+
while (selectedEles.size() < k) {
88+
int randomNum = rand.nextInt((max - min) + 1) + min;
89+
if (randomNum != withoutNum) {
90+
selectedEles.add(randomNum);
91+
}
92+
}
93+
94+
return selectedEles;
95+
}
3996

40-
return selectedEles;
41-
}
4297
}

Diff for: lib/utils/SRCUtil.java

100644100755
+8-34
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,30 @@
11
package javaToolkit.lib.utils;
22

33
import java.nio.file.Path;
4-
import java.nio.file.Paths;
54
import java.util.List;
65

76
public class SRCUtil {
87

9-
public static void changePackage(String oriSRCPath, String dstSRCPath, String newPackName) {
10-
List<String> srcStrList = FileUtil.readFileToLineList(Paths.get(oriSRCPath));
8+
public static String changePackage(Path SRCPath, String newPackName) {
9+
List<String> srcStrList = FileUtil.readFileToLineList(SRCPath);
1110

1211
Boolean ifchanged = false;
13-
String wrtStr = "package " + newPackName + ";";
12+
String wrtStr = "";
1413
for (int i = 0; i < srcStrList.size(); i++) {
1514
String line = srcStrList.get(i);
1615
if (line.trim().startsWith("package ") && !ifchanged) {
16+
wrtStr += ("package " + newPackName + ";\n");
1717
ifchanged = true;
1818
} else {
1919
wrtStr += (line + "\n");
2020
}
2121
}
2222

23-
FileUtil.writeStr2File(wrtStr, dstSRCPath);
24-
25-
}
26-
27-
public static void changePackage(Path srcPath, String newPackName) {
28-
List<String> srcStrList = FileUtil.readFileToLineList(srcPath);
29-
30-
Boolean ifchanged = false;
31-
String wrtStr = "";
32-
for (int i = 0; i < srcStrList.size(); i++) {
33-
String line = srcStrList.get(i);
34-
if (line.trim().startsWith("package ") && !ifchanged) {
35-
line = "package " + newPackName + ";";
36-
ifchanged = true;
37-
}
38-
wrtStr += (line + "\n");
23+
if (!ifchanged) {
24+
wrtStr = "package " + newPackName + ";\n" + wrtStr;
3925
}
40-
41-
FileUtil.writeStr2File(wrtStr, srcPath);
42-
43-
}
44-
45-
public static void main(String[] args) {
46-
/*
47-
* test purpose
48-
*/
49-
String oriPathString = "/home/appevolve/Desktop/2019_ISSTA_AE_225/run/Update_Example_Analysis+API-Usage_Update/EPR-Data/RepositoryWriter.java";
50-
String dstPathString = "/home/appevolve/Desktop/2019_ISSTA_AE_225/run/Update_Example_Analysis+API-Usage_Update/EPR-Data/RepositoryWriter_tmp.java";
51-
String newPackString = "main";
52-
changePackage(oriPathString, dstPathString, newPackString);
53-
System.out.println("Finished!");
26+
27+
return wrtStr.trim();
5428

5529
}
5630

Diff for: lib/utils/TimeUtil.java

100644100755
File mode changed.

Diff for: lib/utils/XMLUtil.java

100644100755
File mode changed.

0 commit comments

Comments
 (0)