2024-10-07 00:58:29 +03:00
|
|
|
|
package Common.Utils;
|
2024-10-07 14:22:52 +03:00
|
|
|
|
import Common.CommonConstants;
|
2024-10-14 12:54:52 +03:00
|
|
|
|
import Common.Passes.PassException;
|
2024-10-07 00:58:29 +03:00
|
|
|
|
import com.google.gson.Gson;
|
|
|
|
|
|
import com.google.gson.GsonBuilder;
|
2024-10-15 20:25:51 +03:00
|
|
|
|
import com.google.gson.JsonObject;
|
|
|
|
|
|
import com.google.gson.JsonParser;
|
2024-10-07 00:58:29 +03:00
|
|
|
|
import org.apache.commons.io.FileUtils;
|
|
|
|
|
|
|
2024-10-07 20:04:11 +03:00
|
|
|
|
import javax.swing.*;
|
|
|
|
|
|
import java.awt.*;
|
2024-10-07 22:04:09 +03:00
|
|
|
|
import java.awt.datatransfer.DataFlavor;
|
|
|
|
|
|
import java.awt.datatransfer.StringSelection;
|
2024-10-07 00:58:29 +03:00
|
|
|
|
import java.io.File;
|
2024-10-07 22:04:09 +03:00
|
|
|
|
import java.io.FileOutputStream;
|
2024-10-07 20:04:11 +03:00
|
|
|
|
import java.net.URL;
|
2024-10-07 00:58:29 +03:00
|
|
|
|
import java.nio.charset.Charset;
|
2024-10-07 22:04:09 +03:00
|
|
|
|
import java.nio.file.Files;
|
|
|
|
|
|
import java.text.SimpleDateFormat;
|
2024-10-07 17:46:38 +03:00
|
|
|
|
import java.util.Arrays;
|
2024-10-07 22:04:09 +03:00
|
|
|
|
import java.util.Date;
|
2024-10-07 17:46:38 +03:00
|
|
|
|
import java.util.Vector;
|
2024-10-07 22:22:51 +03:00
|
|
|
|
import java.util.concurrent.Semaphore;
|
2024-10-07 22:04:09 +03:00
|
|
|
|
import java.util.regex.Matcher;
|
2024-10-07 17:46:38 +03:00
|
|
|
|
import java.util.stream.Collectors;
|
2024-10-11 00:00:30 +03:00
|
|
|
|
public class Utils_ {
|
2024-10-07 22:04:09 +03:00
|
|
|
|
//ГЛОБАЛЬНЫЙ ЖУРНАЛ
|
|
|
|
|
|
public static Loggable MainLog;
|
2024-10-07 22:22:51 +03:00
|
|
|
|
public static Semaphore date_semaphore = new Semaphore(1);
|
|
|
|
|
|
public static long last_ticks = CommonConstants.Nan;
|
|
|
|
|
|
//--
|
2024-10-07 14:22:52 +03:00
|
|
|
|
//JSON
|
|
|
|
|
|
//--
|
|
|
|
|
|
// public static String jsonToPrettyFormat(String packed) {
|
|
|
|
|
|
// JsonParser parser = new JsonParser();
|
|
|
|
|
|
// JsonObject json = parser.parse(packed).getAsJsonObject();
|
|
|
|
|
|
// Gson gson = new GsonBuilder().setPrettyPrinting().create();
|
|
|
|
|
|
// return gson.toJson(json);
|
|
|
|
|
|
// }
|
|
|
|
|
|
//--
|
2024-10-07 00:58:29 +03:00
|
|
|
|
public static Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().setPrettyPrinting().create();
|
2024-10-14 15:19:13 +03:00
|
|
|
|
static String HomePath = System.getProperty("user.dir");
|
|
|
|
|
|
//Текущая оперционная система
|
|
|
|
|
|
public static boolean isWindows() {
|
|
|
|
|
|
return System.getProperty("os.name").startsWith("Windows");
|
|
|
|
|
|
}
|
|
|
|
|
|
public static String getHomePath() {
|
|
|
|
|
|
return HomePath;
|
|
|
|
|
|
}
|
|
|
|
|
|
public static void setHomePath(String path_in) {
|
|
|
|
|
|
HomePath = path_in;
|
|
|
|
|
|
}
|
|
|
|
|
|
public static File getHomeDirectory() {
|
|
|
|
|
|
return new File(getHomePath());
|
|
|
|
|
|
}
|
|
|
|
|
|
//--------------------------------------------------
|
|
|
|
|
|
public static Object requireNonNullElse(Object value, Object default_value) {
|
|
|
|
|
|
return (value != null) ? value : default_value;
|
|
|
|
|
|
}
|
2024-10-07 22:04:09 +03:00
|
|
|
|
//------------------------------------------------------------------
|
|
|
|
|
|
//-
|
2024-10-07 00:58:29 +03:00
|
|
|
|
public static <T> T jsonFromFile(File file, Class<T> json_class) throws Exception {
|
|
|
|
|
|
return gson.fromJson(FileUtils.readFileToString(file, Charset.defaultCharset()), json_class);
|
|
|
|
|
|
}
|
|
|
|
|
|
public static void jsonToFile(Object json_object, File file) throws Exception {
|
|
|
|
|
|
FileUtils.writeStringToFile(file, gson.toJson(json_object));
|
|
|
|
|
|
}
|
2024-10-15 23:01:36 +03:00
|
|
|
|
public static JsonObject getPropertiesAsJsonObject() throws Exception {
|
2024-10-15 20:25:51 +03:00
|
|
|
|
File propertiesFile = new File(System.getProperty("user.dir"), "properties");
|
2024-10-15 23:01:36 +03:00
|
|
|
|
if (!propertiesFile.exists()) {
|
2024-10-15 20:25:51 +03:00
|
|
|
|
System.out.println("Файл properties не найден!");
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
String packed = FileUtils.readFileToString(propertiesFile, Charset.defaultCharset());
|
|
|
|
|
|
JsonParser parser = new JsonParser();
|
|
|
|
|
|
JsonObject json = parser.parse(packed).getAsJsonObject();
|
|
|
|
|
|
return json;
|
|
|
|
|
|
}
|
2024-10-07 22:04:09 +03:00
|
|
|
|
//БУФЕР ОБМЕНА
|
|
|
|
|
|
public static void CopyToClipboard(String text) {
|
|
|
|
|
|
Toolkit.getDefaultToolkit()
|
|
|
|
|
|
.getSystemClipboard()
|
|
|
|
|
|
.setContents(
|
|
|
|
|
|
new StringSelection(text),
|
|
|
|
|
|
null
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
public static String getFromClipboard() {
|
|
|
|
|
|
String res = "";
|
|
|
|
|
|
try {
|
|
|
|
|
|
res = (String) Toolkit.getDefaultToolkit().getSystemClipboard().getData(DataFlavor.stringFlavor);
|
|
|
|
|
|
} catch (Exception ex) {
|
|
|
|
|
|
MainLog.PrintException(ex);
|
|
|
|
|
|
}
|
|
|
|
|
|
return res;
|
|
|
|
|
|
}
|
|
|
|
|
|
public static String strikeThrough(String s) {
|
|
|
|
|
|
StringBuilder res = new StringBuilder();
|
|
|
|
|
|
for (char c : s.toCharArray()) {
|
|
|
|
|
|
res.append(c);
|
|
|
|
|
|
if (c != CommonConstants.toStrike)
|
|
|
|
|
|
res.append(CommonConstants.toStrike);
|
|
|
|
|
|
}
|
|
|
|
|
|
return res.toString();
|
|
|
|
|
|
}
|
|
|
|
|
|
public static String noStrikeThrough(String s) {
|
|
|
|
|
|
StringBuilder res = new StringBuilder();
|
|
|
|
|
|
for (char c : s.toCharArray()) {
|
|
|
|
|
|
if (c != (CommonConstants.toStrike))
|
|
|
|
|
|
res.append(c);
|
|
|
|
|
|
}
|
|
|
|
|
|
return res.toString();
|
|
|
|
|
|
}
|
|
|
|
|
|
public static boolean validateEmail(String address, TextLog log) {
|
|
|
|
|
|
Matcher matcher = CommonConstants.VALID_EMAIL_ADDRESS_REGEX.matcher(address);
|
|
|
|
|
|
if (!matcher.find()) {
|
|
|
|
|
|
log.Writeln_("введённый адрес электронной почты некорректен.");
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
String match = address.substring(matcher.start(), matcher.end());
|
|
|
|
|
|
if (!match.equals(address)) {
|
|
|
|
|
|
log.Writeln_("введённый адрес электронной почты некорректен.");
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
2024-10-07 14:22:52 +03:00
|
|
|
|
public static String hideRegularMetasymbols(String word) {
|
|
|
|
|
|
String res = word.replace("\\", "\\\\");
|
|
|
|
|
|
for (char c : CommonConstants.regular_metasymbols)
|
|
|
|
|
|
res = res.replace(String.valueOf(c), "\\" + c);
|
|
|
|
|
|
return res;
|
|
|
|
|
|
}
|
|
|
|
|
|
public static String DQuotes(Object o) {
|
|
|
|
|
|
return "\"" + o.toString() + "\"";
|
|
|
|
|
|
}
|
|
|
|
|
|
public static String Quotes(Object o) {
|
|
|
|
|
|
return "'" + o.toString() + "'";
|
|
|
|
|
|
}
|
|
|
|
|
|
public static String Brackets(Object o) {
|
|
|
|
|
|
return "[" + o.toString() + "]";
|
|
|
|
|
|
}
|
|
|
|
|
|
public static String RBrackets(Object o) {
|
|
|
|
|
|
return "(" + o.toString() + ")";
|
|
|
|
|
|
}
|
|
|
|
|
|
public static String TBrackets(Object o) {
|
|
|
|
|
|
return "<" + o.toString() + ">";
|
2024-10-07 14:44:24 +03:00
|
|
|
|
}//FortranSPFTokenMaker
|
2024-10-07 22:04:09 +03:00
|
|
|
|
public static boolean ContainsForbiddenName(String string) {
|
|
|
|
|
|
char[] chars = string.toCharArray();
|
|
|
|
|
|
for (char c : chars)
|
|
|
|
|
|
if (isForbiddenCharacter(c)) return true;
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
public static boolean isForbiddenCharacter(char c) {
|
|
|
|
|
|
return CommonConstants.forbidden_file_name_characters.contains(c);
|
|
|
|
|
|
}
|
|
|
|
|
|
public static String ReplaceForbiddenCharacters(String name) {
|
|
|
|
|
|
StringBuilder res = new StringBuilder();
|
|
|
|
|
|
for (char c : name.toCharArray())
|
|
|
|
|
|
res.append(isForbiddenCharacter(c) ? '_' : c);
|
|
|
|
|
|
return res.toString();
|
|
|
|
|
|
}
|
|
|
|
|
|
//Синтаксис и регулярные выражения
|
2024-10-08 22:33:49 +03:00
|
|
|
|
public static String printAllForbiddenCharacters() {
|
2024-10-07 22:04:09 +03:00
|
|
|
|
Vector<String> res = new Vector<>();
|
2024-10-08 22:33:49 +03:00
|
|
|
|
for (char c : CommonConstants.forbidden_file_name_characters)
|
2024-10-07 22:04:09 +03:00
|
|
|
|
res.add(String.valueOf(c));
|
|
|
|
|
|
return String.join(" ", res);
|
|
|
|
|
|
}
|
2024-10-07 14:22:52 +03:00
|
|
|
|
public static boolean ContainsCyrillic(String string) {
|
|
|
|
|
|
return string.chars()
|
|
|
|
|
|
.mapToObj(Character.UnicodeBlock::of)
|
|
|
|
|
|
.anyMatch(b -> b.equals(Character.UnicodeBlock.CYRILLIC));
|
|
|
|
|
|
}
|
2024-10-08 00:39:13 +03:00
|
|
|
|
public static boolean isIntegerValue(String s) {
|
2024-10-07 14:22:52 +03:00
|
|
|
|
try {
|
|
|
|
|
|
Integer.parseInt(s);
|
|
|
|
|
|
return true;
|
|
|
|
|
|
} catch (NumberFormatException e) {
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
public static boolean isEnglishLetter(char c) {
|
|
|
|
|
|
return (((c >= 'a') && (c <= 'z')) || ((c >= 'A') && (c <= 'Z')));
|
|
|
|
|
|
}
|
|
|
|
|
|
public static boolean isRussianLetter(char c) {
|
|
|
|
|
|
return ((c >= 'а') && (c <= 'я'))
|
|
|
|
|
|
|| ((c >= 'А') && (c <= 'Я'))
|
|
|
|
|
|
|| (c == 'Ё')
|
|
|
|
|
|
|| (c == 'ё');
|
|
|
|
|
|
}
|
|
|
|
|
|
public static boolean isSign(char c) {
|
|
|
|
|
|
switch (c) {
|
|
|
|
|
|
//арифметика.
|
|
|
|
|
|
case '+':
|
|
|
|
|
|
case '-':
|
|
|
|
|
|
case '*':
|
|
|
|
|
|
case '/':
|
|
|
|
|
|
case '<':
|
|
|
|
|
|
case '>':
|
|
|
|
|
|
case '&':
|
|
|
|
|
|
case '=':
|
|
|
|
|
|
case '%':
|
|
|
|
|
|
case '^':
|
|
|
|
|
|
//- обр слеш
|
|
|
|
|
|
case '\\':
|
|
|
|
|
|
//препинание
|
|
|
|
|
|
case ' ':
|
|
|
|
|
|
case '_':
|
|
|
|
|
|
case '.':
|
|
|
|
|
|
case ',':
|
|
|
|
|
|
case '!':
|
|
|
|
|
|
case '?':
|
|
|
|
|
|
case ';':
|
|
|
|
|
|
case ':':
|
|
|
|
|
|
//escape последовательности
|
|
|
|
|
|
case '\t':
|
|
|
|
|
|
case '\n':
|
|
|
|
|
|
case '\r':
|
|
|
|
|
|
//кавычки
|
|
|
|
|
|
case '\'':
|
|
|
|
|
|
case '"':
|
|
|
|
|
|
//- скобки
|
|
|
|
|
|
case '(':
|
|
|
|
|
|
case ')':
|
|
|
|
|
|
case '[':
|
|
|
|
|
|
case ']':
|
|
|
|
|
|
case '{':
|
|
|
|
|
|
case '}':
|
|
|
|
|
|
//прочее
|
|
|
|
|
|
case '~':
|
|
|
|
|
|
case '`':
|
|
|
|
|
|
case '|':
|
|
|
|
|
|
case '@':
|
|
|
|
|
|
case '$':
|
|
|
|
|
|
case '#':
|
|
|
|
|
|
case '№':
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
public static char Translit(char c) {
|
|
|
|
|
|
switch (c) {
|
|
|
|
|
|
case 'А':
|
|
|
|
|
|
case 'а':
|
|
|
|
|
|
case 'Я':
|
|
|
|
|
|
case 'я':
|
|
|
|
|
|
return 'A';
|
|
|
|
|
|
//
|
|
|
|
|
|
case 'Б':
|
|
|
|
|
|
case 'б':
|
|
|
|
|
|
return 'B';
|
|
|
|
|
|
//-
|
|
|
|
|
|
case 'В':
|
|
|
|
|
|
case 'в':
|
|
|
|
|
|
return 'V';
|
|
|
|
|
|
//
|
|
|
|
|
|
case 'Г':
|
|
|
|
|
|
case 'г':
|
|
|
|
|
|
return 'G';
|
|
|
|
|
|
//
|
|
|
|
|
|
case 'Д':
|
|
|
|
|
|
case 'д':
|
|
|
|
|
|
return 'D';
|
|
|
|
|
|
//
|
|
|
|
|
|
case 'Е':
|
|
|
|
|
|
case 'е':
|
|
|
|
|
|
case 'Ё':
|
|
|
|
|
|
case 'ё':
|
|
|
|
|
|
case 'Э':
|
|
|
|
|
|
case 'э':
|
|
|
|
|
|
return 'E';
|
|
|
|
|
|
//
|
|
|
|
|
|
case 'Ж':
|
|
|
|
|
|
case 'ж':
|
|
|
|
|
|
return 'J';
|
|
|
|
|
|
//
|
|
|
|
|
|
case 'З':
|
|
|
|
|
|
case 'з':
|
|
|
|
|
|
return 'Z';
|
|
|
|
|
|
//
|
|
|
|
|
|
case 'И':
|
|
|
|
|
|
case 'и':
|
|
|
|
|
|
case 'Й':
|
|
|
|
|
|
case 'й':
|
|
|
|
|
|
return 'I';
|
|
|
|
|
|
//
|
|
|
|
|
|
case 'К':
|
|
|
|
|
|
case 'к':
|
|
|
|
|
|
return 'K';
|
|
|
|
|
|
//
|
|
|
|
|
|
case 'Л':
|
|
|
|
|
|
case 'л':
|
|
|
|
|
|
return 'L';
|
|
|
|
|
|
//
|
|
|
|
|
|
case 'М':
|
|
|
|
|
|
case 'м':
|
|
|
|
|
|
return 'M';
|
|
|
|
|
|
//
|
|
|
|
|
|
case 'Н':
|
|
|
|
|
|
case 'н':
|
|
|
|
|
|
return 'N';
|
|
|
|
|
|
//
|
|
|
|
|
|
case 'О':
|
|
|
|
|
|
case 'о':
|
|
|
|
|
|
return 'O';
|
|
|
|
|
|
//
|
|
|
|
|
|
case 'П':
|
|
|
|
|
|
case 'п':
|
|
|
|
|
|
return 'P';
|
|
|
|
|
|
//
|
|
|
|
|
|
case 'Р':
|
|
|
|
|
|
case 'р':
|
|
|
|
|
|
return 'R';
|
|
|
|
|
|
//
|
|
|
|
|
|
case 'С':
|
|
|
|
|
|
case 'с':
|
|
|
|
|
|
return 'S';
|
|
|
|
|
|
case 'Т':
|
|
|
|
|
|
case 'т':
|
|
|
|
|
|
return 'T';
|
|
|
|
|
|
//
|
|
|
|
|
|
case 'У':
|
|
|
|
|
|
case 'у':
|
|
|
|
|
|
case 'Ю':
|
|
|
|
|
|
case 'ю':
|
|
|
|
|
|
return 'U';
|
|
|
|
|
|
case 'Х':
|
|
|
|
|
|
case 'х':
|
|
|
|
|
|
case 'Щ':
|
|
|
|
|
|
case 'щ':
|
|
|
|
|
|
case 'Ш':
|
|
|
|
|
|
case 'ш':
|
|
|
|
|
|
return 'H';
|
|
|
|
|
|
//
|
|
|
|
|
|
case 'Ф':
|
|
|
|
|
|
case 'ф':
|
|
|
|
|
|
return 'F';
|
|
|
|
|
|
//
|
|
|
|
|
|
case 'Ч':
|
|
|
|
|
|
case 'ч':
|
|
|
|
|
|
case 'Ц':
|
|
|
|
|
|
case 'ц':
|
|
|
|
|
|
return 'C';
|
|
|
|
|
|
//
|
|
|
|
|
|
case 'Ы':
|
|
|
|
|
|
case 'ы':
|
|
|
|
|
|
return 'Y';
|
|
|
|
|
|
//
|
|
|
|
|
|
}
|
|
|
|
|
|
return ' ';
|
|
|
|
|
|
}
|
|
|
|
|
|
public static String ending(boolean flag) {
|
|
|
|
|
|
return flag ? ")" : ",";
|
|
|
|
|
|
}
|
|
|
|
|
|
public static boolean isRBracketsBalanced(String fragment) {
|
|
|
|
|
|
int cc = 0;
|
|
|
|
|
|
for (char c : fragment.toCharArray()) {
|
|
|
|
|
|
if (c == '(')
|
|
|
|
|
|
cc++;
|
|
|
|
|
|
if (c == ')')
|
|
|
|
|
|
cc--;
|
|
|
|
|
|
if (cc < 0)
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
return (cc == 0);
|
|
|
|
|
|
}
|
2024-10-07 17:46:38 +03:00
|
|
|
|
public static String removeRedundantSpaces(String s_in) {
|
|
|
|
|
|
return String.join(" ",
|
|
|
|
|
|
Arrays.stream(s_in.split(" ")).filter(d -> !d.isEmpty()).collect(Collectors.toCollection(Vector::new)));
|
|
|
|
|
|
}
|
|
|
|
|
|
public static String removeCharacters(String string, String... to_remove) {
|
|
|
|
|
|
String res = string;
|
|
|
|
|
|
for (String c : to_remove)
|
|
|
|
|
|
res = res.replace(c, "");
|
|
|
|
|
|
return res;
|
|
|
|
|
|
}
|
2024-10-07 14:22:52 +03:00
|
|
|
|
//ФАЙЛЫ
|
|
|
|
|
|
public static String getExtension(File file) {
|
|
|
|
|
|
String fn = file.getName();
|
|
|
|
|
|
int di = fn.lastIndexOf(".");
|
|
|
|
|
|
return (di >= 0) ? fn.substring(di + 1).toLowerCase() : "";
|
|
|
|
|
|
}
|
|
|
|
|
|
public static String getExtensionFromName(String fn) {
|
|
|
|
|
|
int di = fn.lastIndexOf(".");
|
|
|
|
|
|
return (di >= 0) ? fn.substring(di + 1).toLowerCase() : "";
|
|
|
|
|
|
}
|
|
|
|
|
|
public static String getFileNameWithoutExtension(File file) {
|
|
|
|
|
|
return getNameWithoutExtension(file.getName());
|
|
|
|
|
|
}
|
|
|
|
|
|
public static String getNameWithoutExtension(String fn) {
|
|
|
|
|
|
return (fn.contains(".")) ? fn.substring(0, fn.lastIndexOf(".")).toLowerCase() : fn.toLowerCase();
|
|
|
|
|
|
}
|
|
|
|
|
|
public static String toU(String path) {
|
|
|
|
|
|
return path.replace('\\', '/');
|
|
|
|
|
|
}
|
|
|
|
|
|
public static String toW(String path) {
|
|
|
|
|
|
return path.replace('/', '\\');
|
|
|
|
|
|
}
|
|
|
|
|
|
public static double getFileSizeMegaBytes(File file) {
|
2024-10-08 22:33:49 +03:00
|
|
|
|
return ((double) file.length()) / (1024 * 1024);
|
2024-10-07 14:22:52 +03:00
|
|
|
|
}
|
2024-10-07 22:04:09 +03:00
|
|
|
|
public static byte[] fileToBytes(File src) throws Exception {
|
|
|
|
|
|
byte[] dst = Files.readAllBytes(src.toPath());
|
|
|
|
|
|
return dst;
|
|
|
|
|
|
}
|
|
|
|
|
|
public static void bytesToFile(byte[] bytes, File dst) throws Exception {
|
|
|
|
|
|
FileOutputStream os = new FileOutputStream(dst);
|
|
|
|
|
|
os.write(bytes);
|
|
|
|
|
|
os.close();
|
|
|
|
|
|
}
|
2024-10-14 12:54:52 +03:00
|
|
|
|
public static void CheckDirectory(File dir) {
|
|
|
|
|
|
if (!dir.exists()) {
|
|
|
|
|
|
try {
|
|
|
|
|
|
FileUtils.forceMkdir(dir);
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
|
MainLog.PrintException(e);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
public static void CheckAndCleanDirectory(File dir) {
|
|
|
|
|
|
if (dir.exists()) {
|
|
|
|
|
|
File[] files = dir.listFiles();
|
|
|
|
|
|
if (files != null)
|
|
|
|
|
|
for (File f : files) {
|
|
|
|
|
|
try {
|
|
|
|
|
|
forceDeleteWithCheck(f);
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
|
MainLog.PrintException(e);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
} else {
|
|
|
|
|
|
try {
|
|
|
|
|
|
FileUtils.forceMkdir(dir);
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
|
MainLog.PrintException(e);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
public static void forceDeleteWithCheck(File file) throws Exception {
|
|
|
|
|
|
int attempts = 0;
|
|
|
|
|
|
while (attempts < 10) {
|
|
|
|
|
|
if (file.exists()) {
|
|
|
|
|
|
try {
|
|
|
|
|
|
FileUtils.forceDelete(file);
|
|
|
|
|
|
} catch (Exception ex) {
|
|
|
|
|
|
ex.printStackTrace();
|
|
|
|
|
|
}
|
|
|
|
|
|
} else return;
|
|
|
|
|
|
if (file.exists()) {
|
|
|
|
|
|
attempts++;
|
|
|
|
|
|
System.out.println("неудачная попытка удаления: файл " + Brackets(file.getAbsolutePath()) + " занят");
|
|
|
|
|
|
sleep(2000);
|
|
|
|
|
|
} else return;
|
|
|
|
|
|
}
|
|
|
|
|
|
throw new PassException("Не удалось удалить файл " + Brackets(file.getAbsolutePath()) + " за " + attempts + " попыток");
|
|
|
|
|
|
}
|
2024-10-07 20:04:11 +03:00
|
|
|
|
//Иконки
|
|
|
|
|
|
public static ImageIcon getIcon(String path) {
|
2024-10-11 00:00:30 +03:00
|
|
|
|
URL imageUrl = Utils_.class.getResource(path);
|
2024-10-07 20:04:11 +03:00
|
|
|
|
if (imageUrl == null) {
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
return new ImageIcon(imageUrl);
|
|
|
|
|
|
}
|
|
|
|
|
|
public static ImageIcon getTabIcon(String path) {
|
2024-10-11 00:00:30 +03:00
|
|
|
|
URL imageUrl = Utils_.class.getResource(path);
|
2024-10-07 20:04:11 +03:00
|
|
|
|
if (imageUrl == null) {
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
ImageIcon icon = new ImageIcon(imageUrl);
|
|
|
|
|
|
return new ImageIcon(icon.getImage().getScaledInstance(
|
|
|
|
|
|
18,
|
|
|
|
|
|
18,
|
|
|
|
|
|
Image.SCALE_DEFAULT));
|
|
|
|
|
|
}
|
2024-10-07 22:04:09 +03:00
|
|
|
|
//Даты
|
|
|
|
|
|
public static String print_date(Date date) {
|
|
|
|
|
|
return new SimpleDateFormat("dd.MM.yyyy HH:mm:ss").format(date);
|
|
|
|
|
|
}
|
2024-10-07 22:22:51 +03:00
|
|
|
|
public static String printSplittedDateInterval(long milliseconds) {
|
|
|
|
|
|
//--
|
|
|
|
|
|
long seconds = milliseconds / 1000;
|
|
|
|
|
|
long hours = seconds / 3600;
|
|
|
|
|
|
seconds = seconds - hours * 3600;
|
|
|
|
|
|
long minutes = (seconds) / 60;
|
|
|
|
|
|
seconds = seconds - minutes * 60;
|
|
|
|
|
|
//--
|
|
|
|
|
|
return hours + " часов, " + minutes + " минут, " + seconds + " секунд";
|
|
|
|
|
|
}
|
|
|
|
|
|
//Синхронизация
|
|
|
|
|
|
public static void sleep(long millis) {
|
|
|
|
|
|
try {
|
|
|
|
|
|
Thread.sleep(millis);
|
|
|
|
|
|
} catch (Exception ignore) {
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2024-10-07 22:04:09 +03:00
|
|
|
|
//ГЕНЕРАЦИЯ ИМЕН
|
2024-10-07 22:22:51 +03:00
|
|
|
|
public static long getDateNumber() {
|
|
|
|
|
|
//-
|
|
|
|
|
|
try {
|
|
|
|
|
|
date_semaphore.acquire();
|
|
|
|
|
|
} catch (Exception ignore) {
|
|
|
|
|
|
}
|
|
|
|
|
|
//-
|
|
|
|
|
|
long ticks = new Date().toInstant().getEpochSecond();
|
|
|
|
|
|
while (ticks == last_ticks) {
|
|
|
|
|
|
sleep(1);
|
|
|
|
|
|
ticks = new Date().toInstant().getEpochSecond();
|
|
|
|
|
|
}
|
|
|
|
|
|
last_ticks = ticks; // Это и есть разделяемый ресурс.
|
|
|
|
|
|
date_semaphore.release();
|
|
|
|
|
|
return ticks;
|
|
|
|
|
|
}
|
|
|
|
|
|
public static String getDateName(String name_) {
|
|
|
|
|
|
return name_ + "_" + getDateNumber();
|
|
|
|
|
|
}
|
2024-10-08 00:39:13 +03:00
|
|
|
|
public static int fromBoolean(boolean flag) {
|
|
|
|
|
|
return flag ? 1 : 0;
|
|
|
|
|
|
}
|
2024-10-07 00:58:29 +03:00
|
|
|
|
}
|