Repository: netty/netty
File: handler/src/main/java/io/netty/handler/ipfilter/IpSubnetFilter.java (Java)

Code before:
         *
         * In this case, we'll set `ipFilterRuleTypeIPv4` to `IpFilterRuleType.ACCEPT`.
         */
        if (numAcceptIPv4 == 0 && numRejectIPv4 > 0) {
            ipFilterRuleTypeIPv4 = IpFilterRuleType.REJECT;
        } else if (numAcceptIPv4 > 0 && numRejectIPv4 == 0) {
            ipFilterRuleTypeIPv4 = IpFilterRuleType.ACCEPT;
        } else {
            ipFilterRuleTypeIPv4 = null;
        }

        if (numAcceptIPv6 == 0 && numRejectIPv6 > 0) {
            ipFilterRuleTypeIPv6 = IpFilterRuleType.REJECT;
        } else if (numAcceptIPv6 > 0 && numRejectIPv6 == 0) {
            ipFilterRuleTypeIPv6 = IpFilterRuleType.ACCEPT;
        } else {
            ipFilterRuleTypeIPv6 = null;
        }

        this.ipv4Rules = unsortedIPv4Rules.isEmpty() ? null : sortAndFilter(unsortedIPv4Rules);
        this.ipv6Rules = unsortedIPv6Rules.isEmpty() ? null : sortAndFilter(unsortedIPv6Rules);
    }

    @Override
    protected boolean accept(ChannelHandlerContext ctx, InetSocketAddress remoteAddress) {
        if (ipv4Rules != null && remoteAddress.getAddress() instanceof Inet4Address) {
            int indexOf = Collections.binarySearch(ipv4Rules, remoteAddress, IpSubnetFilterRuleComparator.INSTANCE);
            if (indexOf >= 0) {
                if (ipFilterRuleTypeIPv4 == null) {
                    return ipv4Rules.get(indexOf).ruleType() == IpFilterRuleType.ACCEPT;
                } else {
                    return ipFilterRuleTypeIPv4 == IpFilterRuleType.ACCEPT;
                }
            }
        } else if (ipv6Rules != null) {
            int indexOf = Collections.binarySearch(ipv6Rules, remoteAddress, IpSubnetFilterRuleComparator.INSTANCE);
            if (indexOf >= 0) {
                if (ipFilterRuleTypeIPv6 == null) {
                    return ipv6Rules.get(indexOf).ruleType() == IpFilterRuleType.ACCEPT;
                } else {
                    return ipFilterRuleTypeIPv6 == IpFilterRuleType.ACCEPT;
                }
            }
        }

        return acceptIfNotFound;
    }

    /**
     * <ol>
     *     <li> Sort the list </li>

Review comment (performance):
It also makes sense to use an array instead of List for `ipv4Rules` and `ipv6Rules` -> less memory usage, and better performance.

Code after:
         *
         * In this case, we'll set `ipFilterRuleTypeIPv4` to `IpFilterRuleType.ACCEPT`.
         */
        if (numAcceptIPv4 == 0 && numRejectIPv4 > 0) {
            ipFilterRuleTypeIPv4 = IpFilterRuleType.REJECT;
        } else if (numAcceptIPv4 > 0 && numRejectIPv4 == 0) {
            ipFilterRuleTypeIPv4 = IpFilterRuleType.ACCEPT;
        } else {
            ipFilterRuleTypeIPv4 = null;
        }

        if (numAcceptIPv6 == 0 && numRejectIPv6 > 0) {
            ipFilterRuleTypeIPv6 = IpFilterRuleType.REJECT;
        } else if (numAcceptIPv6 > 0 && numRejectIPv6 == 0) {
            ipFilterRuleTypeIPv6 = IpFilterRuleType.ACCEPT;
        } else {
            ipFilterRuleTypeIPv6 = null;
        }

        this.ipv4Rules = unsortedIPv4Rules.isEmpty() ? null : sortAndFilter(unsortedIPv4Rules);
        this.ipv6Rules = unsortedIPv6Rules.isEmpty() ? null : sortAndFilter(unsortedIPv6Rules);
    }

    @Override
    protected boolean accept(ChannelHandlerContext ctx, InetSocketAddress remoteAddress) {
        if (ipv4Rules != null && remoteAddress.getAddress() instanceof Inet4Address) {
            int indexOf = Arrays.binarySearch(ipv4Rules, remoteAddress, IpSubnetFilterRuleComparator.INSTANCE);
            if (indexOf >= 0) {
                if (ipFilterRuleTypeIPv4 == null) {
                    return ipv4Rules[indexOf].ruleType() == IpFilterRuleType.ACCEPT;
                } else {
                    return ipFilterRuleTypeIPv4 == IpFilterRuleType.ACCEPT;
                }
            }
        } else if (ipv6Rules != null) {
            int indexOf = Arrays.binarySearch(ipv6Rules, remoteAddress, IpSubnetFilterRuleComparator.INSTANCE);
            if (indexOf >= 0) {
                if (ipFilterRuleTypeIPv6 == null) {
                    return ipv6Rules[indexOf].ruleType() == IpFilterRuleType.ACCEPT;
                } else {
                    return ipFilterRuleTypeIPv6 == IpFilterRuleType.ACCEPT;
                }
            }
        }

        return acceptIfNotFound;
    }

    /**
     * <ol>
     *     <li> Sort the list </li>