Package org.jboss.netty.channel
Class ChannelLocal<T>
- java.lang.Object
-
- org.jboss.netty.channel.ChannelLocal<T>
-
- All Implemented Interfaces:
java.lang.Iterable<java.util.Map.Entry<Channel,T>>
public class ChannelLocal<T> extends java.lang.Object implements java.lang.Iterable<java.util.Map.Entry<Channel,T>>
A global variable that is local to aChannel. Think of this as a variation ofThreadLocalwhose key is aChannelrather than aThread.currentThread(). One difference is that you always have to specify theChannelto access the variable.Alternatively, you might want to use the
ChannelHandlerContext.attachmentproperty, which performs better.
-
-
Field Summary
Fields Modifier and Type Field Description private java.util.concurrent.ConcurrentMap<Channel,T>mapprivate booleanremoveOnCloseprivate ChannelFutureListenerremover
-
Constructor Summary
Constructors Constructor Description ChannelLocal()ChannelLocal(boolean removeOnClose)Creates aChannellocal variable.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description Tget(Channel channel)Returns the value of this variable.protected TinitialValue(Channel channel)Returns the initial value of the variable.java.util.Iterator<java.util.Map.Entry<Channel,T>>iterator()Returns a read-onlyIteratorthat holds allMap.Entry's of this ChannelLocalTremove(Channel channel)Removes the variable and returns the removed value.Tset(Channel channel, T value)Sets the value of this variable.TsetIfAbsent(Channel channel, T value)Sets the value of this variable only when no value was set.
-
-
-
Field Detail
-
remover
private final ChannelFutureListener remover
-
removeOnClose
private final boolean removeOnClose
-
-
Constructor Detail
-
ChannelLocal
public ChannelLocal()
-
ChannelLocal
public ChannelLocal(boolean removeOnClose)
Creates aChannellocal variable.- Parameters:
removeOnClose- iftruetheChannelLocalwill remove aChannelfrom it own once theChannelwas closed.
-
-
Method Detail
-
initialValue
protected T initialValue(Channel channel)
Returns the initial value of the variable. By default, it returnsnull. Override it to change the initial value.- Parameters:
channel- the channel where this local variable is accessed with
-
set
public T set(Channel channel, T value)
Sets the value of this variable.- Returns:
- the old value.
nullif there was no old value.
-
setIfAbsent
public T setIfAbsent(Channel channel, T value)
Sets the value of this variable only when no value was set.- Returns:
nullif the specified value was set. An existing value if failed to set.
-
remove
public T remove(Channel channel)
Removes the variable and returns the removed value. If no value was set, this method returns the return value ofinitialValue(Channel), which isnullby default.- Returns:
- the removed value.
an initial value (by default
null) if no value was set.
-
-