Utils.java 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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.logging.Level;
  19. import java.util.logging.Logger;
  20. /**
  21. *
  22. * @author Eugenio
  23. */
  24. public class Utils {
  25. public static boolean isset(String[] data, int index) {
  26. try {
  27. String a;
  28. a = data[index];
  29. return true;
  30. } catch (ArrayIndexOutOfBoundsException e) {
  31. return false;
  32. }
  33. }
  34. public static boolean isNumber(String string) {
  35. try {
  36. Integer.parseInt(string);
  37. } catch (Exception e) {
  38. return false;
  39. }
  40. return true;
  41. }
  42. public static String clearName(String nome) {
  43. return nome.split("\\[")[0].replace("*", "").replace("&", "");
  44. }
  45. public static ArrayList<String> arraypad(ArrayList<String> data, String string, int tam) {
  46. if (data.size() == tam) {
  47. return data;
  48. }
  49. for (int i = data.size(); i <= tam; i++) {
  50. data.add(string);
  51. }
  52. return data;
  53. }
  54. public static Object Pad(String str, String replace, String direction, int size) {
  55. if (size == 0) {
  56. return str;
  57. }
  58. return String.format("%" + direction + size + "s", str).replace(" ", replace);
  59. }
  60. public static String pad(int size, String str) {
  61. if (size == 0) {
  62. return str;
  63. }
  64. return String.format("%" + size + "s", str).replace(' ', '0');
  65. }
  66. public static String padPreserveSignal(int size, String str) {
  67. if (size == 0) {
  68. return str;
  69. }
  70. return String.format("%" + size + "s", str).replace(' ', str.charAt(0));
  71. }
  72. public static String int2hex(int num) {
  73. return Integer.toHexString(num);
  74. // return Integer.valueOf(String.valueOf(n), 16);
  75. }
  76. public static String cut(int SizeBits, String paded) {
  77. int strSize = paded.length();
  78. return paded.substring(strSize - SizeBits, strSize);
  79. }
  80. public static String rTrim(String s) {
  81. return s.replaceAll("\\s+$", "");
  82. }
  83. public static String lTrim(String s) {
  84. return s.replaceAll("^\\s+", "");
  85. }
  86. /**
  87. *
  88. * @param s
  89. * @param remove ex: "|\"" para remover terminado com {"}
  90. * @return
  91. */
  92. public static String rTrim(String s, String remover) {
  93. return s.replaceAll("(\\s+" + remover + ")$", "");
  94. }
  95. /**
  96. *
  97. * @param s
  98. * @param remove ex: "|\"" para remover iniciado com {"}
  99. * @return
  100. */
  101. public static String lTrim(String s, String remover) {
  102. return s.replaceAll("^(\\s+" + remover + ")", "");
  103. }
  104. /**
  105. *
  106. * @param s
  107. * @param remove ex: "|\"" para remover iniciado e encerrado com {"}
  108. * @return
  109. */
  110. public static String trim(String s, String remover) {
  111. return Utils.rTrim(Utils.lTrim(s, remover), remover);
  112. }
  113. public static String charToInt(String atribuicao) {
  114. char chr = atribuicao.charAt(0);
  115. return "" + (int) chr;
  116. }
  117. public static String charToInt(char c) {
  118. return "" + (int) c;
  119. }
  120. public static int parseInt(String v) {
  121. return Integer.parseInt(v);
  122. }
  123. public static String Join(List<String> v, String delimiter) {
  124. String[] a = new String[v.size()];
  125. return Join(v.toArray(a), delimiter);
  126. }
  127. public static String Join(String[] v, String delimiter) {
  128. String result = "";
  129. int i = 0;
  130. for (; i < v.length - 1; i++) {
  131. result += v[i] + delimiter;
  132. }
  133. result += v[i];
  134. return result;
  135. }
  136. public static <T> T[] Concat(T[] first, T[] second) {
  137. T[] result = Arrays.copyOf(first, first.length + second.length);
  138. System.arraycopy(second, 0, result, first.length, second.length);
  139. return result;
  140. }
  141. public static String Repeat(String s, String times) {
  142. return Repeat(s, Integer.parseInt(times));
  143. }
  144. public static String Repeat(String s, int times) {
  145. StringBuilder builder = new StringBuilder();
  146. for (int i = 0; i < times; i++) {
  147. builder.append(s);
  148. }
  149. return builder.toString();
  150. }
  151. public static boolean Empty(String s) {
  152. // return (s == null || s.replaceAll("\\s+", "").equals(""));
  153. // System.out.println("s.trim().equals::" + s.trim().equals("") + ":'" + s + "'");
  154. // return (s == null || s.trim().equals(""));
  155. return (s == null || s.equals(""));
  156. }
  157. public static void WriteFile(String filename, String content) {
  158. PrintStream out = null;
  159. try {
  160. File file = new File(filename);
  161. if (!file.isAbsolute()) {
  162. filename = Paths.get(".").toAbsolutePath().normalize().toString() + File.separator + filename;
  163. file = new File(filename);
  164. }
  165. File dir = file.getParentFile();
  166. if (!dir.exists()) {
  167. Files.createDirectories(Paths.get(dir.getPath()));
  168. }
  169. out = new PrintStream(new FileOutputStream(filename));
  170. out.print(content);
  171. } catch (FileNotFoundException ex) {
  172. Logger.getLogger(Utils.class.getName()).log(Level.SEVERE, null, ex);
  173. } catch (IOException ex) {
  174. Logger.getLogger(Utils.class.getName()).log(Level.SEVERE, null, ex);
  175. } finally {
  176. if (out != null) {
  177. out.close();
  178. }
  179. }
  180. }
  181. public static Integer ResolveExpr(String op, String p1, String p2) throws Exception {
  182. return ResolveExpr(op, Integer.parseInt(p1), Integer.parseInt(p2));
  183. }
  184. public static Integer ResolveExpr(String op, Integer p1, Integer p2) {
  185. switch (op.substring(0, 1)) {
  186. case "+":
  187. return p1 + p2;
  188. case "-":
  189. return p1 - p2;
  190. case "*":
  191. return p1 * p2;
  192. case "/":
  193. return p1 / p2;
  194. case "%":
  195. return p1 % p2;
  196. case "<<":
  197. return p1 << p2;
  198. case ">>":
  199. return p1 >> p2;
  200. case "&":
  201. return p1 & p2;
  202. case "|":
  203. return p1 | p2;
  204. }
  205. return null;
  206. }
  207. public static String ComplementOperation(String op) {
  208. return Complements.get(op);
  209. }
  210. protected static HashMap<String, String> Complements = new HashMap<String, String>() {
  211. {
  212. put("!=", "==");
  213. put("==", "!=");
  214. put("<", ">=");
  215. put("<=", ">");
  216. put(">", "<=");
  217. put(">=", "<");
  218. }
  219. };
  220. }