org.xnap.util
Class PortRange

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

public class PortRange
extends java.lang.Object

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

See Also:
xnap.util.prefs.PortRangeValidator

Nested Class Summary
static class PortRange.IntIterator
          An iterator over Integer objects.
 
Field Summary
static int MAX_PORT
           
static int MIN_PORT
           
 
Constructor Summary
PortRange()
           
PortRange(int first, int last)
           
PortRange(java.lang.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(java.lang.String range)
          Adds a range as a string.
 java.net.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.
 java.util.Iterator iterator()
          Returns an iterator over all contained ports.
 PortRange.IntIterator random()
          Returns an iterator over a shuffeled instance.
 int size()
          Returns the number of ports.
 java.lang.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(java.lang.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(java.lang.String range)
Adds a range as a string.

See Also:
xnap.util.prefs.PortRangeValidator

bindRandom

public java.net.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 java.util.Iterator iterator()
Returns an iterator over all contained ports.


random

public PortRange.IntIterator random()
Returns an iterator over a shuffeled instance.


size

public int size()
Returns the number of ports.


toString

public java.lang.String toString()
Returns an ordered string representation.

Overrides:
toString in class java.lang.Object


Copyright © 2001-2005 XNap Team. All Rights Reserved.