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;
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.net.InetSocketAddress;
import java.nio.channels.SelectionKey;
@ -29,10 +33,6 @@ import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
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
* implementation.
@ -141,38 +141,38 @@ final class NioProviderMetadata {
// System.out.println(provider);
// Sun JVM
if (vendor.indexOf("sun") >= 0) {
if (vendor.contains("sun")) {
// Linux
if (os.indexOf("linux") >= 0) {
if (os.contains("linux")) {
if (provider.equals("sun.nio.ch.EPollSelectorProvider") ||
provider.equals("sun.nio.ch.PollSelectorProvider")) {
return 0;
}
// Windows
} else if (os.indexOf("windows") >= 0) {
} else if (os.contains("windows")) {
if (provider.equals("sun.nio.ch.WindowsSelectorProvider")) {
return 0;
}
// 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")) {
return 0;
}
}
// Apple JVM
} else if (vendor.indexOf("apple") >= 0) {
} else if (vendor.contains("apple")) {
// 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")) {
return 0;
}
}
// IBM
} else if (vendor.indexOf("ibm") >= 0) {
} else if (vendor.contains("ibm")) {
// 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 (provider.equals("sun.nio.ch.PollSelectorProvider")) {
return 1;
@ -206,22 +206,22 @@ final class NioProviderMetadata {
}
}
// BEA
} else if (vendor.indexOf("bea") >= 0 || vendor.indexOf("oracle") >= 0) {
} else if (vendor.contains("bea") || vendor.contains("oracle")) {
// Linux
if (os.indexOf("linux") >= 0) {
if (os.contains("linux")) {
if (provider.equals("sun.nio.ch.EPollSelectorProvider") ||
provider.equals("sun.nio.ch.PollSelectorProvider")) {
return 0;
}
// Windows
} else if (os.indexOf("windows") >= 0) {
} else if (os.contains("windows")) {
if (provider.equals("sun.nio.ch.WindowsSelectorProvider")) {
return 0;
}
}
// Apache Software Foundation
} else if (vendor.indexOf("apache") >= 0) {
} else if (vendor.contains("apache")) {
if (provider.equals("org.apache.harmony.nio.internal.SelectorProviderImpl")) {
return 1;
}

View File

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

View File

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

View File

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

View File

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

View File

@ -394,10 +394,8 @@ public final class ConcurrentIdentityWeakKeyHashMap<K, V> extends AbstractMap<K,
boolean containsValue(Object value) {
if (count != 0) { // read-volatile
HashEntry<K, V>[] tab = table;
int len = tab.length;
for (int i = 0; i < len; i ++) {
for (HashEntry<K, V> e = tab[i]; e != null; e = e.next) {
for (HashEntry<K, V> e: table) {
for (; e != null; e = e.next) {
Object opaque = e.valueRef;
V v;
@ -518,11 +516,9 @@ public final class ConcurrentIdentityWeakKeyHashMap<K, V> extends AbstractMap<K,
threshold = (int) (newTable.length * loadFactor);
int sizeMask = newTable.length - 1;
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
// proceed. So we cannot yet null out each bin.
HashEntry<K, V> e = oldTable[i];
if (e != null) {
HashEntry<K, V> next = e.next;
int idx = e.hash & sizeMask;
@ -547,7 +543,7 @@ public final class ConcurrentIdentityWeakKeyHashMap<K, V> extends AbstractMap<K,
// Skip GC'd weak references
K key = p.key();
if (key == null) {
reduce ++;
reduce++;
continue;
}
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
sum = 0;
for (int i = 0; i < segments.length; ++ i) {
segments[i].lock();
for (Segment<K, V> segment: segments) {
segment.lock();
}
for (int i = 0; i < segments.length; ++ i) {
sum += segments[i].count;
for (Segment<K, V> segment: segments) {
sum += segment.count;
}
for (int i = 0; i < segments.length; ++ i) {
segments[i].unlock();
for (Segment<K, V> segment: segments) {
segment.unlock();
}
}
if (sum > Integer.MAX_VALUE) {
@ -920,20 +916,20 @@ public final class ConcurrentIdentityWeakKeyHashMap<K, V> extends AbstractMap<K,
}
}
// Resort to locking all segments
for (int i = 0; i < segments.length; ++ i) {
segments[i].lock();
for (Segment<K, V> segment: segments) {
segment.lock();
}
boolean found = false;
try {
for (int i = 0; i < segments.length; ++ i) {
if (segments[i].containsValue(value)) {
for (Segment<K, V> segment: segments) {
if (segment.containsValue(value)) {
found = true;
break;
}
}
} finally {
for (int i = 0; i < segments.length; ++ i) {
segments[i].unlock();
for (Segment<K, V> segment: segments) {
segment.unlock();
}
}
return found;
@ -1060,8 +1056,8 @@ public final class ConcurrentIdentityWeakKeyHashMap<K, V> extends AbstractMap<K,
*/
@Override
public void clear() {
for (int i = 0; i < segments.length; ++ i) {
segments[i].clear();
for (Segment<K, V> segment: segments) {
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.
*/
public void purgeStaleEntries() {
for (int i = 0; i < segments.length; ++ i) {
segments[i].removeStale();
for (Segment<K, V> segment: segments) {
segment.removeStale();
}
}

View File

@ -394,10 +394,8 @@ public final class ConcurrentWeakKeyHashMap<K, V> extends AbstractMap<K, V> impl
boolean containsValue(Object value) {
if (count != 0) { // read-volatile
HashEntry<K, V>[] tab = table;
int len = tab.length;
for (int i = 0; i < len; i ++) {
for (HashEntry<K, V> e = tab[i]; e != null; e = e.next) {
for (HashEntry<K, V> e: table) {
for (; e != null; e = e.next) {
Object opaque = e.valueRef;
V v;
@ -518,11 +516,9 @@ public final class ConcurrentWeakKeyHashMap<K, V> extends AbstractMap<K, V> impl
threshold = (int) (newTable.length * loadFactor);
int sizeMask = newTable.length - 1;
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
// proceed. So we cannot yet null out each bin.
HashEntry<K, V> e = oldTable[i];
if (e != null) {
HashEntry<K, V> next = e.next;
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
K key = p.key();
if (key == null) {
reduce ++;
reduce++;
continue;
}
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
sum = 0;
for (int i = 0; i < segments.length; ++ i) {
segments[i].lock();
for (Segment<K, V> segment: segments) {
segment.lock();
}
for (int i = 0; i < segments.length; ++ i) {
sum += segments[i].count;
for (Segment<K, V> segment: segments) {
sum += segment.count;
}
for (int i = 0; i < segments.length; ++ i) {
segments[i].unlock();
for (Segment<K, V> segment: segments) {
segment.unlock();
}
}
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
for (int i = 0; i < segments.length; ++ i) {
segments[i].lock();
for (Segment<K, V> segment: segments) {
segment.lock();
}
boolean found = false;
try {
for (int i = 0; i < segments.length; ++ i) {
if (segments[i].containsValue(value)) {
for (Segment<K, V> segment: segments) {
if (segment.containsValue(value)) {
found = true;
break;
}
}
} finally {
for (int i = 0; i < segments.length; ++ i) {
segments[i].unlock();
for (Segment<K, V> segment: segments) {
segment.unlock();
}
}
return found;
@ -1060,8 +1056,8 @@ public final class ConcurrentWeakKeyHashMap<K, V> extends AbstractMap<K, V> impl
*/
@Override
public void clear() {
for (int i = 0; i < segments.length; ++ i) {
segments[i].clear();
for (Segment<K, V> segment: segments) {
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.
*/
public void purgeStaleEntries() {
for (int i = 0; i < segments.length; ++ i) {
segments[i].removeStale();
for (Segment<K, V> segment: segments) {
segment.removeStale();
}
}

View File

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