Extracted int-to-string conversion to ConversionUtil

This commit is contained in:
Trustin Lee 2010-01-07 05:33:50 +00:00
parent bfc3b3999d
commit 0596aaac48
2 changed files with 19 additions and 17 deletions

View File

@ -24,6 +24,7 @@ import java.util.Map;
import org.jboss.netty.logging.InternalLogger;
import org.jboss.netty.logging.InternalLoggerFactory;
import org.jboss.netty.util.internal.ConversionUtil;
/**
* A {@link ChannelPipeline} that might perform better at the cost of
@ -42,12 +43,6 @@ public class StaticChannelPipeline implements ChannelPipeline {
// FIXME Code duplication with DefaultChannelPipeline
static final InternalLogger logger = InternalLoggerFactory.getInstance(StaticChannelPipeline.class);
// The names of the first 16 handlers to avoid int->string conversion cost
private static final String[] NAMES = {
"0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
"10","11","12","13","14","15",
};
private volatile Channel channel;
private volatile ChannelSink sink;
private final StaticChannelHandlerContext[] contexts;
@ -78,13 +73,7 @@ public class StaticChannelPipeline implements ChannelPipeline {
throw new NullPointerException("handlers[" + i + ']');
}
String name;
if (i < NAMES.length) {
name = NAMES[i];
} else {
name = Integer.toString(i);
}
String name = ConversionUtil.toString(i);
StaticChannelHandlerContext ctx =
new StaticChannelHandlerContext(i, name, h);
contexts[i] = ctx;

View File

@ -70,10 +70,6 @@ public class ConversionUtil {
}
}
private ConversionUtil() {
// Unused
}
/**
* Converts the specified object into an array of strings.
*/
@ -96,4 +92,21 @@ public class ConversionUtil {
return String.valueOf(value).split("[, \\t\\n\\r\\f\\e\\a]");
}
private static final String[] INTEGERS = {
"0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
"10","11","12","13","14","15",
};
public static String toString(int value) {
if (value >= 0 && value < INTEGERS.length) {
return INTEGERS[value];
} else {
return Integer.toString(value);
}
}
private ConversionUtil() {
// Unused
}
}