Skip to content
This repository was archived by the owner on Jan 4, 2024. It is now read-only.

Commit 0c547b6

Browse files
committed
1.1
1 parent 45f4398 commit 0c547b6

File tree

16 files changed

+512
-261
lines changed

16 files changed

+512
-261
lines changed

.idea/libraries/FakeBukkitApi_jdk11_version1.xml

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/vcs.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

JavaNeoUtil.iml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
<component name="NewModuleRootManager" inherit-compiler-output="true">
44
<exclude-output />
55
<content url="file://$MODULE_DIR$">
6+
<sourceFolder url="file://$MODULE_DIR$/bukkit-res" type="java-resource" />
67
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
78
</content>
89
<orderEntry type="inheritedJdk" />
910
<orderEntry type="sourceFolder" forTests="false" />
11+
<orderEntry type="library" name="FakeBukkitApi-jdk11-version1" level="project" />
1012
</component>
1113
</module>

bukkit-res/plugin.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
name: JavaNeoUtil
2+
version: 1.1
3+
main: com.fazziclay.javaneoutil.bukkit.Plugin
990 Bytes
Binary file not shown.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.fazziclay.javaneoutil;
2+
3+
/**
4+
* @author FazziCLAY ( <a href="https://fazziclay.github.io">https://fazziclay.github.io</a> )
5+
* **/
6+
public class ArrayUtil {
7+
public static <T> void sortByName(T[] list, SortStringConsumer<T> consumer) {
8+
int n = list.length;
9+
T temp;
10+
11+
for (int i = 0; i < n; i++) {
12+
for (int j = i + 1; j < n; j++) {
13+
T oI = list[i];
14+
T oJ = list[j];
15+
16+
String s1 = consumer.get(oI);
17+
String s2 = consumer.get(oJ);
18+
if (s1 == null) s1 = "";
19+
if (s2 == null) s2 = "";
20+
21+
if (s1.compareTo(s2) > 0) {
22+
temp = list[i];
23+
list[i] = list[j];
24+
list[j] = temp;
25+
}
26+
}
27+
}
28+
}
29+
30+
public interface SortStringConsumer<T> {
31+
String get(T o);
32+
}
33+
34+
public static <T> void reverse(T[] list) {
35+
for (int i = 0; i < list.length; i++) {
36+
int iRev = list.length - 1 - i;
37+
T temp = list[i];
38+
list[i] = list[iRev];
39+
list[iRev] = temp;
40+
}
41+
}
42+
}
Lines changed: 285 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,285 @@
1+
package com.fazziclay.javaneoutil;
2+
3+
import java.io.File;
4+
import java.io.FileReader;
5+
import java.io.FileWriter;
6+
7+
/**
8+
* For files :). License: MIT
9+
* @author FazziCLAY ( <a href="https://fazziclay.github.io">https://fazziclay.github.io</a> )
10+
* **/
11+
public class FileUtil {
12+
/**
13+
* Fix Windows-Linux path (replace / & \\ to File.separator)
14+
**/
15+
private static String fixPathSeparator(@NonNull String path) {
16+
return path
17+
.replace("/", File.separator)
18+
.replace("\\", File.separator);
19+
}
20+
21+
/**
22+
* Получить файл из пути, путь чинится Windows-Linux
23+
**/
24+
public static File getFile(String path) {
25+
return new File(fixPathSeparator(path));
26+
}
27+
28+
/**
29+
* <h1>Example</h1>
30+
* <p>Input: "/test/owo/f.json"</p>
31+
* <p>Output: "/test/owo"</p>
32+
**/
33+
public static String getParentDirOfFile(String path) {
34+
path = fixPathSeparator(path);
35+
int lastSep = path.lastIndexOf(File.separator);
36+
if (lastSep > 0) {
37+
return path.substring(0, lastSep) + File.separator;
38+
}
39+
return File.separator;
40+
}
41+
42+
/**
43+
* @see FileUtil#getParentDirOfFile(String)
44+
**/
45+
public static File getParentDirOfFile(File file) {
46+
return new File(getParentDirOfFile(fixPathSeparator(file.getAbsolutePath())));
47+
}
48+
49+
/**
50+
* @see FileUtil#createDirIfNotExists(File)
51+
**/
52+
public static void createDirIfNotExists(String path) {
53+
createDirIfNotExists(getFile(path));
54+
}
55+
56+
/**
57+
* Создать дирректорию если она не существует
58+
**/
59+
public static void createDirIfNotExists(File file) {
60+
if (!file.exists()) {
61+
//noinspection ResultOfMethodCallIgnored
62+
file.mkdirs();
63+
}
64+
}
65+
66+
/**
67+
* @see FileUtil#getParentDirOfFile(File)
68+
**/
69+
public static void createNewIfNotExist(@NonNull String path) {
70+
createNewIfNotExist(getFile(path));
71+
}
72+
73+
/**
74+
* Создать файл, если он существует то ничего не делаем
75+
**/
76+
public static void createNewIfNotExist(@NonNull File file) {
77+
try {
78+
if (!file.exists()) {
79+
createDirIfNotExists(getParentDirOfFile(file));
80+
81+
//noinspection ResultOfMethodCallIgnored
82+
file.createNewFile();
83+
}
84+
} catch (Exception e) {
85+
throw new RuntimeException("Exception while create new file", e);
86+
}
87+
}
88+
89+
/**
90+
* @see FileUtil#isExist(File)
91+
**/
92+
public static boolean isExist(@NonNull String path) {
93+
return isExist(getFile(path));
94+
}
95+
96+
/**
97+
* Существует ли файл
98+
**/
99+
public static boolean isExist(@NonNull File file) {
100+
return file.exists();
101+
}
102+
103+
/**
104+
* @see FileUtil#getFilesList(File)
105+
**/
106+
public static File[] getFilesList(@NonNull String dirPath) {
107+
return getFilesList(getFile(dirPath));
108+
}
109+
110+
/**
111+
* Список файлов в дирректории
112+
**/
113+
public static File[] getFilesList(@NonNull File dir) {
114+
if (!isExist(dir) || !dir.isDirectory()) {
115+
return null;
116+
}
117+
return dir.listFiles();
118+
}
119+
120+
public static void delete(@NonNull String path) {
121+
delete(getFile(path));
122+
}
123+
124+
/**
125+
* Если файл, то удаляет
126+
* Если папка, то удаляет (рекурсивно)
127+
* #RECURSION
128+
**/
129+
public static void delete(@NonNull File path) {
130+
if (path.isDirectory()) {
131+
File[] children = getFilesList(path);
132+
if (children != null) {
133+
for (File child : children) {
134+
delete(child);
135+
}
136+
}
137+
//noinspection ResultOfMethodCallIgnored
138+
path.delete();
139+
140+
} else if (path.isFile()) {
141+
//noinspection ResultOfMethodCallIgnored
142+
path.delete();
143+
}
144+
}
145+
146+
/**
147+
* @see FileUtil#writeText(File, boolean, String)
148+
**/
149+
public static void writeText(@NonNull String path, boolean append, String text) {
150+
writeText(getFile(path), append, text);
151+
}
152+
153+
/**
154+
* Записывает текст в файл, если нету то создаст
155+
**/
156+
public static void writeText(@NonNull File file, boolean append, String text) {
157+
createNewIfNotExist(file);
158+
try {
159+
final FileWriter fileWriter = new FileWriter(file, append);
160+
fileWriter.write(text);
161+
fileWriter.flush();
162+
fileWriter.close();
163+
164+
} catch (Exception e) {
165+
throw new RuntimeException("Exception while write text to file", e);
166+
}
167+
}
168+
169+
/**
170+
* @see FileUtil#setText(File, String)
171+
**/
172+
public static void setText(@NonNull String path, String text) {
173+
setText(getFile(path), text);
174+
}
175+
176+
/**
177+
* Ставит в файл текст
178+
*
179+
* @see #writeText(File, boolean, String)
180+
**/
181+
public static void setText(@NonNull File file, String text) {
182+
writeText(file, false, text);
183+
}
184+
185+
/**
186+
* @see #addText(File, String)
187+
**/
188+
public static void addText(@NonNull String path, String text) {
189+
addText(getFile(path), text);
190+
}
191+
192+
/**
193+
* Добавляет в файл текст в конец
194+
*
195+
* @see #writeText(File, boolean, String)
196+
**/
197+
public static void addText(@NonNull File file, String text) {
198+
writeText(file, true, text);
199+
}
200+
201+
/**
202+
* @see #getText(File, String)
203+
**/
204+
public static String getText(@NonNull String path, String defaultValue) {
205+
return getText(getFile(path), defaultValue);
206+
}
207+
208+
/**
209+
* Получить текст из файла, если файла нету то возвращаем defaultValue
210+
* если файла не существует, то и не создаём его!
211+
*
212+
* @param defaultValue вывод если файла не существует
213+
**/
214+
public static String getText(@NonNull File file, String defaultValue) {
215+
if (!isExist(file)) {
216+
return defaultValue;
217+
}
218+
219+
try {
220+
final StringBuilder result = new StringBuilder();
221+
final FileReader fileReader = new FileReader(file);
222+
223+
final char[] buff = new char[1024];
224+
225+
int i;
226+
while ((i = fileReader.read(buff)) > 0) {
227+
result.append(new String(buff, 0, i));
228+
}
229+
230+
fileReader.close();
231+
return result.toString();
232+
233+
} catch (Exception e) {
234+
throw new RuntimeException("Exception while get file text", e);
235+
}
236+
}
237+
238+
/**
239+
* @see #getText(File)
240+
**/
241+
public static String getText(String path) {
242+
return getText(getFile(path));
243+
}
244+
245+
/**
246+
* if file not exist: return null
247+
*
248+
* @see #getText(String, String)
249+
**/
250+
public static String getText(File file) {
251+
return getText(file, null);
252+
}
253+
254+
255+
// == DEPRECATED ==
256+
@Deprecated
257+
public static void write(File file, String content) {
258+
write(file.getAbsolutePath(), content);
259+
}
260+
261+
@Deprecated
262+
public static void write(String path, String content) {
263+
setText(getFile(path), content);
264+
}
265+
266+
@Deprecated
267+
public static String read(File file, String defaultValue) {
268+
return read(file.getAbsolutePath(), defaultValue);
269+
}
270+
271+
@Deprecated
272+
public static String read(String path) {
273+
return read(path, null);
274+
}
275+
276+
@Deprecated
277+
public static String read(String path, String defaultValue) {
278+
return getText(getFile(path), defaultValue);
279+
}
280+
281+
@Deprecated
282+
public static void deleteDir(File dir) {
283+
delete(dir);
284+
}
285+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.fazziclay.javaneoutil;
2+
3+
public interface Filter<T> {
4+
boolean accept(T entry);
5+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.fazziclay.javaneoutil;
2+
3+
public class JavaNeoUtil {
4+
// !!! Change in bukkit-res/plugin.yml !!!
5+
public final static String VERSION_NAME = "1.1";
6+
public final static int VERSION_CODE = 2;
7+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package com.fazziclay.javaneoutil;
2+
3+
public @interface NonNull {
4+
}

0 commit comments

Comments
 (0)