Skip to content

Commit cdd142e

Browse files
committed
2 parents da8ff52 + 23bce29 commit cdd142e

File tree

6 files changed

+194
-93
lines changed

6 files changed

+194
-93
lines changed

lib/utils/DiffUtil.java

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package javaToolkit.lib.utils;
22

3+
import java.nio.file.Path;
4+
import java.nio.file.Paths;
35
import java.util.ArrayList;
46
import java.util.List;
57

@@ -29,37 +31,40 @@ public static List<Integer> getChangedLineNumListInOldVersion(String diffStr) {
2931
return changedLineNumList;
3032
}
3133

32-
public static Integer getChangedLineCount(String diffStr, String mode) {
34+
/**
35+
* the mode can only be "add" or "delete"
36+
*
37+
* @param diffStr
38+
* @param mode
39+
* @return
40+
*/
41+
public static int getChangedLineCount(String diffStr, String mode) {
3342

3443
int count = 0;
3544
String symbol = null;
36-
try {
37-
if ("delete".equals(mode)) {
38-
symbol = "-";
39-
} else if ("add".equals(mode)) {
40-
symbol = "+";
41-
} else {
42-
throw new Exception("Only have delete or add mode!");
43-
}
44-
for (String line : diffStr.split("\n")) {
45-
if (line.startsWith(symbol) && !line.startsWith("---") && !line.startsWith("+++")) {
45+
if ("delete".equals(mode)) {
46+
symbol = "-";
47+
} else if ("add".equals(mode)) {
48+
symbol = "+";
49+
}
50+
for (String line : diffStr.split("\n")) {
51+
if (line.startsWith(symbol) && !line.startsWith("---") && !line.startsWith("+++")) {
52+
// if the line is comment
53+
if (line.substring(1).trim().startsWith("*") || line.substring(1).trim().startsWith("/*")
54+
|| line.substring(1).trim().startsWith("//")) {
55+
continue;
56+
} else {
4657
count++;
4758
}
4859
}
49-
} catch (Exception e) {
50-
// TODO: handle exception
51-
e.printStackTrace();
52-
return null;
5360
}
5461
return count;
5562
}
5663

57-
public static List<Integer> getChangedLineNumListInNewVersion(String diffFilePath) {
58-
64+
public static List<Integer> getChangedLineNumListInNewVersion(String diffStr) {
5965
List<Integer> changedLineNumList = new ArrayList<>();
6066
try {
61-
List<String> diffFileStrList = FileUtil.readFileToLineList(diffFilePath);
62-
for (String line : diffFileStrList) {
67+
for (String line : diffStr.split("\n")) {
6368
if (line.startsWith("@@")) {
6469
for (String tmp : line.split(" ")) {
6570
if (tmp.trim().startsWith("+")) {
@@ -74,16 +79,16 @@ public static List<Integer> getChangedLineNumListInNewVersion(String diffFilePat
7479
}
7580
} catch (Exception e) {
7681
// TODO: handle exception
77-
System.out.println(diffFilePath);
7882
e.printStackTrace();
7983
}
8084
return changedLineNumList;
8185
}
8286

83-
public static List<String> getModifiedFileList(String diffFilePath) {
87+
public static List<String> getModifiedFileList(String diffFilePathString) {
8488
/**
8589
* can to be further improved in future
8690
*/
91+
Path diffFilePath = Paths.get(diffFilePathString);
8792
List<String> modifiedFileRelPathList = new ArrayList<String>();
8893
for (String line : FileUtil.readFileToLineList(diffFilePath)) {
8994
if (line.startsWith("diff --git ")) {

lib/utils/FileUtil.java

Lines changed: 30 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
package javaToolkit.lib.utils;
22

33
import java.io.BufferedReader;
4-
import java.io.BufferedWriter;
54
import java.io.File;
65
import java.io.FileInputStream;
76
import java.io.FileNotFoundException;
87
import java.io.FileReader;
9-
import java.io.FileWriter;
108
import java.io.FilenameFilter;
119
import java.io.IOException;
1210
import java.io.InputStreamReader;
13-
import java.io.Writer;
1411
import java.nio.file.Files;
1512
import java.nio.file.Path;
1613
import java.nio.file.Paths;
@@ -52,10 +49,10 @@ public boolean accept(File current, String name) {
5249
return directories;
5350
}
5451

55-
public static String readFile2Str(String fpath) {
52+
public static String readFile2Str(Path fpath) {
5653
String content = "";
5754
try {
58-
content = new String(Files.readAllBytes(Paths.get(fpath)));
55+
content = new String(Files.readAllBytes(fpath));
5956
} catch (IOException e) {
6057
System.out.printf("%s not found!", fpath);
6158
e.printStackTrace();
@@ -127,6 +124,22 @@ public static Boolean copyFile2Dir(File f, String DestDir) {
127124
return true;
128125
}
129126

127+
public static Boolean copyFile2Dir(Path fPath, Path DestDir) {
128+
try {
129+
File DestDirFile = DestDir.toFile();
130+
if (!DestDirFile.exists()) {
131+
DestDirFile.mkdirs();
132+
}
133+
Path targetFilePath = Paths.get(DestDirFile.getAbsolutePath(), fPath.toFile().getName());
134+
FileUtils.copyFile(fPath.toFile(), targetFilePath.toFile());
135+
} catch (IOException e) {
136+
137+
e.printStackTrace();
138+
return false;
139+
}
140+
return true;
141+
}
142+
130143
public static Boolean copyFile2File(File srcFile, File dstFile) {
131144
try {
132145
// dstfile will be overwrited if exist
@@ -139,50 +152,27 @@ public static Boolean copyFile2File(File srcFile, File dstFile) {
139152
return true;
140153
}
141154

142-
public static boolean deleteDirectory(String dirPath) {
143-
File directoryToBeDeleted = new File(dirPath);
155+
public static boolean deleteDirectory(Path dirPath) {
156+
File directoryToBeDeleted = dirPath.toFile();
144157
File[] allContents = directoryToBeDeleted.listFiles();
145158
if (allContents != null) {
146159
for (File file : allContents) {
147-
deleteDirectory(file.getAbsolutePath());
160+
deleteDirectory(file.toPath());
148161
}
149162
}
150163
return directoryToBeDeleted.delete();
151164
}
152165

153-
public static void copyFolder(String srcDirStr, String destDirStr) {
154-
File source = new File(srcDirStr);
155-
File dest = new File(destDirStr);
166+
public static void copyDirectory(Path srcDirPath, Path destDirPath) {
167+
File source = srcDirPath.toFile();
168+
File dest = destDirPath.toFile();
156169
try {
157170
FileUtils.copyDirectory(source, dest);
158171
} catch (IOException e) {
159172
e.printStackTrace();
160173
}
161174
}
162175

163-
public static void writeStringToFile(String filePath, String dataStr) {
164-
BufferedWriter bufferedWriter = null;
165-
try {
166-
File file = new File(filePath);
167-
if (!file.exists()) {
168-
file.createNewFile();
169-
}
170-
Writer writer = new FileWriter(file);
171-
bufferedWriter = new BufferedWriter(writer);
172-
bufferedWriter.write(dataStr);
173-
} catch (IOException e) {
174-
e.printStackTrace();
175-
} finally {
176-
try {
177-
if (bufferedWriter != null)
178-
bufferedWriter.close();
179-
} catch (Exception e) {
180-
System.out.println("Write file error!");
181-
e.printStackTrace();
182-
}
183-
}
184-
}
185-
186176
public static String readStringFromFile(String filePath) {
187177
try {
188178
File file = new File(filePath);
@@ -219,14 +209,14 @@ public static String readStringFromFile(String filePath) {
219209
* @param filePath
220210
* @return
221211
*/
222-
public static List<String> readFileToLineList(String filePath) {
212+
public static List<String> readFileToLineList(Path filePath) {
223213
ArrayList<String> strList = new ArrayList<String>();
224214
try {
225-
if (!new File(filePath).exists()) {
215+
if (!filePath.toFile().exists()) {
226216
System.out.println("File not exists! " + filePath);
227217
return null;
228218
}
229-
BufferedReader reader = new BufferedReader(new FileReader(filePath));
219+
BufferedReader reader = new BufferedReader(new FileReader(filePath.toString()));
230220
String line = reader.readLine();
231221
while (line != null) {
232222
strList.add(line);
@@ -247,7 +237,7 @@ public static List<String> readFileToLineList(String filePath) {
247237
public static void createFolder(String dirStr, Boolean deleteIfExist) {
248238
File dir = new File(dirStr);
249239
if (deleteIfExist && dir.exists()) {
250-
deleteDirectory(dir.getAbsolutePath());
240+
deleteDirectory(dir.toPath());
251241
dir.mkdirs();
252242
} else if (!dir.exists()) {
253243
dir.mkdirs();
@@ -264,11 +254,11 @@ public static boolean deleteDirectory(File directoryToBeDeleted) {
264254
return directoryToBeDeleted.delete();
265255
}
266256

267-
public static List<File> findFilePathofSpecifcTypeRecusive(String tarDir, String extension) {
257+
public static List<File> findFilePathofSpecifcTypeRecusive(Path tarDir, String extension) {
268258

269259
List<File> fileList = new ArrayList<File>();
270260
try {
271-
Files.walk(Paths.get(tarDir)).filter(Files::isRegularFile).forEach((f) -> {
261+
Files.walk(tarDir).filter(Files::isRegularFile).forEach((f) -> {
272262
String filepath = f.toString();
273263
if (filepath.endsWith(extension))
274264
// System.out.println(file + " found!");

0 commit comments

Comments
 (0)