Utils.java 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package API;
  7. import java.io.File;
  8. import java.io.FileNotFoundException;
  9. import java.io.FileOutputStream;
  10. import java.io.IOException;
  11. import java.io.PrintStream;
  12. import java.nio.file.Files;
  13. import java.nio.file.Paths;
  14. import java.util.ArrayList;
  15. import java.util.Arrays;
  16. import java.util.HashMap;
  17. import java.util.List;
  18. import java.util.Objects;
  19. import java.util.logging.Level;
  20. import java.util.logging.Logger;
  21. /**
  22. *
  23. * @author Eugenio
  24. */
  25. public class Utils {
  26. public static boolean isset(String[] data, int index) {
  27. try {
  28. String a;
  29. a = data[index];
  30. return true;
  31. } catch (ArrayIndexOutOfBoundsException e) {
  32. return false;
  33. }
  34. }
  35. public static boolean IsNumber(String string) {
  36. try {
  37. Integer.parseInt(string);
  38. } catch (Exception e) {
  39. return false;
  40. }
  41. return true;
  42. }
  43. public static String clearName(String nome) {
  44. return nome.split("\\[")[0].replace("*", "").replace("&", "");
  45. }
  46. public static ArrayList<String> arraypad(ArrayList<String> data, String string, int tam) {
  47. if (data.size() == tam) {
  48. return data;
  49. }
  50. for (int i = data.size(); i <= tam; i++) {
  51. data.add(string);
  52. }
  53. return data;
  54. }
  55. // protected String PAD(String value, String replace, int i) {
  56. // return String.format("%" + i + "s", value).replace(" ", replace);
  57. // }
  58. public static String PAD_LEFT = "1$";
  59. public static String PAD_RIGHT = "1$-";
  60. /**
  61. *
  62. * @param str
  63. * @param pad
  64. * @param direction (padRight = '1$-' | padLeft = '1$')
  65. * @param size
  66. * @return
  67. */
  68. public static String Pad(String str, String pad, String direction, int size) {
  69. if (size == 0) {
  70. return str;
  71. }
  72. return String.format("%" + direction + size + "s", str).replace(" ", pad);
  73. }
  74. public static String Pad(int size, String str) {
  75. if (size == 0) {
  76. return str;
  77. }
  78. return String.format("%" + size + "s", str).replace(' ', '0');
  79. }
  80. public static String padPreserveSignal(int size, String str) {
  81. if (size == 0) {
  82. return str;
  83. }
  84. return String.format("%" + size + "s", str).replace(' ', str.charAt(0));
  85. }
  86. public static String int2hex(int num) {
  87. return Integer.toHexString(num);
  88. // return Integer.valueOf(String.valueOf(n), 16);
  89. }
  90. public static String cut(int SizeBits, String paded) {
  91. int strSize = paded.length();
  92. return paded.substring(strSize - SizeBits, strSize);
  93. }
  94. public static String rTrim(String s) {
  95. return s.replaceAll("\\s+$", "");
  96. }
  97. public static String lTrim(String s) {
  98. return s.replaceAll("^\\s+", "");
  99. }
  100. /**
  101. *
  102. * @param s
  103. * @param remove ex: "|\"" para remover terminado com {"}
  104. * @return
  105. */
  106. public static String rTrim(String s, String remover) {
  107. return s.replaceAll("(\\s+" + remover + ")$", "");
  108. }
  109. /**
  110. *
  111. * @param s
  112. * @param remove ex: "|\"" para remover iniciado com {"}
  113. * @return
  114. */
  115. public static String lTrim(String s, String remover) {
  116. return s.replaceAll("^(\\s+" + remover + ")", "");
  117. }
  118. /**
  119. *
  120. * @param s
  121. * @param remove ex: "|\"" para remover iniciado e encerrado com {"}
  122. * @return
  123. */
  124. public static String trim(String s, String remover) {
  125. return Utils.rTrim(Utils.lTrim(s, remover), remover);
  126. }
  127. public static String charToInt(String atribuicao) {
  128. char chr = atribuicao.charAt(0);
  129. return "" + (int) chr;
  130. }
  131. public static String charToInt(char c) {
  132. return "" + (int) c;
  133. }
  134. public static int parseInt(String v) {
  135. return Integer.parseInt(v);
  136. }
  137. public static String Join(List<String> v, String delimiter) {
  138. String[] a = new String[v.size()];
  139. return Join(v.toArray(a), delimiter);
  140. }
  141. public static String Join(String[] v, String delimiter) {
  142. String result = "";
  143. int i = 0;
  144. for (; i < v.length - 1; i++) {
  145. result += v[i] + delimiter;
  146. }
  147. result += v[i];
  148. return result;
  149. }
  150. public static <T> T[] Concat(T[] first, T[] second) {
  151. T[] result = Arrays.copyOf(first, first.length + second.length);
  152. System.arraycopy(second, 0, result, first.length, second.length);
  153. return result;
  154. }
  155. public static String Repeat(String s, String times) {
  156. return Repeat(s, Integer.parseInt(times));
  157. }
  158. public static String Repeat(String s, int times) {
  159. StringBuilder builder = new StringBuilder();
  160. for (int i = 0; i < times; i++) {
  161. builder.append(s);
  162. }
  163. return builder.toString();
  164. }
  165. public static boolean Empty(String s) {
  166. // return (s == null || s.replaceAll("\\s+", "").equals(""));
  167. // System.out.println("s.trim().equals::" + s.trim().equals("") + ":'" + s + "'");
  168. // return (s == null || s.trim().equals(""));
  169. return (s == null || s.equals(""));
  170. }
  171. public static void WriteFile(String filename, String content) {
  172. PrintStream out = null;
  173. try {
  174. File file = new File(filename);
  175. if (!file.isAbsolute()) {
  176. filename = Paths.get(".").toAbsolutePath().normalize().toString() + File.separator + filename;
  177. file = new File(filename);
  178. }
  179. File dir = file.getParentFile();
  180. if (!dir.exists()) {
  181. Files.createDirectories(Paths.get(dir.getPath()));
  182. }
  183. out = new PrintStream(new FileOutputStream(filename));
  184. out.print(content);
  185. } catch (FileNotFoundException ex) {
  186. Logger.getLogger(Utils.class.getName()).log(Level.SEVERE, null, ex);
  187. } catch (IOException ex) {
  188. Logger.getLogger(Utils.class.getName()).log(Level.SEVERE, null, ex);
  189. } finally {
  190. if (out != null) {
  191. out.close();
  192. }
  193. }
  194. }
  195. public static Integer ResolveExpr(String op, String p1, String p2) throws Exception {
  196. return ResolveExpr(op, Integer.parseInt(p1), Integer.parseInt(p2));
  197. }
  198. public static Integer ResolveExpr(String op, Integer p1, Integer p2) {
  199. switch (op.substring(0, 1)) {
  200. case "+":
  201. return p1 + p2;
  202. case "-":
  203. return p1 - p2;
  204. case "*":
  205. return p1 * p2;
  206. case "/":
  207. return p1 / p2;
  208. case "%":
  209. return p1 % p2;
  210. case "<<":
  211. return p1 << p2;
  212. case ">>":
  213. return p1 >> p2;
  214. case "&":
  215. return p1 & p2;
  216. case "|":
  217. return p1 | p2;
  218. }
  219. return null;
  220. }
  221. public static boolean ResolveExprBool(String op, String p1, String p2) throws Exception {
  222. return ResolveExprBool(op, Integer.parseInt(p1), Integer.parseInt(p2));
  223. }
  224. public static boolean ResolveExprBool(String op, Integer p1, Integer p2) {
  225. switch (op) {
  226. case "<":
  227. return p1 < p2;
  228. case "<=":
  229. return p1 <= p2;
  230. case ">":
  231. return p1 > p2;
  232. case ">=":
  233. return p1 >= p2;
  234. case "==":
  235. return Objects.equals(p1, p2);
  236. case "!=":
  237. return !Objects.equals(p1, p2);
  238. }
  239. return false;
  240. }
  241. public static String ComplementOperation(String op) {
  242. return Complements.get(op);
  243. }
  244. protected static HashMap<String, String> Complements = new HashMap<String, String>() {
  245. {
  246. put("!=", "==");
  247. put("==", "!=");
  248. put("<", ">=");
  249. put("<=", ">");
  250. put(">", "<=");
  251. put(">=", "<");
  252. }
  253. };
  254. public static String FormatNum(String value, String format) {
  255. return FormatNum(Long.parseLong(value), format);
  256. }
  257. public static String FormatNum(long value, String format) {
  258. switch (format) {
  259. case "hex":
  260. return Long.toHexString(value);
  261. case "bin":
  262. return Long.toBinaryString(value);
  263. case "oct":
  264. return Long.toOctalString(value);
  265. }
  266. return value + "";
  267. }
  268. // public String hex(String value) {
  269. // return Integer.toHexString(Integer.parseInt(value));
  270. // }
  271. /**
  272. * Retorna uma lista de substrings do tamanho definido
  273. *
  274. * @param src - String a ser dividida
  275. * @param each - Quantidade de caracteres de cada parte
  276. * @return
  277. */
  278. public static String[] SplitEach(String src, int each) {
  279. return src.split("(?<=\\G.{" + each + "})");
  280. }
  281. public static long bin32ToDec(String bin) {
  282. String c = bin;
  283. bin = Utils.padPreserveSignal(32, bin);
  284. // System.out.println("bin32ToDec:" + bin + ">" + c);
  285. if (bin.charAt(0) == '0') {
  286. return Long.parseLong(bin, 2);
  287. } else {
  288. int number = 0;
  289. for (int index = 0; index < 32; index++) {
  290. int b = bin.charAt(index) - '0';
  291. number = number << 1 | b;
  292. }
  293. return (int) number;
  294. }
  295. }
  296. }