package org.bitstrings.maven.plugins.portallocator;

import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;

public class PortAllocator
{
    public static class Builder
    {
        private final List<PortRange> portRanges = new LinkedList<>();

        public Builder port( int port )
        {
            portRanges.add( new PortRange( port ) );

            return this;
        }

        public Builder portRange( int lowest, int highest )
            throws IllegalArgumentException
        {
            portRanges.add( new PortRange( lowest, highest ) );

            return this;
        }

        public PortAllocator build()
        {
            return new PortAllocator( portRanges );
        }
    }

    public static class PortRange
    {
        private int lowest;
        private int highest;

        public PortRange( int port )
        {
            this( port, port );
        }

        public PortRange( int lowest, int highest )
            throws IllegalArgumentException
        {
            this.lowest = lowest;
            this.highest = highest;

            if ( ( lowest > highest ) || ( lowest < 0 ) )
            {
                throw new IllegalArgumentException( "Range must be valid, i.e.: lowest >= 0 and lowest <= highest." );
            }
        }

        public int getLowest()
        {
            return lowest;
        }

        public int getHighest()
        {
            return highest;
        }
    }

    private final List<PortRange> portRanges = new ArrayList<>();

    public PortAllocator( Collection<PortRange> portRanges )
    {
        this.portRanges.addAll( portRanges );
    }

    public PortAllocator() {}

    public void addPortRange( PortRange portRange )
    {
        portRanges.add( portRange );
    }

    public int next()
    {
        return 0;
    }
}

@@ -185,16 +185,193 @@
 cator%0A%7B%0A
+    private static final int LOWEST_PORT_DEFAULT = 1024;%0A    private static final int HIGHEST_PORT_DEFAULT = 65535;%0A    private static final int PREFERRED_PORT_DEFAULT = 8090;%0A%0A
     publ
@@ -1751,24 +1751,88 @@
 rayList%3C%3E();
+%0A    private int portRangesListIndex;%0A    private int portIndex;
 %0A%0A    public

add defaults