/* * 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 arraypad(ArrayList 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 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[] 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 Complements = new HashMap() { { 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)); // } }