static Range ipToRangeIfPossible()

in src/main/software/amazon/event/ruler/CIDR.java [82:111]


    static Range ipToRangeIfPossible(final String ip) {
        if (!isIPv4OrIPv6(ip)) {
            return null;
        }
        try {
            final byte[] digits = toHexDigits(ipToBytes(ip));
            final byte[] bottom = digits.clone();
            final byte[] top = digits.clone();
            boolean openBottom;
            boolean openTop;
            byte lastByte = top[top.length - 1];

            if (lastByte == Constants.MAX_HEX_DIGIT) {
                bottom[top.length - 1] = (byte) (lastByte - 1);
                openBottom = true;
                openTop = false;
            } else {
                if (lastByte != HEX_DIGITS[9]) { // 9
                    top[top.length - 1] = (byte) (lastByte + 1);
                } else {
                    top[top.length - 1] = HEX_DIGITS[10];
                }
                openBottom = false;
                openTop = true;
            }
            return new Range(bottom, openBottom, top, openTop, true);
        } catch (Exception e) {
            return null;
        }
    }