Add a shortcut method for collision-free naming

This commit is contained in:
Trustin Lee 2013-10-25 20:01:31 +09:00
parent b533a1361b
commit 499033d44f
15 changed files with 95 additions and 36 deletions

View File

@ -61,7 +61,7 @@ public class WebSocketServerProtocolHandler extends WebSocketProtocolHandler {
}
private static final AttributeKey<WebSocketServerHandshaker> HANDSHAKER_ATTR_KEY =
AttributeKey.valueOf(WebSocketServerHandshaker.class.getName() + ".HANDSHAKER");
AttributeKey.valueOf(WebSocketServerHandshaker.class, "HANDSHAKER");
private final String websocketPath;
private final String subprotocols;

View File

@ -19,8 +19,8 @@ import io.netty.util.Signal;
public class DecoderResult {
protected static final Signal SIGNAL_UNFINISHED = Signal.valueOf(DecoderResult.class.getName() + ".UNFINISHED");
protected static final Signal SIGNAL_SUCCESS = Signal.valueOf(DecoderResult.class.getName() + ".SUCCESS");
protected static final Signal SIGNAL_UNFINISHED = Signal.valueOf(DecoderResult.class, "UNFINISHED");
protected static final Signal SIGNAL_SUCCESS = Signal.valueOf(DecoderResult.class, "SUCCESS");
public static final DecoderResult UNFINISHED = new DecoderResult(SIGNAL_UNFINISHED);
public static final DecoderResult SUCCESS = new DecoderResult(SIGNAL_SUCCESS);

View File

@ -266,7 +266,7 @@ import java.util.List;
*/
public abstract class ReplayingDecoder<S> extends ByteToMessageDecoder {
static final Signal REPLAY = Signal.valueOf(ReplayingDecoder.class.getName() + ".REPLAY");
static final Signal REPLAY = Signal.valueOf(ReplayingDecoder.class, "REPLAY");
private final ReplayingDecoderBuffer replayable = new ReplayingDecoderBuffer();
private S state;

View File

@ -35,7 +35,7 @@ import org.jboss.marshalling.Unmarshaller;
public class ContextBoundUnmarshallerProvider extends DefaultUnmarshallerProvider {
private static final AttributeKey<Unmarshaller> UNMARSHALLER = AttributeKey.valueOf(
ContextBoundUnmarshallerProvider.class.getName() + ".UNMARSHALLER");
ContextBoundUnmarshallerProvider.class, "UNMARSHALLER");
public ContextBoundUnmarshallerProvider(MarshallerFactory factory, MarshallingConfiguration config) {
super(factory, config);

View File

@ -37,7 +37,7 @@ import java.io.Serializable;
public class CompatibleObjectEncoder extends MessageToByteEncoder<Serializable> {
private static final AttributeKey<ObjectOutputStream> OOS =
AttributeKey.valueOf(CompatibleObjectEncoder.class.getName() + ".OOS");
AttributeKey.valueOf(CompatibleObjectEncoder.class, "OOS");
private final int resetInterval;
private int writtenObjects;

View File

@ -39,6 +39,14 @@ public final class AttributeKey<T> extends AbstractConstant<AttributeKey<T>> {
return (AttributeKey<T>) pool.valueOf(name);
}
/**
* Shortcut of {@link #valueOf(String) valueOf(firstNameComponent.getName() + "#" + secondNameComponent)}.
*/
@SuppressWarnings("unchecked")
public static <T> AttributeKey<T> valueOf(Class<?> firstNameComponent, String secondNameComponent) {
return (AttributeKey<T>) pool.valueOf(firstNameComponent, secondNameComponent);
}
private AttributeKey(int id, String name) {
super(id, name);
}

View File

@ -31,13 +31,36 @@ public abstract class ConstantPool<T extends Constant<T>> {
private int nextId = 1;
/**
* Creates a new or the existing {@link Constant} mapped to the specified {@code name}.
* Shortcut of {@link #valueOf(String) valueOf(firstNameComponent.getName() + "#" + secondNameComponent)}.
*/
public T valueOf(Class<?> firstNameComponent, String secondNameComponent) {
if (firstNameComponent == null) {
throw new NullPointerException("firstNameComponent");
}
if (secondNameComponent == null) {
throw new NullPointerException("secondNameComponent");
}
return valueOf(firstNameComponent.getName() + '#' + secondNameComponent);
}
/**
* Returns the {@link Constant} which is assigned to the specified {@code name}.
* If there's no such {@link Constant}, a new one will be created and returned.
* Once created, the subsequent calls with the same {@code name} will always return the previously created one
* (i.e. singleton.)
*
* @param name the name of the {@link Constant}
*/
public T valueOf(String name) {
if (name == null) {
throw new NullPointerException("name");
}
if (name.isEmpty()) {
throw new IllegalArgumentException("empty name");
}
synchronized (constants) {
T c = constants.get(name);
if (c == null) {

View File

@ -38,6 +38,13 @@ public final class Signal extends Error implements Constant<Signal> {
return pool.valueOf(name);
}
/**
* Shortcut of {@link #valueOf(String) valueOf(firstNameComponent.getName() + "#" + secondNameComponent)}.
*/
public static Signal valueOf(Class<?> firstNameComponent, String secondNameComponent) {
return pool.valueOf(firstNameComponent, secondNameComponent);
}
private final int id;
private final String name;

View File

@ -41,8 +41,8 @@ public class DefaultPromise<V> extends AbstractFuture<V> implements Promise<V> {
return 0;
}
};
private static final Signal SUCCESS = Signal.valueOf(DefaultPromise.class.getName() + ".SUCCESS");
private static final Signal UNCANCELLABLE = Signal.valueOf(DefaultPromise.class.getName() + ".UNCANCELLABLE");
private static final Signal SUCCESS = Signal.valueOf(DefaultPromise.class, "SUCCESS");
private static final Signal UNCANCELLABLE = Signal.valueOf(DefaultPromise.class, "UNCANCELLABLE");
private static final CauseHolder CANCELLATION_CAUSE_HOLDER = new CauseHolder(new CancellationException());
static {

View File

@ -81,4 +81,10 @@ public class ConstantPoolTest {
assertThat(array[3], is(sameInstance(d)));
assertThat(array[4], is(sameInstance(e)));
}
@Test
public void testComposedName() {
TestConstant a = pool.valueOf(Object.class, "A");
assertThat(a.name(), is("java.lang.Object#A"));
}
}

View File

@ -73,10 +73,10 @@ public abstract class AbstractTrafficShapingHandler extends ChannelDuplexHandler
*/
protected long checkInterval = DEFAULT_CHECK_INTERVAL; // default 1 s
private static final AttributeKey<Boolean> READ_SUSPENDED = AttributeKey.valueOf(
AbstractTrafficShapingHandler.class.getName() + ".READ_SUSPENDED");
private static final AttributeKey<Runnable> REOPEN_TASK = AttributeKey.valueOf(
AbstractTrafficShapingHandler.class.getName() + ".REOPEN_TASK");
private static final AttributeKey<Boolean> READ_SUSPENDED =
AttributeKey.valueOf(AbstractTrafficShapingHandler.class, "READ_SUSPENDED");
private static final AttributeKey<Runnable> REOPEN_TASK =
AttributeKey.valueOf(AbstractTrafficShapingHandler.class, "REOPEN_TASK");
/**
*

View File

@ -28,14 +28,16 @@ import static io.netty.channel.ChannelOption.*;
public final class RxtxChannelOption {
public static final ChannelOption<Integer> BAUD_RATE = valueOf("BAUD_RATE");
public static final ChannelOption<Boolean> DTR = valueOf("DTR");
public static final ChannelOption<Boolean> RTS = valueOf("RTS");
public static final ChannelOption<Stopbits> STOP_BITS = valueOf("STOP_BITS");
public static final ChannelOption<Databits> DATA_BITS = valueOf("DATA_BITS");
public static final ChannelOption<Paritybit> PARITY_BIT = valueOf("PARITY_BIT");
public static final ChannelOption<Integer> WAIT_TIME = valueOf("WAIT_TIME");
public static final ChannelOption<Integer> READ_TIMEOUT = valueOf("READ_TIMEOUT");
private static final Class<RxtxChannelOption> T = RxtxChannelOption.class;
public static final ChannelOption<Integer> BAUD_RATE = valueOf(T, "BAUD_RATE");
public static final ChannelOption<Boolean> DTR = valueOf(T, "DTR");
public static final ChannelOption<Boolean> RTS = valueOf(T, "RTS");
public static final ChannelOption<Stopbits> STOP_BITS = valueOf(T, "STOP_BITS");
public static final ChannelOption<Databits> DATA_BITS = valueOf(T, "DATA_BITS");
public static final ChannelOption<Paritybit> PARITY_BIT = valueOf(T, "PARITY_BIT");
public static final ChannelOption<Integer> WAIT_TIME = valueOf(T, "WAIT_TIME");
public static final ChannelOption<Integer> READ_TIMEOUT = valueOf(T, "READ_TIMEOUT");
private RxtxChannelOption() { }
}

View File

@ -27,14 +27,17 @@ import static io.netty.channel.ChannelOption.*;
*/
public final class SctpChannelOption {
public static final ChannelOption<Boolean> SCTP_DISABLE_FRAGMENTS = valueOf("SCTP_DISABLE_FRAGMENTS");
public static final ChannelOption<Boolean> SCTP_EXPLICIT_COMPLETE = valueOf("SCTP_EXPLICIT_COMPLETE");
public static final ChannelOption<Integer> SCTP_FRAGMENT_INTERLEAVE = valueOf("SCTP_FRAGMENT_INTERLEAVE");
public static final ChannelOption<InitMaxStreams> SCTP_INIT_MAXSTREAMS = valueOf("SCTP_INIT_MAXSTREAMS");
private static final Class<SctpChannelOption> T = SctpChannelOption.class;
public static final ChannelOption<Boolean> SCTP_NODELAY = valueOf("SCTP_NODELAY");
public static final ChannelOption<SocketAddress> SCTP_PRIMARY_ADDR = valueOf("SCTP_PRIMARY_ADDR");
public static final ChannelOption<SocketAddress> SCTP_SET_PEER_PRIMARY_ADDR = valueOf("SCTP_SET_PEER_PRIMARY_ADDR");
public static final ChannelOption<Boolean> SCTP_DISABLE_FRAGMENTS = valueOf(T, "SCTP_DISABLE_FRAGMENTS");
public static final ChannelOption<Boolean> SCTP_EXPLICIT_COMPLETE = valueOf(T, "SCTP_EXPLICIT_COMPLETE");
public static final ChannelOption<Integer> SCTP_FRAGMENT_INTERLEAVE = valueOf(T, "SCTP_FRAGMENT_INTERLEAVE");
public static final ChannelOption<InitMaxStreams> SCTP_INIT_MAXSTREAMS = valueOf(T, "SCTP_INIT_MAXSTREAMS");
public static final ChannelOption<Boolean> SCTP_NODELAY = valueOf(T, "SCTP_NODELAY");
public static final ChannelOption<SocketAddress> SCTP_PRIMARY_ADDR = valueOf(T, "SCTP_PRIMARY_ADDR");
public static final ChannelOption<SocketAddress> SCTP_SET_PEER_PRIMARY_ADDR =
valueOf(T, "SCTP_SET_PEER_PRIMARY_ADDR");
private SctpChannelOption() { }
}

View File

@ -25,29 +25,28 @@ import static io.netty.channel.ChannelOption.*;
*/
public final class UdtChannelOption {
private static final Class<UdtChannelOption> T = UdtChannelOption.class;
/**
* See {@link OptionUDT#Protocol_Receive_Buffer_Size}.
*/
public static final ChannelOption<Integer> PROTOCOL_RECEIVE_BUFFER_SIZE = valueOf(
"PROTOCOL_RECEIVE_BUFFER_SIZE");
public static final ChannelOption<Integer> PROTOCOL_RECEIVE_BUFFER_SIZE =
valueOf(T, "PROTOCOL_RECEIVE_BUFFER_SIZE");
/**
* See {@link OptionUDT#Protocol_Send_Buffer_Size}.
*/
public static final ChannelOption<Integer> PROTOCOL_SEND_BUFFER_SIZE = valueOf(
"PROTOCOL_SEND_BUFFER_SIZE");
public static final ChannelOption<Integer> PROTOCOL_SEND_BUFFER_SIZE = valueOf(T, "PROTOCOL_SEND_BUFFER_SIZE");
/**
* See {@link OptionUDT#System_Receive_Buffer_Size}.
*/
public static final ChannelOption<Integer> SYSTEM_RECEIVE_BUFFER_SIZE = valueOf(
"SYSTEM_RECEIVE_BUFFER_SIZE");
public static final ChannelOption<Integer> SYSTEM_RECEIVE_BUFFER_SIZE = valueOf(T, "SYSTEM_RECEIVE_BUFFER_SIZE");
/**
* See {@link OptionUDT#System_Send_Buffer_Size}.
*/
public static final ChannelOption<Integer> SYSTEM_SEND_BUFFER_SIZE = valueOf(
"SYSTEM_SEND_BUFFER_SIZE");
public static final ChannelOption<Integer> SYSTEM_SEND_BUFFER_SIZE = valueOf(T, "SYSTEM_SEND_BUFFER_SIZE");
private UdtChannelOption() { }
}

View File

@ -39,11 +39,22 @@ public final class ChannelOption<T> extends AbstractConstant<ChannelOption<T>> {
}
};
/**
* Returns the {@link ChannelOption} of the specified name.
*/
@SuppressWarnings("unchecked")
public static <T> ChannelOption<T> valueOf(String name) {
return (ChannelOption<T>) pool.valueOf(name);
}
/**
* Shortcut of {@link #valueOf(String) valueOf(firstNameComponent.getName() + "#" + secondNameComponent)}.
*/
@SuppressWarnings("unchecked")
public static <T> ChannelOption<T> valueOf(Class<?> firstNameComponent, String secondNameComponent) {
return (ChannelOption<T>) pool.valueOf(firstNameComponent, secondNameComponent);
}
public static final ChannelOption<ByteBufAllocator> ALLOCATOR = valueOf("ALLOCATOR");
public static final ChannelOption<RecvByteBufAllocator> RCVBUF_ALLOCATOR = valueOf("RCVBUF_ALLOCATOR");
public static final ChannelOption<MessageSizeEstimator> MESSAGE_SIZE_ESTIMATOR = valueOf("MESSAGE_SIZE_ESTIMATOR");