123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311 |
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package API;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.PrintStream;
- import java.nio.file.Files;
- import java.nio.file.Paths;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Objects;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- /**
- *
- * @author Eugenio
- */
- public class Utils {
- public static boolean isset(String[] data, int index) {
- try {
- String a;
- a = data[index];
- return true;
- } catch (ArrayIndexOutOfBoundsException e) {
- return false;
- }
- }
- public static boolean IsNumber(String string) {
- try {
- Integer.parseInt(string);
- } catch (Exception e) {
- return false;
- }
- return true;
- }
- public static String clearName(String nome) {
- return nome.split("\\[")[0].replace("*", "").replace("&", "");
- }
- public static ArrayList<String> arraypad(ArrayList<String> data, String string, int tam) {
- if (data.size() == tam) {
- return data;
- }
- for (int i = data.size(); i <= tam; i++) {
- data.add(string);
- }
- return data;
- }
- // protected String PAD(String value, String replace, int i) {
- // return String.format("%" + i + "s", value).replace(" ", replace);
- // }
- /**
- *
- * @param str
- * @param replace
- * @param direction (padRight = '1$-' | padLeft = '1$')
- * @param size
- * @return
- */
- public static String Pad(String str, String replace, String direction, int size) {
- if (size == 0) {
- return str;
- }
- return String.format("%" + direction + size + "s", str).replace(" ", replace);
- }
- public static String Pad(int size, String str) {
- if (size == 0) {
- return str;
- }
- return String.format("%" + size + "s", str).replace(' ', '0');
- }
- public static String padPreserveSignal(int size, String str) {
- if (size == 0) {
- return str;
- }
- return String.format("%" + size + "s", str).replace(' ', str.charAt(0));
- }
- public static String int2hex(int num) {
- return Integer.toHexString(num);
- // return Integer.valueOf(String.valueOf(n), 16);
- }
- public static String cut(int SizeBits, String paded) {
- int strSize = paded.length();
- return paded.substring(strSize - SizeBits, strSize);
- }
- public static String rTrim(String s) {
- return s.replaceAll("\\s+$", "");
- }
- public static String lTrim(String s) {
- return s.replaceAll("^\\s+", "");
- }
- /**
- *
- * @param s
- * @param remove ex: "|\"" para remover terminado com {"}
- * @return
- */
- public static String rTrim(String s, String remover) {
- return s.replaceAll("(\\s+" + remover + ")$", "");
- }
- /**
- *
- * @param s
- * @param remove ex: "|\"" para remover iniciado com {"}
- * @return
- */
- public static String lTrim(String s, String remover) {
- return s.replaceAll("^(\\s+" + remover + ")", "");
- }
- /**
- *
- * @param s
- * @param remove ex: "|\"" para remover iniciado e encerrado com {"}
- * @return
- */
- public static String trim(String s, String remover) {
- return Utils.rTrim(Utils.lTrim(s, remover), remover);
- }
- public static String charToInt(String atribuicao) {
- char chr = atribuicao.charAt(0);
- return "" + (int) chr;
- }
- public static String charToInt(char c) {
- return "" + (int) c;
- }
- public static int parseInt(String v) {
- return Integer.parseInt(v);
- }
- public static String Join(List<String> v, String delimiter) {
- String[] a = new String[v.size()];
- return Join(v.toArray(a), delimiter);
- }
- public static String Join(String[] v, String delimiter) {
- String result = "";
- int i = 0;
- for (; i < v.length - 1; i++) {
- result += v[i] + delimiter;
- }
- result += v[i];
- return result;
- }
- public static <T> T[] Concat(T[] first, T[] second) {
- T[] result = Arrays.copyOf(first, first.length + second.length);
- System.arraycopy(second, 0, result, first.length, second.length);
- return result;
- }
- public static String Repeat(String s, String times) {
- return Repeat(s, Integer.parseInt(times));
- }
- public static String Repeat(String s, int times) {
- StringBuilder builder = new StringBuilder();
- for (int i = 0; i < times; i++) {
- builder.append(s);
- }
- return builder.toString();
- }
- public static boolean Empty(String s) {
- // return (s == null || s.replaceAll("\\s+", "").equals(""));
- // System.out.println("s.trim().equals::" + s.trim().equals("") + ":'" + s + "'");
- // return (s == null || s.trim().equals(""));
- return (s == null || s.equals(""));
- }
- public static void WriteFile(String filename, String content) {
- PrintStream out = null;
- try {
- File file = new File(filename);
- if (!file.isAbsolute()) {
- filename = Paths.get(".").toAbsolutePath().normalize().toString() + File.separator + filename;
- file = new File(filename);
- }
- File dir = file.getParentFile();
- if (!dir.exists()) {
- Files.createDirectories(Paths.get(dir.getPath()));
- }
- out = new PrintStream(new FileOutputStream(filename));
- out.print(content);
- } catch (FileNotFoundException ex) {
- Logger.getLogger(Utils.class.getName()).log(Level.SEVERE, null, ex);
- } catch (IOException ex) {
- Logger.getLogger(Utils.class.getName()).log(Level.SEVERE, null, ex);
- } finally {
- if (out != null) {
- out.close();
- }
- }
- }
- public static Integer ResolveExpr(String op, String p1, String p2) throws Exception {
- return ResolveExpr(op, Integer.parseInt(p1), Integer.parseInt(p2));
- }
- public static Integer ResolveExpr(String op, Integer p1, Integer p2) {
- switch (op.substring(0, 1)) {
- case "+":
- return p1 + p2;
- case "-":
- return p1 - p2;
- case "*":
- return p1 * p2;
- case "/":
- return p1 / p2;
- case "%":
- return p1 % p2;
- case "<<":
- return p1 << p2;
- case ">>":
- return p1 >> p2;
- case "&":
- return p1 & p2;
- case "|":
- return p1 | p2;
- }
- return null;
- }
- public static boolean ResolveExprBool(String op, String p1, String p2) throws Exception {
- return ResolveExprBool(op, Integer.parseInt(p1), Integer.parseInt(p2));
- }
- public static boolean ResolveExprBool(String op, Integer p1, Integer p2) {
- switch (op) {
- case "<":
- return p1 < p2;
- case "<=":
- return p1 <= p2;
- case ">":
- return p1 > p2;
- case ">=":
- return p1 >= p2;
- case "==":
- return Objects.equals(p1, p2);
- case "!=":
- return !Objects.equals(p1, p2);
- }
- return false;
- }
- public static String ComplementOperation(String op) {
- return Complements.get(op);
- }
- protected static HashMap<String, String> Complements = new HashMap<String, String>() {
- {
- put("!=", "==");
- put("==", "!=");
- put("<", ">=");
- put("<=", ">");
- put(">", "<=");
- put(">=", "<");
- }
- };
- public static String FormatNum(String value, String format) {
- return FormatNum(Long.parseLong(value), format);
- }
- public static String FormatNum(long value, String format) {
- switch (format) {
- case "hex":
- return Long.toHexString(value);
- case "bin":
- return Long.toBinaryString(value);
- case "oct":
- return Long.toOctalString(value);
- }
- return value + "";
- }
- // public String hex(String value) {
- // return Integer.toHexString(Integer.parseInt(value));
- // }
- }
|