Files
VisualSapfor/src/Common/Utils/CommonUtils.java

275 lines
7.6 KiB
Java
Raw Normal View History

package Common.Utils;
2024-10-07 14:22:52 +03:00
import Common.CommonConstants;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import org.apache.commons.io.FileUtils;
import java.io.File;
import java.nio.charset.Charset;
public class CommonUtils {
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);
// }
//--
public static Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().setPrettyPrinting().create();
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-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 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));
}
public static boolean isDigit(String s) {
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);
}
//ФАЙЛЫ
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) {
return ((double)file.length()) / (1024 * 1024);
}
//-
//ГЕНЕРАЦИЯ ИМЕН
}