Remove unused classes in util.internal

This commit is contained in:
Trustin Lee 2012-07-07 13:41:49 +09:00
parent 9af7512c35
commit 8dc1f321cb
9 changed files with 1 additions and 562 deletions

View File

@ -1,53 +0,0 @@
/*
* Copyright 2012 The Netty Project
*
* The Netty Project licenses this file to you under the Apache License,
* version 2.0 (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
package io.netty.util.internal;
import java.util.concurrent.Executor;
/**
*/
public final class DeadLockProofWorker {
/**
* An <em>internal use only</em> thread-local variable that tells the
* {@link Executor} that this worker acquired a worker thread from.
*/
public static final ThreadLocal<Executor> PARENT = new ThreadLocal<Executor>();
public static void start(final Executor parent, final Runnable runnable) {
if (parent == null) {
throw new NullPointerException("parent");
}
if (runnable == null) {
throw new NullPointerException("runnable");
}
parent.execute(new Runnable() {
@Override
public void run() {
PARENT.set(parent);
try {
runnable.run();
} finally {
PARENT.remove();
}
}
});
}
private DeadLockProofWorker() {
}
}

View File

@ -1,119 +0,0 @@
/*
* Copyright 2012 The Netty Project
*
* The Netty Project licenses this file to you under the Apache License,
* version 2.0 (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
package io.netty.util.internal;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.TimeUnit;
/**
* Shuts down a list of {@link Executor}s. {@link #terminate(Executor...)} will
* shut down all specified {@link ExecutorService}s immediately and wait for
* their termination. An {@link Executor} which is not an {@link ExecutorService}
* will be ignored silently.
*/
public final class ExecutorUtil {
/**
* Returns {@code true} if and only if the specified {@code executor}
* is an {@link ExecutorService} and is shut down. Please note that this
* method returns {@code false} if the specified {@code executor} is not an
* {@link ExecutorService}.
*/
public static boolean isShutdown(Executor executor) {
if (executor instanceof ExecutorService) {
if (((ExecutorService) executor).isShutdown()) {
return true;
}
}
return false;
}
/**
* Shuts down the specified executors.
*/
public static void terminate(Executor... executors) {
// Check nulls.
if (executors == null) {
throw new NullPointerException("executors");
}
Executor[] executorsCopy = new Executor[executors.length];
for (int i = 0; i < executors.length; i ++) {
if (executors[i] == null) {
throw new NullPointerException("executors[" + i + "]");
}
executorsCopy[i] = executors[i];
}
// Check dead lock.
final Executor currentParent = DeadLockProofWorker.PARENT.get();
if (currentParent != null) {
for (Executor e: executorsCopy) {
if (e == currentParent) {
throw new IllegalStateException(
"An Executor cannot be shut down from the thread " +
"acquired from itself. Please make sure you are " +
"not calling releaseExternalResources() from an " +
"I/O worker thread.");
}
}
}
// Shut down all executors.
boolean interrupted = false;
for (Executor e: executorsCopy) {
if (!(e instanceof ExecutorService)) {
continue;
}
ExecutorService es = (ExecutorService) e;
for (;;) {
try {
es.shutdownNow();
} catch (SecurityException ex) {
// Running in a restricted environment - fall back.
try {
es.shutdown();
} catch (SecurityException ex2) {
// Running in a more restricted environment.
// Can't shut down this executor - skip to the next.
break;
} catch (NullPointerException ex2) {
// Some JDK throws NPE here, but shouldn't.
}
} catch (NullPointerException ex) {
// Some JDK throws NPE here, but shouldn't.
}
try {
if (es.awaitTermination(100, TimeUnit.MILLISECONDS)) {
break;
}
} catch (InterruptedException ex) {
interrupted = true;
}
}
}
if (interrupted) {
Thread.currentThread().interrupt();
}
}
private ExecutorUtil() {
}
}

View File

@ -1,148 +0,0 @@
/*
* Copyright 2012 The Netty Project
*
* The Netty Project licenses this file to you under the Apache License,
* version 2.0 (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
package io.netty.util.internal;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.AbstractQueuedSynchronizer;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
/**
* A custom implementation of a lock that does not allow reentry
*/
public final class NonReentrantLock extends AbstractQueuedSynchronizer
implements Lock {
/**
* The serial version unique ID
*/
private static final long serialVersionUID = -833780837233068610L;
/**
* The {@link Thread} that owns this {@link NonReentrantLock}
*/
private Thread owner;
/**
* Locks this {@link NonReentrantLock}
*/
@Override
public void lock() {
acquire(1);
}
/**
* Locks this {@link NonReentrantLock}, but allow interruption
*
* @throws InterruptedException The lock was interrupted
*/
@Override
public void lockInterruptibly() throws InterruptedException {
acquireInterruptibly(1);
}
/**
* Try to lock this {@link NonReentrantLock}
*
* @return True if locking was successful, otherwise false
*/
@Override
public boolean tryLock() {
return tryAcquire(1);
}
/**
* Tries to lock this {@link NonReentrantLock} over a period of time
*
* @param time The maximum number of time units to attempt to get a lock for.
* @param unit The {@link TimeUnit} associated with the time parameter
* @return True if the lock was successful, otherwise false
* @throws InterruptedException The locking attempt was interrupted
*/
@Override
public boolean tryLock(long time, TimeUnit unit)
throws InterruptedException {
return tryAcquireNanos(1, unit.toNanos(time));
}
/**
* Unlocks this {@link NonReentrantLock}
*/
@Override
public void unlock() {
release(1);
}
/**
* Checks to see if this {@link NonReentrantLock} is held by the current {@link Thread}
*
* @return True if held by the current thread, otherwise false
*/
public boolean isHeldByCurrentThread() {
return isHeldExclusively();
}
/**
* Creates a new {@link Condition}
*
* @return The condition object
*/
@Override
public Condition newCondition() {
return new ConditionObject();
}
/**
* Try to acquire a lock
*
* @param acquires A number that is sent by acquiring methods
* @return True if a lock is acquired, otherwise false
*/
@Override
protected boolean tryAcquire(int acquires) {
if (compareAndSetState(0, 1)) {
owner = Thread.currentThread();
return true;
}
return false;
}
/**
* Tries to release the lock
*
* @param releases A number that is passed by the release methods
* @return True if a release is granted, otherwise false
*/
@Override
protected boolean tryRelease(int releases) {
if (Thread.currentThread() != owner) {
throw new IllegalMonitorStateException();
}
owner = null;
setState(0);
return true;
}
/**
* Checks to see if this {@link NonReentrantLock} is held exclusively by the current {@link Thread}
*
* @return True if held exclusively, otherwise false
*/
@Override
protected boolean isHeldExclusively() {
return getState() != 0 && owner == Thread.currentThread();
}
}

View File

@ -38,80 +38,6 @@ public final class StringUtil {
}
NEWLINE = newLine;
}
/**
* Strip an Object of it's ISO control characters.
*
* @param value
* The Object that should be stripped. This objects toString method will
* called and the result passed to {@link #stripControlCharacters(String)}.
* @return {@code String}
* A new String instance with its hexadecimal control characters replaced
* by a space. Or the unmodified String if it does not contain any ISO
* control characters.
*/
public static String stripControlCharacters(Object value) {
if (value == null) {
return null;
}
return stripControlCharacters(value.toString());
}
/**
* Strip a String of it's ISO control characters.
*
* @param value
* The String that should be stripped.
* @return {@code String}
* A new String instance with its hexadecimal control characters replaced
* by a space. Or the unmodified String if it does not contain any ISO
* control characters.
*/
public static String stripControlCharacters(String value) {
if (value == null) {
return null;
}
boolean hasControlChars = false;
for (int i = value.length() - 1; i >= 0; i --) {
if (Character.isISOControl(value.charAt(i))) {
hasControlChars = true;
break;
}
}
if (!hasControlChars) {
return value;
}
StringBuilder buf = new StringBuilder(value.length());
int i = 0;
// Skip initial control characters (i.e. left trim)
for (; i < value.length(); i ++) {
if (!Character.isISOControl(value.charAt(i))) {
break;
}
}
// Copy non control characters and substitute control characters with
// a space. The last control characters are trimmed.
boolean suppressingControlChars = false;
for (; i < value.length(); i ++) {
if (Character.isISOControl(value.charAt(i))) {
suppressingControlChars = true;
continue;
} else {
if (suppressingControlChars) {
suppressingControlChars = false;
buf.append(' ');
}
buf.append(value.charAt(i));
}
}
return buf.toString();
}
}

View File

@ -20,7 +20,7 @@ import java.util.regex.Pattern;
/**
* Accesses the system property swallowing a {@link SecurityException}.
*/
public final class SystemPropertyUtil {
final class SystemPropertyUtil {
/**
* Returns the value of the Java system property with the specified

View File

@ -1,34 +0,0 @@
/*
* Copyright 2012 The Netty Project
*
* The Netty Project licenses this file to you under the Apache License,
* version 2.0 (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
package io.netty.util.internal;
public class ThreadLocalBoolean extends ThreadLocal<Boolean> {
private final boolean defaultValue;
public ThreadLocalBoolean() {
this(false);
}
public ThreadLocalBoolean(boolean defaultValue) {
this.defaultValue = defaultValue;
}
@Override
protected Boolean initialValue() {
return defaultValue? Boolean.TRUE : Boolean.FALSE;
}
}

View File

@ -1,38 +0,0 @@
/*
* Copyright 2012 The Netty Project
*
* The Netty Project licenses this file to you under the Apache License,
* version 2.0 (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
package io.netty.util.internal;
import java.util.concurrent.Executor;
/**
* Disables shutdown of an {@link Executor} by wrapping the {@link Executor}.
*/
public class UnterminatableExecutor implements Executor {
private final Executor executor;
public UnterminatableExecutor(Executor executor) {
if (executor == null) {
throw new NullPointerException("executor");
}
this.executor = executor;
}
@Override
public void execute(Runnable command) {
executor.execute(command);
}
}

View File

@ -1,82 +0,0 @@
/*
* Copyright 2012 The Netty Project
*
* The Netty Project licenses this file to you under the Apache License,
* version 2.0 (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
package io.netty.util.internal;
import static org.junit.Assert.*;
import org.junit.Test;
/**
* Unit test for {@link StringUtil}.
*/
public class StringUtilTest {
@Test
public void stripControlCharactersObjectNull() {
assertNull(StringUtil.stripControlCharacters(null));
}
@Test
public void stripControlCharactersNull() {
assertNull(StringUtil.stripControlCharacters((String) null));
}
@Test
public void stripControlCharactersRightTrim() {
final char controlCode = 0x0000;
final Object object = "abbb" + controlCode;
assertEquals(5, ((String) object).length());
final String stripped = StringUtil.stripControlCharacters(object);
assertFalse(object.equals(stripped));
assertEquals(4, stripped.length());
}
@Test
public void stripControlCharactersLeftTrim() {
final char controlCode = 0x0000;
final String string = controlCode + "abbb";
assertEquals(5, string.length());
final String stripped = StringUtil.stripControlCharacters(string);
assertFalse(string.equals(stripped));
assertEquals(4, stripped.length());
}
@Test
public void stripControlCharacters() {
for (char i = 0x0000; i <= 0x001F; i ++) {
assertStripped(i);
}
for (char i = 0x007F; i <= 0x009F; i ++) {
assertStripped(i);
}
}
private static void assertStripped(final char controlCode) {
final Object object = "aaa" + controlCode + "bbb";
final String stripped = StringUtil.stripControlCharacters(object);
assertEquals("aaa bbb", stripped);
}
@Test
public void stripNonControlCharacter() {
final char controlCode = 0x002F;
final String string = controlCode + "abbb";
final String stripped = StringUtil.stripControlCharacters(string);
assertEquals("The string should be unchanged", string, stripped);
}
}

View File

@ -21,7 +21,6 @@ import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.logging.InternalLogger;
import io.netty.logging.InternalLoggerFactory;
import io.netty.util.internal.DeadLockProofWorker;
import java.util.ArrayList;
import java.util.Collection;
@ -216,7 +215,6 @@ public class DefaultChannelGroupFuture implements ChannelGroupFuture {
synchronized (this) {
while (!done) {
checkDeadLock();
waiters++;
try {
wait();
@ -244,7 +242,6 @@ public class DefaultChannelGroupFuture implements ChannelGroupFuture {
boolean interrupted = false;
synchronized (this) {
while (!done) {
checkDeadLock();
waiters++;
try {
wait();
@ -298,7 +295,6 @@ public class DefaultChannelGroupFuture implements ChannelGroupFuture {
return done;
}
checkDeadLock();
waiters++;
try {
for (;;) {
@ -332,15 +328,6 @@ public class DefaultChannelGroupFuture implements ChannelGroupFuture {
}
}
private static void checkDeadLock() {
if (DeadLockProofWorker.PARENT.get() != null) {
throw new IllegalStateException(
"await*() in I/O thread causes a dead lock or " +
"sudden performance drop. Use addListener() instead or " +
"call await*() from a different thread.");
}
}
void setDone() {
synchronized (this) {
// Allow only once.