/*
 * 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.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 *
 * @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;
    }

    public static Object 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 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(""));
    }

}
