Utils.java 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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. /**
  59. *
  60. * @param str
  61. * @param replace
  62. * @param direction (padRight = '1$-' | padLeft = '1$')
  63. * @param size
  64. * @return
  65. */
  66. public static String Pad(String str, String replace, String direction, int size) {
  67. if (size == 0) {
  68. return str;
  69. }
  70. return String.format("%" + direction + size + "s", str).replace(" ", replace);
  71. }
  72. public static String Pad(int size, String str) {
  73. if (size == 0) {
  74. return str;
  75. }
  76. return String.format("%" + size + "s", str).replace(' ', '0');
  77. }
  78. public static String padPreserveSignal(int size, String str) {
  79. if (size == 0) {
  80. return str;
  81. }
  82. return String.format("%" + size + "s", str).replace(' ', str.charAt(0));
  83. }
  84. public static String int2hex(int num) {
  85. return Integer.toHexString(num);
  86. // return Integer.valueOf(String.valueOf(n), 16);
  87. }
  88. public static String cut(int SizeBits, String paded) {
  89. int strSize = paded.length();
  90. return paded.substring(strSize - SizeBits, strSize);
  91. }
  92. public static String rTrim(String s) {
  93. return s.replaceAll("\\s+$", "");
  94. }
  95. public static String lTrim(String s) {
  96. return s.replaceAll("^\\s+", "");
  97. }
  98. /**
  99. *
  100. * @param s
  101. * @param remove ex: "|\"" para remover terminado com {"}
  102. * @return
  103. */
  104. public static String rTrim(String s, String remover) {
  105. return s.replaceAll("(\\s+" + remover + ")$", "");
  106. }
  107. /**
  108. *
  109. * @param s
  110. * @param remove ex: "|\"" para remover iniciado com {"}
  111. * @return
  112. */
  113. public static String lTrim(String s, String remover) {
  114. return s.replaceAll("^(\\s+" + remover + ")", "");
  115. }
  116. /**
  117. *
  118. * @param s
  119. * @param remove ex: "|\"" para remover iniciado e encerrado com {"}
  120. * @return
  121. */
  122. public static String trim(String s, String remover) {
  123. return Utils.rTrim(Utils.lTrim(s, remover), remover);
  124. }
  125. public static String charToInt(String atribuicao) {
  126. char chr = atribuicao.charAt(0);
  127. return "" + (int) chr;
  128. }
  129. public static String charToInt(char c) {
  130. return "" + (int) c;
  131. }
  132. public static int parseInt(String v) {
  133. return Integer.parseInt(v);
  134. }
  135. public static String Join(List<String> v, String delimiter) {
  136. String[] a = new String[v.size()];
  137. return Join(v.toArray(a), delimiter);
  138. }
  139. public static String Join(String[] v, String delimiter) {
  140. String result = "";
  141. int i = 0;
  142. for (; i < v.length - 1; i++) {
  143. result += v[i] + delimiter;
  144. }
  145. result += v[i];
  146. return result;
  147. }
  148. public static <T> T[] Concat(T[] first, T[] second) {
  149. T[] result = Arrays.copyOf(first, first.length + second.length);
  150. System.arraycopy(second, 0, result, first.length, second.length);
  151. return result;
  152. }
  153. public static String Repeat(String s, String times) {
  154. return Repeat(s, Integer.parseInt(times));
  155. }
  156. public static String Repeat(String s, int times) {
  157. StringBuilder builder = new StringBuilder();
  158. for (int i = 0; i < times; i++) {
  159. builder.append(s);
  160. }
  161. return builder.toString();
  162. }
  163. public static boolean Empty(String s) {
  164. // return (s == null || s.replaceAll("\\s+", "").equals(""));
  165. // System.out.println("s.trim().equals::" + s.trim().equals("") + ":'" + s + "'");
  166. // return (s == null || s.trim().equals(""));
  167. return (s == null || s.equals(""));
  168. }
  169. public static void WriteFile(String filename, String content) {
  170. PrintStream out = null;
  171. try {
  172. File file = new File(filename);
  173. if (!file.isAbsolute()) {
  174. filename = Paths.get(".").toAbsolutePath().normalize().toString() + File.separator + filename;
  175. file = new File(filename);
  176. }
  177. File dir = file.getParentFile();
  178. if (!dir.exists()) {
  179. Files.createDirectories(Paths.get(dir.getPath()));
  180. }
  181. out = new PrintStream(new FileOutputStream(filename));
  182. out.print(content);
  183. } catch (FileNotFoundException ex) {
  184. Logger.getLogger(Utils.class.getName()).log(Level.SEVERE, null, ex);
  185. } catch (IOException ex) {
  186. Logger.getLogger(Utils.class.getName()).log(Level.SEVERE, null, ex);
  187. } finally {
  188. if (out != null) {
  189. out.close();
  190. }
  191. }
  192. }
  193. public static Integer ResolveExpr(String op, String p1, String p2) throws Exception {
  194. return ResolveExpr(op, Integer.parseInt(p1), Integer.parseInt(p2));
  195. }
  196. public static Integer ResolveExpr(String op, Integer p1, Integer p2) {
  197. switch (op.substring(0, 1)) {
  198. case "+":
  199. return p1 + p2;
  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. }
  217. return null;
  218. }
  219. public static boolean ResolveExprBool(String op, String p1, String p2) throws Exception {
  220. return ResolveExprBool(op, Integer.parseInt(p1), Integer.parseInt(p2));
  221. }
  222. public static boolean ResolveExprBool(String op, Integer p1, Integer p2) {
  223. switch (op) {
  224. case "<":
  225. return p1 < p2;
  226. case "<=":
  227. return p1 <= p2;
  228. case ">":
  229. return p1 > p2;
  230. case ">=":
  231. return p1 >= p2;
  232. case "==":
  233. return Objects.equals(p1, p2);
  234. case "!=":
  235. return !Objects.equals(p1, p2);
  236. }
  237. return false;
  238. }
  239. public static String ComplementOperation(String op) {
  240. return Complements.get(op);
  241. }
  242. protected static HashMap<String, String> Complements = new HashMap<String, String>() {
  243. {
  244. put("!=", "==");
  245. put("==", "!=");
  246. put("<", ">=");
  247. put("<=", ">");
  248. put(">", "<=");
  249. put(">=", "<");
  250. }
  251. };
  252. public static String FormatNum(String value, String format) {
  253. return FormatNum(Long.parseLong(value), format);
  254. }
  255. public static String FormatNum(long value, String format) {
  256. switch (format) {
  257. case "hex":
  258. return Long.toHexString(value);
  259. case "bin":
  260. return Long.toBinaryString(value);
  261. case "oct":
  262. return Long.toOctalString(value);
  263. }
  264. return value + "";
  265. }
  266. // public String hex(String value) {
  267. // return Integer.toHexString(Integer.parseInt(value));
  268. // }
  269. }