Skip to content

Instantly share code, notes, and snippets.

@andhikayuana
Created February 2, 2018 06:45
Show Gist options
  • Save andhikayuana/b9f97d5fc8bb64557e8b49d82b184ada to your computer and use it in GitHub Desktop.
Save andhikayuana/b9f97d5fc8bb64557e8b49d82b184ada to your computer and use it in GitHub Desktop.
Some Util

paginate List

    public static <T> List<List<T>> getPaginate(Collection<T> c, Integer pageSize) {
        if (c == null) return Collections.emptyList();
        List<T> list = new ArrayList<T>(c);
        if (pageSize == null || pageSize <= 0 || pageSize > list.size()) pageSize = list.size();
        int numPages = (int) Math.ceil((double) list.size() / (double) pageSize);
        List<List<T>> pages = new ArrayList<List<T>>(numPages);
        for (int pageNum = 0; pageNum < numPages; ) {
            pages.add(list.subList(pageNum * pageSize, Math.min(++pageNum * pageSize, list.size())));
        }
        return pages;
    }

normalize phone number

    public static String normalizePhoneNumber(String number, String countryCode) {
        number = number.replaceAll("[^+0-9]", ""); // All weird characters such as /, -, ...
        if (isNumberWithZeroFirst(number)) {
            number = "+" + countryCode + number.substring(1); // e.g. 0172 12 34 567 -> + (countryCode) 172 12 34 567
        }
        number = number.replaceAll("^[0]{1,4}", "+"); // e.g. 004912345678 -> +4912345678
        return number;
    }

    public static String normalizePhoneNumber(String number) {
        String countryCode = "62"; // Default Indonesia
        return normalizePhoneNumber(number, countryCode);
    }

    public static boolean isNumberWithZeroFirst(String number) {
        return number.substring(0, 1).compareTo("0") == 0 && number.substring(1, 2).compareTo("0") != 0;
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment