forked from android-exchange/Android-Interview
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBackstageWorker.Java
More file actions
236 lines (221 loc) · 8.16 KB
/
BackstageWorker.Java
File metadata and controls
236 lines (221 loc) · 8.16 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
import javafx.util.Pair;
import java.io.*;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class BackstageWorker {
public static void main(String[] args) {
File sourceFile = getSourceFile();
statisticalProgress(sourceFile, null);
}
/**
* Newcomer registration
*
* @param arrayList null
* @param file null
* @param sequence 0
* @return maxSize
*/
private static int newcomerRegistration(ArrayList<File> arrayList, File file, int sequence) {
if (file != null && !file.isDirectory() && -1 != getIndex(file)) {
Integer integer = getIndex(file);
if (integer > sequence) {
sequence = integer;
}
}
if (null == file) {
File sourceFile = getSourceFile();
if (sourceFile == null || !sourceFile.isDirectory()) {
throw new NullPointerException("Source file cannot be empty");
}
ArrayList<File> fileArrayList = new ArrayList<>();
for (File f : sourceFile.listFiles()) {
sequence = newcomerRegistration(fileArrayList, f, sequence);
}
File f = null;
String format = null;
Scanner scanner = new Scanner(System.in);
for (int i = fileArrayList.size() - 1; i >= 0; i--) {
f = fileArrayList.get(i);
String name = f.getName();
if (name.contains("-") && name.indexOf("-") == 0) {
name = name.replace("-", "");
}
format = String.format("-%d-%s", sequence++, name);
System.out.println(name + " rename " + format + " y/n");
String anObject = "";
while (true) {
anObject = scanner.nextLine();
if ("y".equals(anObject) || "n".equals(anObject)) {
break;
}
System.out.println("Unable to recognize input");
}
if ("y".equals(anObject)) {
System.out.println(f.renameTo(new File(f.getParent(), format)));
fileArrayList.remove(i);
}
}
System.out.println("Name format is incorrect:");
for (int j = 0; j < fileArrayList.size(); j++) {
System.out.println(fileArrayList.get(j).getPath());
}
} else if (!file.isDirectory() && -1 == getIndex(file)) {
arrayList.add(file);
} else if (file.isDirectory()) {
File[] files = file.listFiles();
if (files != null) {
for (File f : files) {
sequence = newcomerRegistration(arrayList, f, sequence);
}
}
}
return sequence;
}
/**
* Update directory
*
* @param tagerFile null
* @param stringBuilder null
* @param floor 0
*/
private static void directoryUpdate(File tagerFile, StringBuilder stringBuilder, int floor) {
if (tagerFile == null) {
File file = Paths.get(System.getProperty("user.dir"), "Directory.md").toFile();
createNewFile(file);
File sourceFile = getSourceFile();
File[] files = sourceFile.listFiles();
if (files != null) {
stringBuilder = new StringBuilder();
floor++;
for (File f : files) {
directoryUpdate(f, stringBuilder, floor);
}
}
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(file));
bw.write(stringBuilder.toString());
bw.flush();
} catch (IOException e) {
e.printStackTrace();
}
} else if (!tagerFile.isDirectory()) {
// for (int i = 0; i < floor; i++) {
// stringBuilder.append("#");
// }
stringBuilder
.append("##### [")
.append(tagerFile.getName())
.append("](")
.append(getDomainNameConversion(tagerFile))
.append(")\n");
} else if (tagerFile.isDirectory()) {
for (int i = 0; i < floor; i++) {
stringBuilder.append("#");
}
stringBuilder
.append(" [")
.append(tagerFile.getName())
.append("](")
.append(getDomainNameConversion(tagerFile))
.append(")\n");
File[] files = tagerFile.listFiles();
if (files != null) {
floor++;
for (File f : files) {
directoryUpdate(f, stringBuilder, floor);
}
}
}
}
private static void statisticalProgress(File sourceFile, List<Pair<Long, String>> arrayList) {
if (arrayList == null) {
arrayList = new LinkedList<>();
}
if (sourceFile.isDirectory()) {
File[] files = sourceFile.listFiles();
for (int i = 0; i < files.length; i++) {
statisticalProgress(files[i], arrayList);
}
arrayList = new ArrayList<>(arrayList);
arrayList.sort((o1, o2) -> o1.getKey() > o2.getKey() ? 0 : -1);
File file = Paths.get(System.getProperty("user.dir"), "ProjectProgress.md").toFile();
createNewFile(file);
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(file));
for (int i = 0; i < arrayList.size(); i++) {
Pair<Long, String> longStringPair = arrayList.get(i);
File file1 = new File(longStringPair.getValue());
bw.write("+ [" + file1.getName());
bw.write("(" + longStringPair.getKey() + ")]");
bw.write("(");
bw.write(getDomainNameConversion(file1));
bw.write("\n");
bw.flush();
}
} catch (IOException e) {
e.printStackTrace();
}
} else {
arrayList.add(new Pair<>(sourceFile.length(), sourceFile.getPath()));
}
}
private static void createNewFile(File file) {
if (!file.exists()) {
try {
boolean newFile = file.createNewFile();
System.out.println("createNewFile:" + newFile);
} catch (IOException e) {
e.printStackTrace();
}
}
}
private static int getIndex(File file) {
int index = -1;
String name = file.getName();
int count = 0;
Pattern p = Pattern.compile("-");
Matcher m = p.matcher(name);
while (m.find()) {
count++;
}
if (count < 2) {
return index;
}
String substring = name.substring(name.indexOf("-") + 1, name.indexOf("-", name.indexOf("-") + 1));
if ("".equals(substring)) {
return index;
}
index = Integer.valueOf(substring);
return index;
}
private static String getDomainNameConversion(File file) {
String domainName = "https://github.com/android-exchange/Android-Interview/tree/master";
Path path = Paths.get(file.getPath().replace(System.getProperty("user.dir"), ""));
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append(domainName);
Iterator<Path> iterator = path.iterator();
while (iterator.hasNext()) {
stringBuilder.append("/").append(iterator.next());
}
return stringBuilder.toString();
}
/**
* get source file
*
* @return
*/
private static File getSourceFile() {
return Paths.get(System.getProperty("user.dir"), "resources", "sourcefile").toFile();
}
/**
* get base file
*
* @return
*/
private static File getBaseFile() {
return Paths.get(System.getProperty("user.dir")).toFile();
}
}