Utils.java 5.8 KB

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