org.xnap.commons.util
Class PortRange

java.lang.Object
  extended by org.xnap.commons.util.PortRange

public class PortRange
extends Object

Provides a data container for port ranges. All methods make sure that only valid ports are added.

See Also:
PortRangeValidator

Field Summary
static int MAX_PORT
           
static int MIN_PORT
           
 
Constructor Summary
PortRange()
           
PortRange(int first, int last)
           
PortRange(String range)
           
 
Method Summary
 void add(int port)
          Adds port to the range.
 void add(int first, int last)
          Adds all ports that are first <= port <= last.
 void add(String range)
          Adds a range as a string.
 ServerSocket bindRandom()
          Tries to bind random ports from the range and returns the ServerSocket object if successful.
 boolean contains(int port)
          Returns true, if port is in the range.
 int getRandom(int defaultPort)
          Returns a random port from the range.
 Iterator<Integer> iterator()
          Returns an iterator over all contained ports.
 Iterator<Integer> randomIterator()
          Returns an iterator over a shuffeled instance.
 int size()
          Returns the number of ports.
 String toString()
          Returns an ordered string representation.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

MIN_PORT

public static final int MIN_PORT
See Also:
Constant Field Values

MAX_PORT

public static final int MAX_PORT
See Also:
Constant Field Values
Constructor Detail

PortRange

public PortRange(int first,
                 int last)

PortRange

public PortRange(String range)

PortRange

public PortRange()
Method Detail

add

public void add(int first,
                int last)
Adds all ports that are first <= port <= last.


add

public void add(int port)
Adds port to the range.


add

public void add(String range)
Adds a range as a string.

See Also:
PortRangeValidator

bindRandom

public ServerSocket bindRandom()
Tries to bind random ports from the range and returns the ServerSocket object if successful.

Returns:
null, if no port could be bound

contains

public boolean contains(int port)
Returns true, if port is in the range.


getRandom

public int getRandom(int defaultPort)
Returns a random port from the range.

Parameters:
defaultPort - a default port
Returns:
default, if range is empty

iterator

public Iterator<Integer> iterator()
Returns an iterator over all contained ports.


randomIterator

public Iterator<Integer> randomIterator()
Returns an iterator over a shuffeled instance.


size

public int size()
Returns the number of ports.


toString

public String toString()
Returns an ordered string representation.

Overrides:
toString in class Object


Copyright © 2004-2007 XNap Commons Team. All Rights Reserved.