Use foreach loop wherever possible / Prefer String.contains() to indexOf() >= 0 / Prefer StringUtil.split() to String.split()

This commit is contained in:
Trustin Lee 2012-11-10 07:24:54 +09:00
parent ab02e90684
commit 9a4296f320
8 changed files with 97 additions and 113 deletions

View File

@ -15,6 +15,10 @@
*/ */
package org.jboss.netty.channel.socket.nio; package org.jboss.netty.channel.socket.nio;
import org.jboss.netty.logging.InternalLogger;
import org.jboss.netty.logging.InternalLoggerFactory;
import org.jboss.netty.util.internal.SystemPropertyUtil;
import java.io.IOException; import java.io.IOException;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
import java.nio.channels.SelectionKey; import java.nio.channels.SelectionKey;
@ -29,10 +33,6 @@ import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import org.jboss.netty.logging.InternalLogger;
import org.jboss.netty.logging.InternalLoggerFactory;
import org.jboss.netty.util.internal.SystemPropertyUtil;
/** /**
* Provides information which is specific to a NIO service provider * Provides information which is specific to a NIO service provider
* implementation. * implementation.
@ -141,38 +141,38 @@ final class NioProviderMetadata {
// System.out.println(provider); // System.out.println(provider);
// Sun JVM // Sun JVM
if (vendor.indexOf("sun") >= 0) { if (vendor.contains("sun")) {
// Linux // Linux
if (os.indexOf("linux") >= 0) { if (os.contains("linux")) {
if (provider.equals("sun.nio.ch.EPollSelectorProvider") || if (provider.equals("sun.nio.ch.EPollSelectorProvider") ||
provider.equals("sun.nio.ch.PollSelectorProvider")) { provider.equals("sun.nio.ch.PollSelectorProvider")) {
return 0; return 0;
} }
// Windows // Windows
} else if (os.indexOf("windows") >= 0) { } else if (os.contains("windows")) {
if (provider.equals("sun.nio.ch.WindowsSelectorProvider")) { if (provider.equals("sun.nio.ch.WindowsSelectorProvider")) {
return 0; return 0;
} }
// Solaris // Solaris
} else if (os.indexOf("sun") >= 0 || os.indexOf("solaris") >= 0) { } else if (os.contains("sun") || os.contains("solaris")) {
if (provider.equals("sun.nio.ch.DevPollSelectorProvider")) { if (provider.equals("sun.nio.ch.DevPollSelectorProvider")) {
return 0; return 0;
} }
} }
// Apple JVM // Apple JVM
} else if (vendor.indexOf("apple") >= 0) { } else if (vendor.contains("apple")) {
// Mac OS // Mac OS
if (os.indexOf("mac") >= 0 && os.indexOf("os") >= 0) { if (os.contains("mac") && os.contains("os")) {
if (provider.equals("sun.nio.ch.KQueueSelectorProvider")) { if (provider.equals("sun.nio.ch.KQueueSelectorProvider")) {
return 0; return 0;
} }
} }
// IBM // IBM
} else if (vendor.indexOf("ibm") >= 0) { } else if (vendor.contains("ibm")) {
// Linux or AIX // Linux or AIX
if (os.indexOf("linux") >= 0 || os.indexOf("aix") >= 0) { if (os.contains("linux") || os.contains("aix")) {
if (version.equals("1.5") || version.matches("^1\\.5\\D.*$")) { if (version.equals("1.5") || version.matches("^1\\.5\\D.*$")) {
if (provider.equals("sun.nio.ch.PollSelectorProvider")) { if (provider.equals("sun.nio.ch.PollSelectorProvider")) {
return 1; return 1;
@ -206,22 +206,22 @@ final class NioProviderMetadata {
} }
} }
// BEA // BEA
} else if (vendor.indexOf("bea") >= 0 || vendor.indexOf("oracle") >= 0) { } else if (vendor.contains("bea") || vendor.contains("oracle")) {
// Linux // Linux
if (os.indexOf("linux") >= 0) { if (os.contains("linux")) {
if (provider.equals("sun.nio.ch.EPollSelectorProvider") || if (provider.equals("sun.nio.ch.EPollSelectorProvider") ||
provider.equals("sun.nio.ch.PollSelectorProvider")) { provider.equals("sun.nio.ch.PollSelectorProvider")) {
return 0; return 0;
} }
// Windows // Windows
} else if (os.indexOf("windows") >= 0) { } else if (os.contains("windows")) {
if (provider.equals("sun.nio.ch.WindowsSelectorProvider")) { if (provider.equals("sun.nio.ch.WindowsSelectorProvider")) {
return 0; return 0;
} }
} }
// Apache Software Foundation // Apache Software Foundation
} else if (vendor.indexOf("apache") >= 0) { } else if (vendor.contains("apache")) {
if (provider.equals("org.apache.harmony.nio.internal.SelectorProviderImpl")) { if (provider.equals("org.apache.harmony.nio.internal.SelectorProviderImpl")) {
return 1; return 1;
} }

View File

@ -150,11 +150,11 @@ public class HttpContentCompressor extends HttpContentEncoder {
q = 0.0f; q = 0.0f;
} }
} }
if (encoding.indexOf("*") >= 0) { if (encoding.indexOf('*') >= 0) {
starQ = q; starQ = q;
} else if (encoding.indexOf("gzip") >= 0 && q > gzipQ) { } else if (encoding.contains("gzip") && q > gzipQ) {
gzipQ = q; gzipQ = q;
} else if (encoding.indexOf("deflate") >= 0 && q > deflateQ) { } else if (encoding.contains("deflate") && q > deflateQ) {
deflateQ = q; deflateQ = q;
} }
} }

View File

@ -94,7 +94,7 @@ public abstract class CIDR implements Comparable<CIDR> {
String maskString = cidr.substring(p + 1); String maskString = cidr.substring(p + 1);
InetAddress addr = addressStringToInet(addrString); InetAddress addr = addressStringToInet(addrString);
int mask = 0; int mask = 0;
if (maskString.indexOf(".") < 0) { if (maskString.indexOf('.') < 0) {
mask = parseInt(maskString, -1); mask = parseInt(maskString, -1);
} else { } else {
mask = getNetMask(maskString); mask = getNetMask(maskString);

View File

@ -346,10 +346,8 @@ public final class ConcurrentHashMap<K, V> extends AbstractMap<K, V>
boolean containsValue(Object value) { boolean containsValue(Object value) {
if (count != 0) { // read-volatile if (count != 0) { // read-volatile
HashEntry<K, V>[] tab = table; for (HashEntry<K, V> e: table) {
int len = tab.length; for (; e != null; e = e.next) {
for (int i = 0; i < len; i ++) {
for (HashEntry<K, V> e = tab[i]; e != null; e = e.next) {
V opaque = e.value(); V opaque = e.value();
V v; V v;
@ -467,11 +465,9 @@ public final class ConcurrentHashMap<K, V> extends AbstractMap<K, V>
threshold = (int) (newTable.length * loadFactor); threshold = (int) (newTable.length * loadFactor);
int sizeMask = newTable.length - 1; int sizeMask = newTable.length - 1;
int reduce = 0; int reduce = 0;
for (int i = 0; i < oldCapacity; i ++) { for (HashEntry<K, V> e: oldTable) {
// We need to guarantee that any existing reads of old Map can // We need to guarantee that any existing reads of old Map can
// proceed. So we cannot yet null out each bin. // proceed. So we cannot yet null out each bin.
HashEntry<K, V> e = oldTable[i];
if (e != null) { if (e != null) {
HashEntry<K, V> next = e.next; HashEntry<K, V> next = e.next;
int idx = e.hash & sizeMask; int idx = e.hash & sizeMask;
@ -496,7 +492,7 @@ public final class ConcurrentHashMap<K, V> extends AbstractMap<K, V>
// Skip GC'd weak references // Skip GC'd weak references
K key = p.key(); K key = p.key();
if (key == null) { if (key == null) {
reduce ++; reduce++;
continue; continue;
} }
int k = p.hash & sizeMask; int k = p.hash & sizeMask;
@ -763,14 +759,14 @@ public final class ConcurrentHashMap<K, V> extends AbstractMap<K, V>
} }
if (check != sum) { // Resort to locking all segments if (check != sum) { // Resort to locking all segments
sum = 0; sum = 0;
for (int i = 0; i < segments.length; ++ i) { for (Segment<K, V> segment: segments) {
segments[i].lock(); segment.lock();
} }
for (int i = 0; i < segments.length; ++ i) { for (Segment<K, V> segment: segments) {
sum += segments[i].count; sum += segment.count;
} }
for (int i = 0; i < segments.length; ++ i) { for (Segment<K, V> segment: segments) {
segments[i].unlock(); segment.unlock();
} }
} }
if (sum > Integer.MAX_VALUE) { if (sum > Integer.MAX_VALUE) {
@ -857,20 +853,20 @@ public final class ConcurrentHashMap<K, V> extends AbstractMap<K, V>
} }
} }
// Resort to locking all segments // Resort to locking all segments
for (int i = 0; i < segments.length; ++ i) { for (Segment<K, V> segment: segments) {
segments[i].lock(); segment.lock();
} }
boolean found = false; boolean found = false;
try { try {
for (int i = 0; i < segments.length; ++ i) { for (Segment<K, V> segment: segments) {
if (segments[i].containsValue(value)) { if (segment.containsValue(value)) {
found = true; found = true;
break; break;
} }
} }
} finally { } finally {
for (int i = 0; i < segments.length; ++ i) { for (Segment<K, V> segment: segments) {
segments[i].unlock(); segment.unlock();
} }
} }
return found; return found;
@ -997,8 +993,8 @@ public final class ConcurrentHashMap<K, V> extends AbstractMap<K, V>
*/ */
@Override @Override
public void clear() { public void clear() {
for (int i = 0; i < segments.length; ++ i) { for (Segment<K, V> segment: segments) {
segments[i].clear(); segment.clear();
} }
} }

View File

@ -346,10 +346,8 @@ public final class ConcurrentIdentityHashMap<K, V> extends AbstractMap<K, V>
boolean containsValue(Object value) { boolean containsValue(Object value) {
if (count != 0) { // read-volatile if (count != 0) { // read-volatile
HashEntry<K, V>[] tab = table; for (HashEntry<K, V> e: table) {
int len = tab.length; for (; e != null; e = e.next) {
for (int i = 0; i < len; i ++) {
for (HashEntry<K, V> e = tab[i]; e != null; e = e.next) {
V opaque = e.value(); V opaque = e.value();
V v; V v;
@ -467,11 +465,9 @@ public final class ConcurrentIdentityHashMap<K, V> extends AbstractMap<K, V>
threshold = (int) (newTable.length * loadFactor); threshold = (int) (newTable.length * loadFactor);
int sizeMask = newTable.length - 1; int sizeMask = newTable.length - 1;
int reduce = 0; int reduce = 0;
for (int i = 0; i < oldCapacity; i ++) { for (HashEntry<K, V> e: oldTable) {
// We need to guarantee that any existing reads of old Map can // We need to guarantee that any existing reads of old Map can
// proceed. So we cannot yet null out each bin. // proceed. So we cannot yet null out each bin.
HashEntry<K, V> e = oldTable[i];
if (e != null) { if (e != null) {
HashEntry<K, V> next = e.next; HashEntry<K, V> next = e.next;
int idx = e.hash & sizeMask; int idx = e.hash & sizeMask;
@ -496,7 +492,7 @@ public final class ConcurrentIdentityHashMap<K, V> extends AbstractMap<K, V>
// Skip GC'd weak references // Skip GC'd weak references
K key = p.key(); K key = p.key();
if (key == null) { if (key == null) {
reduce ++; reduce++;
continue; continue;
} }
int k = p.hash & sizeMask; int k = p.hash & sizeMask;
@ -763,14 +759,14 @@ public final class ConcurrentIdentityHashMap<K, V> extends AbstractMap<K, V>
} }
if (check != sum) { // Resort to locking all segments if (check != sum) { // Resort to locking all segments
sum = 0; sum = 0;
for (int i = 0; i < segments.length; ++ i) { for (Segment<K, V> segment: segments) {
segments[i].lock(); segment.lock();
} }
for (int i = 0; i < segments.length; ++ i) { for (Segment<K, V> segment: segments) {
sum += segments[i].count; sum += segment.count;
} }
for (int i = 0; i < segments.length; ++ i) { for (Segment<K, V> segment: segments) {
segments[i].unlock(); segment.unlock();
} }
} }
if (sum > Integer.MAX_VALUE) { if (sum > Integer.MAX_VALUE) {
@ -857,20 +853,20 @@ public final class ConcurrentIdentityHashMap<K, V> extends AbstractMap<K, V>
} }
} }
// Resort to locking all segments // Resort to locking all segments
for (int i = 0; i < segments.length; ++ i) { for (Segment<K, V> segment: segments) {
segments[i].lock(); segment.lock();
} }
boolean found = false; boolean found = false;
try { try {
for (int i = 0; i < segments.length; ++ i) { for (Segment<K, V> segment: segments) {
if (segments[i].containsValue(value)) { if (segment.containsValue(value)) {
found = true; found = true;
break; break;
} }
} }
} finally { } finally {
for (int i = 0; i < segments.length; ++ i) { for (Segment<K, V> segment: segments) {
segments[i].unlock(); segment.unlock();
} }
} }
return found; return found;
@ -997,8 +993,8 @@ public final class ConcurrentIdentityHashMap<K, V> extends AbstractMap<K, V>
*/ */
@Override @Override
public void clear() { public void clear() {
for (int i = 0; i < segments.length; ++ i) { for (Segment<K, V> segment: segments) {
segments[i].clear(); segment.clear();
} }
} }

View File

@ -394,10 +394,8 @@ public final class ConcurrentIdentityWeakKeyHashMap<K, V> extends AbstractMap<K,
boolean containsValue(Object value) { boolean containsValue(Object value) {
if (count != 0) { // read-volatile if (count != 0) { // read-volatile
HashEntry<K, V>[] tab = table; for (HashEntry<K, V> e: table) {
int len = tab.length; for (; e != null; e = e.next) {
for (int i = 0; i < len; i ++) {
for (HashEntry<K, V> e = tab[i]; e != null; e = e.next) {
Object opaque = e.valueRef; Object opaque = e.valueRef;
V v; V v;
@ -518,11 +516,9 @@ public final class ConcurrentIdentityWeakKeyHashMap<K, V> extends AbstractMap<K,
threshold = (int) (newTable.length * loadFactor); threshold = (int) (newTable.length * loadFactor);
int sizeMask = newTable.length - 1; int sizeMask = newTable.length - 1;
int reduce = 0; int reduce = 0;
for (int i = 0; i < oldCapacity; i ++) { for (HashEntry<K, V> e: oldTable) {
// We need to guarantee that any existing reads of old Map can // We need to guarantee that any existing reads of old Map can
// proceed. So we cannot yet null out each bin. // proceed. So we cannot yet null out each bin.
HashEntry<K, V> e = oldTable[i];
if (e != null) { if (e != null) {
HashEntry<K, V> next = e.next; HashEntry<K, V> next = e.next;
int idx = e.hash & sizeMask; int idx = e.hash & sizeMask;
@ -547,7 +543,7 @@ public final class ConcurrentIdentityWeakKeyHashMap<K, V> extends AbstractMap<K,
// Skip GC'd weak references // Skip GC'd weak references
K key = p.key(); K key = p.key();
if (key == null) { if (key == null) {
reduce ++; reduce++;
continue; continue;
} }
int k = p.hash & sizeMask; int k = p.hash & sizeMask;
@ -826,14 +822,14 @@ public final class ConcurrentIdentityWeakKeyHashMap<K, V> extends AbstractMap<K,
} }
if (check != sum) { // Resort to locking all segments if (check != sum) { // Resort to locking all segments
sum = 0; sum = 0;
for (int i = 0; i < segments.length; ++ i) { for (Segment<K, V> segment: segments) {
segments[i].lock(); segment.lock();
} }
for (int i = 0; i < segments.length; ++ i) { for (Segment<K, V> segment: segments) {
sum += segments[i].count; sum += segment.count;
} }
for (int i = 0; i < segments.length; ++ i) { for (Segment<K, V> segment: segments) {
segments[i].unlock(); segment.unlock();
} }
} }
if (sum > Integer.MAX_VALUE) { if (sum > Integer.MAX_VALUE) {
@ -920,20 +916,20 @@ public final class ConcurrentIdentityWeakKeyHashMap<K, V> extends AbstractMap<K,
} }
} }
// Resort to locking all segments // Resort to locking all segments
for (int i = 0; i < segments.length; ++ i) { for (Segment<K, V> segment: segments) {
segments[i].lock(); segment.lock();
} }
boolean found = false; boolean found = false;
try { try {
for (int i = 0; i < segments.length; ++ i) { for (Segment<K, V> segment: segments) {
if (segments[i].containsValue(value)) { if (segment.containsValue(value)) {
found = true; found = true;
break; break;
} }
} }
} finally { } finally {
for (int i = 0; i < segments.length; ++ i) { for (Segment<K, V> segment: segments) {
segments[i].unlock(); segment.unlock();
} }
} }
return found; return found;
@ -1060,8 +1056,8 @@ public final class ConcurrentIdentityWeakKeyHashMap<K, V> extends AbstractMap<K,
*/ */
@Override @Override
public void clear() { public void clear() {
for (int i = 0; i < segments.length; ++ i) { for (Segment<K, V> segment: segments) {
segments[i].clear(); segment.clear();
} }
} }
@ -1077,8 +1073,8 @@ public final class ConcurrentIdentityWeakKeyHashMap<K, V> extends AbstractMap<K,
* of this table, so if it is to be used, it should be used sparingly. * of this table, so if it is to be used, it should be used sparingly.
*/ */
public void purgeStaleEntries() { public void purgeStaleEntries() {
for (int i = 0; i < segments.length; ++ i) { for (Segment<K, V> segment: segments) {
segments[i].removeStale(); segment.removeStale();
} }
} }

View File

@ -394,10 +394,8 @@ public final class ConcurrentWeakKeyHashMap<K, V> extends AbstractMap<K, V> impl
boolean containsValue(Object value) { boolean containsValue(Object value) {
if (count != 0) { // read-volatile if (count != 0) { // read-volatile
HashEntry<K, V>[] tab = table; for (HashEntry<K, V> e: table) {
int len = tab.length; for (; e != null; e = e.next) {
for (int i = 0; i < len; i ++) {
for (HashEntry<K, V> e = tab[i]; e != null; e = e.next) {
Object opaque = e.valueRef; Object opaque = e.valueRef;
V v; V v;
@ -518,11 +516,9 @@ public final class ConcurrentWeakKeyHashMap<K, V> extends AbstractMap<K, V> impl
threshold = (int) (newTable.length * loadFactor); threshold = (int) (newTable.length * loadFactor);
int sizeMask = newTable.length - 1; int sizeMask = newTable.length - 1;
int reduce = 0; int reduce = 0;
for (int i = 0; i < oldCapacity; i ++) { for (HashEntry<K, V> e: oldTable) {
// We need to guarantee that any existing reads of old Map can // We need to guarantee that any existing reads of old Map can
// proceed. So we cannot yet null out each bin. // proceed. So we cannot yet null out each bin.
HashEntry<K, V> e = oldTable[i];
if (e != null) { if (e != null) {
HashEntry<K, V> next = e.next; HashEntry<K, V> next = e.next;
int idx = e.hash & sizeMask; int idx = e.hash & sizeMask;
@ -547,7 +543,7 @@ public final class ConcurrentWeakKeyHashMap<K, V> extends AbstractMap<K, V> impl
// Skip GC'd weak references // Skip GC'd weak references
K key = p.key(); K key = p.key();
if (key == null) { if (key == null) {
reduce ++; reduce++;
continue; continue;
} }
int k = p.hash & sizeMask; int k = p.hash & sizeMask;
@ -826,14 +822,14 @@ public final class ConcurrentWeakKeyHashMap<K, V> extends AbstractMap<K, V> impl
} }
if (check != sum) { // Resort to locking all segments if (check != sum) { // Resort to locking all segments
sum = 0; sum = 0;
for (int i = 0; i < segments.length; ++ i) { for (Segment<K, V> segment: segments) {
segments[i].lock(); segment.lock();
} }
for (int i = 0; i < segments.length; ++ i) { for (Segment<K, V> segment: segments) {
sum += segments[i].count; sum += segment.count;
} }
for (int i = 0; i < segments.length; ++ i) { for (Segment<K, V> segment: segments) {
segments[i].unlock(); segment.unlock();
} }
} }
if (sum > Integer.MAX_VALUE) { if (sum > Integer.MAX_VALUE) {
@ -920,20 +916,20 @@ public final class ConcurrentWeakKeyHashMap<K, V> extends AbstractMap<K, V> impl
} }
} }
// Resort to locking all segments // Resort to locking all segments
for (int i = 0; i < segments.length; ++ i) { for (Segment<K, V> segment: segments) {
segments[i].lock(); segment.lock();
} }
boolean found = false; boolean found = false;
try { try {
for (int i = 0; i < segments.length; ++ i) { for (Segment<K, V> segment: segments) {
if (segments[i].containsValue(value)) { if (segment.containsValue(value)) {
found = true; found = true;
break; break;
} }
} }
} finally { } finally {
for (int i = 0; i < segments.length; ++ i) { for (Segment<K, V> segment: segments) {
segments[i].unlock(); segment.unlock();
} }
} }
return found; return found;
@ -1060,8 +1056,8 @@ public final class ConcurrentWeakKeyHashMap<K, V> extends AbstractMap<K, V> impl
*/ */
@Override @Override
public void clear() { public void clear() {
for (int i = 0; i < segments.length; ++ i) { for (Segment<K, V> segment: segments) {
segments[i].clear(); segment.clear();
} }
} }
@ -1077,8 +1073,8 @@ public final class ConcurrentWeakKeyHashMap<K, V> extends AbstractMap<K, V> impl
* of this table, so if it is to be used, it should be used sparingly. * of this table, so if it is to be used, it should be used sparingly.
*/ */
public void purgeStaleEntries() { public void purgeStaleEntries() {
for (int i = 0; i < segments.length; ++ i) { for (Segment<K, V> segment: segments) {
segments[i].removeStale(); segment.removeStale();
} }
} }

View File

@ -40,7 +40,7 @@ public final class DetectionUtil {
static { static {
String os = SystemPropertyUtil.get("os.name", "").toLowerCase(); String os = SystemPropertyUtil.get("os.name", "").toLowerCase();
// windows // windows
IS_WINDOWS = os.indexOf("win") >= 0; IS_WINDOWS = os.contains("win");
} }
/** /**