Skip to content

Commit

Permalink
Use an ArrayList instead of reimplementing it
Browse files Browse the repository at this point in the history
This should not change performance but is way more readable

Signed-off-by: Etienne CHAMPETIER <champetier.etienne@gmail.com>
  • Loading branch information
champtar committed Apr 26, 2016
1 parent 249ebb9 commit 8de8d51
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 94 deletions.
Expand Up @@ -40,8 +40,7 @@ public class MultiplexingDatagramSocket
*/
private final MultiplexingXXXSocketSupport<MultiplexedDatagramSocket>
multiplexingXXXSocketSupport
= new MultiplexingXXXSocketSupport<MultiplexedDatagramSocket>(
MultiplexedDatagramSocket.class)
= new MultiplexingXXXSocketSupport<MultiplexedDatagramSocket>()
{
/**
* {@inheritDoc}
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/org/ice4j/socket/MultiplexingSocket.java
Expand Up @@ -53,8 +53,7 @@ public class MultiplexingSocket
*/
private final MultiplexingXXXSocketSupport<MultiplexedSocket>
multiplexingXXXSocketSupport
= new MultiplexingXXXSocketSupport<MultiplexedSocket>(
MultiplexedSocket.class)
= new MultiplexingXXXSocketSupport<MultiplexedSocket>()
{
/**
* {@inheritDoc}
Expand Down
97 changes: 7 additions & 90 deletions src/main/java/org/ice4j/socket/MultiplexingXXXSocketSupport.java
Expand Up @@ -41,34 +41,6 @@ abstract class MultiplexingXXXSocketSupport
private static final Logger logger
= Logger.getLogger(MultiplexingXXXSocketSupport.class.getName());

/**
* Adds/appends a new element to an existing array.
*
* @param <T> the component type of {@code array} and the type of
* {@code element}
* @param array the array to which {@code element} is to be added/appended
* @param element the new element to add/append to {@code array}
* @return an array which is a concatenation of {@code array} and
* {@code element}
*/
static <T> T[] add(T[] array, T element)
{
int length = array.length;
@SuppressWarnings("unchecked")
T[] newArray
= (T[])
Array.newInstance(
array.getClass().getComponentType(),
length + 1);

if (length != 0)
{
System.arraycopy(array, 0, newArray, 0, length);
}
newArray[length] = element;
return newArray;
}

/**
* Initializes a new <tt>DatagramPacket</tt> instance which is a clone of a
* specific <tt>DatagramPacket</tt> i.e. the properties of the clone
Expand Down Expand Up @@ -225,18 +197,6 @@ else if (logger.isLoggable(Level.WARNING))
*/
private boolean inReceive = false;

/**
* The {@code Class} of the multiplexed sockets supported by this instance.
*/
private final Class<MultiplexedXXXSocketT> multiplexedXXXSocketClass;

/**
* The constant which represents an empty array with
* <tt>MultiplexedDatagramSocket</tt> element type. Explicitly defined in
* order to reduce allocations.
*/
private final MultiplexedXXXSocketT[] NO_SOCKETS;

/**
* The value with which {@link DatagramSocket#setReceiveBufferSize(int)} is
* to be invoked if {@link #setReceiveBufferSize} is <tt>true</tt>.
Expand All @@ -259,29 +219,13 @@ else if (logger.isLoggable(Level.WARNING))
* The IP sockets filtering {@code DatagramPacket}s away from this IP
* socket.
*/
private MultiplexedXXXSocketT[] sockets;

/**
* The {@code Object} which synchronizes the access to the {@link #sockets}
* field of this instance.
*/
private final Object socketsSyncRoot = new Object();
private final List<MultiplexedXXXSocketT> sockets = new ArrayList<>();

/**
* Initializes a new {@code MultiplexingXXXSocketSupport} instance.
*
* @param multiplexedXXXSocketClass the {@code Class} of the multiplexed
* sockets supported by the new instance
*/
protected MultiplexingXXXSocketSupport(
Class<MultiplexedXXXSocketT> multiplexedXXXSocketClass)
protected MultiplexingXXXSocketSupport()
{
this.multiplexedXXXSocketClass = multiplexedXXXSocketClass;

NO_SOCKETS
= (MultiplexedXXXSocketT[])
Array.newInstance(multiplexedXXXSocketClass, 0);
sockets = NO_SOCKETS;
}

/**
Expand All @@ -295,7 +239,7 @@ protected MultiplexingXXXSocketSupport(
*/
private void acceptBySocketsOrThis(DatagramPacket p)
{
synchronized (socketsSyncRoot)
synchronized (sockets)
{
boolean accepted = false;

Expand Down Expand Up @@ -338,36 +282,9 @@ private void acceptBySocketsOrThis(DatagramPacket p)
*/
void close(MultiplexedXXXSocketT multiplexed)
{
synchronized (socketsSyncRoot)
synchronized (sockets)
{
int socketCount = sockets.length;

for (int i = 0; i < socketCount; i++)
{
if (sockets[i].equals(multiplexed))
{
if (socketCount == 1)
{
sockets = NO_SOCKETS;
}
else
{
MultiplexedXXXSocketT[] newSockets
= (MultiplexedXXXSocketT[])
Array.newInstance(
multiplexedXXXSocketClass,
socketCount - 1);

System.arraycopy(sockets, 0, newSockets, 0, i);
System.arraycopy(
sockets, i + 1,
newSockets, i,
newSockets.length - i);
sockets = newSockets;
}
break;
}
}
sockets.remove(multiplexed);
}
}

Expand Down Expand Up @@ -497,7 +414,7 @@ public MultiplexedXXXSocketT getSocket(
if (filter == null)
throw new NullPointerException("filter");

synchronized (socketsSyncRoot)
synchronized (sockets)
{
// If a socket for the specified filter exists already, do not
// create a new one and return the existing.
Expand All @@ -516,7 +433,7 @@ public MultiplexedXXXSocketT getSocket(
// Remember the new socket.
if (socket != null)
{
sockets = add(sockets, socket);
sockets.add(socket);

// A multiplexed socket may be created after packets matching
// its filter have been received. Pull them out of the
Expand Down

0 comments on commit 8de8d51

Please sign in to comment.