Clean up: Moving obsolete stuff to sandbox.

This commit is contained in:
Harald Kuhr 2012-06-21 16:37:27 +02:00
parent 5bac1e3a2b
commit 2cbdd7fd82
8 changed files with 125 additions and 79 deletions

View File

@ -1,9 +1,6 @@
package com.twelvemonkeys.lang;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.lang.reflect.UndeclaredThrowableException;
import java.sql.SQLException;
import static com.twelvemonkeys.lang.Validate.notNull;
@ -18,8 +15,8 @@ public final class ExceptionUtil {
/**
* Re-throws an exception, either as-is if the exception was already unchecked, otherwise wrapped in
* a {@link RuntimeException}.
* "Expected" exception types are wrapped in {@link RuntimeException}s, while
* a {@link RuntimeException} subtype.
* "Expected" exception types are wrapped in {@link RuntimeException}s directly, while
* "unexpected" exception types are wrapped in {@link java.lang.reflect.UndeclaredThrowableException}s.
*
* @param pThrowable the exception to launder
@ -51,7 +48,8 @@ public final class ExceptionUtil {
throwAs(RuntimeException.class, pThrowable);
}
@SuppressWarnings({"unchecked"})
/*@SafeVarargs*/
@SuppressWarnings({"unchecked", "varargs"})
/*public*/ static void handle(final Throwable pThrowable, final ThrowableHandler<? extends Throwable>... pHandlers) {
handleImpl(pThrowable, (ThrowableHandler<Throwable>[]) pHandlers);
}
@ -88,55 +86,4 @@ public final class ExceptionUtil {
public abstract void handle(T pThrowable);
}
@SuppressWarnings({"InfiniteLoopStatement"})
public static void main(String[] pArgs) {
while (true) {
foo();
}
}
private static void foo() {
try {
bar();
}
catch (Throwable t) {
handle(t,
new ThrowableHandler<IOException>(IOException.class) {
public void handle(final IOException pThrowable) {
System.out.println("IOException: " + pThrowable + " handled");
}
},
new ThrowableHandler<Exception>(SQLException.class, NumberFormatException.class) {
public void handle(final Exception pThrowable) {
System.out.println("Exception: " + pThrowable + " handled");
}
},
new ThrowableHandler<Throwable>(Throwable.class) {
public void handle(final Throwable pThrowable) {
System.err.println("Generic throwable: " + pThrowable + " NOT handled");
throwUnchecked(pThrowable);
}
}
);
}
}
private static void bar() {
baz();
}
@SuppressWarnings({"ThrowableInstanceNeverThrown"})
private static void baz() {
double random = Math.random();
if (random < (1.0 / 3.0)) {
throwUnchecked(new FileNotFoundException("FNF Boo"));
}
if (random < (2.0 / 3.0)) {
throwUnchecked(new SQLException("SQL Boo"));
}
else {
throwUnchecked(new Exception("Some Boo"));
}
}
}

View File

@ -45,7 +45,7 @@ public interface Resource {
*
* The id might be a {@code URL}, a {@code File} or a string
* representation of some system resource.
* It will always be equal to the {@code reourceId} parameter
* It will always be equal to the {@code resourceId} parameter
* sent to the {@link ResourceMonitor#addResourceChangeListener} method
* for a given resource.
*
@ -61,9 +61,9 @@ public interface Resource {
public URL asURL();
/**
* Opens an input stream, that reads from this reource.
* Opens an input stream, that reads from this resource.
*
* @return an intput stream, that reads frmo this resource.
* @return an input stream, that reads from this resource.
*
* @throws IOException if an I/O exception occurs
*/

View File

@ -41,8 +41,8 @@ import java.util.EventListener;
public interface ResourceChangeListener extends EventListener {
/**
* Invoked when a resource is changed.
* Implementations that listens for multiple eventes, should check that
* {@code Resource.getId()} is equal to the {@code reourceId} parameter
* Implementations that listens for multiple events, should check that
* {@code Resource.getId()} is equal to the {@code resourceId} parameter
* sent to the {@link ResourceMonitor#addResourceChangeListener} method.
*
* @param pResource changed file/url/etc.

View File

@ -135,13 +135,13 @@ public abstract class ResourceMonitor {
*
*/
private class ResourceMonitorTask extends TimerTask {
ResourceChangeListener mListener;
Resource mMonitoredResource;
long mLastModified;
ResourceChangeListener listener;
Resource monitoredResource;
long lastModified;
public ResourceMonitorTask(ResourceChangeListener pListener, Object pResourceId) throws IOException {
mListener = pListener;
mLastModified = 0;
listener = pListener;
lastModified = 0;
String resourceId = null;
File file = null;
@ -159,13 +159,13 @@ public abstract class ResourceMonitor {
}
else if (pResourceId instanceof String) {
resourceId = (String) pResourceId; // This one might be looked up
file = new File((String) resourceId);
file = new File(resourceId);
}
if (file != null && file.exists()) {
// Easy, this is a file
mMonitoredResource = new FileResource(pResourceId, file);
//System.out.println("File: " + mMonitoredResource);
monitoredResource = new FileResource(pResourceId, file);
//System.out.println("File: " + monitoredResource);
}
else {
// No file there, but is it on CLASSPATH?
@ -179,28 +179,28 @@ public abstract class ResourceMonitor {
if (url != null && "file".equals(url.getProtocol())
&& (file = new File(url.getFile())).exists()) {
// It's a file in classpath, so try this as an optimization
mMonitoredResource = new FileResource(pResourceId, file);
//System.out.println("File: " + mMonitoredResource);
monitoredResource = new FileResource(pResourceId, file);
//System.out.println("File: " + monitoredResource);
}
else if (url != null) {
// No, not a file, might even be an external resource
mMonitoredResource = new URLResource(pResourceId, url);
//System.out.println("URL: " + mMonitoredResource);
monitoredResource = new URLResource(pResourceId, url);
//System.out.println("URL: " + monitoredResource);
}
else {
throw new FileNotFoundException(resourceId);
}
}
mLastModified = mMonitoredResource.lastModified();
lastModified = monitoredResource.lastModified();
}
public void run() {
long lastModified = mMonitoredResource.lastModified();
long lastModified = monitoredResource.lastModified();
if (lastModified != mLastModified) {
mLastModified = lastModified;
fireResourceChangeEvent(mListener, mMonitoredResource);
if (lastModified != this.lastModified) {
this.lastModified = lastModified;
fireResourceChangeEvent(listener, monitoredResource);
}
}
}

View File

@ -0,0 +1,99 @@
/*
* Copyright (c) 2012, Harald Kuhr
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name "TwelveMonkeys" nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package com.twelvemonkeys.lang;
import org.junit.Ignore;
import org.junit.Test;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.SQLException;
/**
* ExceptionUtilTest
*
* @author <a href="mailto:harald.kuhr@gmail.com">Harald Kuhr</a>
* @author last modified by $Author: haraldk$
* @version $Id: ExceptionUtilTest.java,v 1.0 11.04.12 16:07 haraldk Exp$
*/
@Ignore("Under development")
public class ExceptionUtilTest {
@Test(expected = BadException.class)
@SuppressWarnings({"InfiniteLoopStatement"})
public void test() {
while (true) {
foo();
}
}
@SuppressWarnings({"unchecked", "varargs"})
private static void foo() {
try {
bar();
}
catch (Throwable t) {
ExceptionUtil.handle(t,
new ExceptionUtil.ThrowableHandler<IOException>(IOException.class) {
public void handle(final IOException pThrowable) {
System.out.println("IOException: " + pThrowable + " handled");
}
},
new ExceptionUtil.ThrowableHandler<Exception>(SQLException.class, NumberFormatException.class) {
public void handle(final Exception pThrowable) {
System.out.println("Exception: " + pThrowable + " handled");
}
}
);
}
}
private static void bar() {
baz();
}
@SuppressWarnings({"ThrowableInstanceNeverThrown"})
private static void baz() {
double random = Math.random();
if (random < (2.0 / 3.0)) {
ExceptionUtil.throwUnchecked(new FileNotFoundException("FNF Boo"));
}
if (random < (5.0 / 6.0)) {
ExceptionUtil.throwUnchecked(new SQLException("SQL Boo"));
}
else {
ExceptionUtil.throwUnchecked(new BadException("Some Boo"));
}
}
static final class BadException extends Exception {
public BadException(String s) {
super(s);
}
}
}