package net.ripe.commons.ip.utils;

import static java.math.BigInteger.*;
import static net.ripe.commons.ip.resource.Ipv6.*;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import net.ripe.commons.ip.range.Ipv6Range;

public class PrefixUtils {
    //TODO change to work for both Ipv6/Ipv4, add tests for Ipv4

    public static boolean isValidPrefix(Ipv6Range range) {
        int maxContainedPrefix = getMaxContainedPrefix(range.size());
        return getPrefixSize(maxContainedPrefix).compareTo(range.size()) == 0;
    }
    
    public static int getPrefixLength(Ipv6Range range) {
        int maxContainedPrefix = getMaxContainedPrefix(range.size());
        if (getPrefixSize(maxContainedPrefix).compareTo(range.size()) == 0) {
            return maxContainedPrefix;
        } else {
            throw new IllegalArgumentException(range + " is not a valid prefix, cannot get prefix length!");
        }
    }

    public static List<Ipv6Range> splitIntoPrefixes(Ipv6Range range) {
        BigInteger dynamicStart = range.start().value();
        List<Ipv6Range> result = new ArrayList<Ipv6Range>();

        while (dynamicStart.compareTo(range.end().value()) <= 0) {
            BigInteger availableSize = range.end().value().subtract(dynamicStart).add(ONE);
            int prefixToCut = getBiggestPossiblePrefix(dynamicStart, availableSize);

            BigInteger dynamicEnd = dynamicStart.add(getPrefixSize(prefixToCut)).subtract(ONE);
            Ipv6Range cutPrefix = Ipv6Range.from(dynamicStart).to(dynamicEnd);
            result.add(cutPrefix);

            dynamicStart = dynamicEnd.add(ONE);
        }

        return result;
    }

    private static BigInteger getPrefixSize(int prefixLength) {
        return BigInteger.ONE.shiftLeft(IPv6_NUMBER_OF_BITS - prefixLength);
    }

    private static int getBiggestPossiblePrefix(BigInteger start, BigInteger size) {
        int maxValidPrefix = getMaxValidPrefix(start);
        int maxContainedPrefix = getMaxContainedPrefix(size);
        return (maxValidPrefix < maxContainedPrefix) ? maxContainedPrefix : maxValidPrefix;
    }

    private static int getMaxValidPrefix(BigInteger number) {
        int powerOfTwo = 0;
        int maxPowerOfTwo = powerOfTwo;

        while (powerOfTwo <= IPv6_NUMBER_OF_BITS && number.divideAndRemainder(ONE.shiftLeft(powerOfTwo))[1].compareTo(ZERO) == 0) {
            maxPowerOfTwo = powerOfTwo;
            powerOfTwo++;
        }
        return IPv6_NUMBER_OF_BITS - maxPowerOfTwo;
    }

    private static int getMaxContainedPrefix(BigInteger number) {
        int powerOfTwo = 0;
        int maxPowerOfTwo = powerOfTwo;

        while (powerOfTwo <= IPv6_NUMBER_OF_BITS && number.compareTo(ONE.shiftLeft(powerOfTwo)) >= 0) {
            maxPowerOfTwo = powerOfTwo;
            powerOfTwo++;
        }
        return IPv6_NUMBER_OF_BITS - maxPowerOfTwo;
    }

}

@@ -241,16 +241,58 @@
 v6Range;
+%0Aimport org.apache.commons.lang3.Validate;
 %0A%0Apublic
@@ -736,11 +736,23 @@
     
-if 
+Validate.isTrue
 (get
@@ -814,186 +814,148 @@
 == 0
-) %7B%0A            return maxContainedPrefix;%0A        %7D else %7B%0A            throw new IllegalArgumentException(range + %22 is not a valid prefix, cannot get prefix length!%22);%0A        %7D
+, String.format(%22%25s is not a valid prefix, cannot get prefix length!%22, range.toStringInRangeNotation()));%0A        return maxContainedPrefix;
 %0A   

Use apache Validate in getPrefixLength.