mirror of
https://github.com/haraldk/TwelveMonkeys.git
synced 2025-08-03 11:35:29 -04:00
Clean up: Moving obsolete stuff to sandbox.
This commit is contained in:
parent
5bac1e3a2b
commit
2cbdd7fd82
@ -1,9 +1,6 @@
|
|||||||
package com.twelvemonkeys.lang;
|
package com.twelvemonkeys.lang;
|
||||||
|
|
||||||
import java.io.FileNotFoundException;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.lang.reflect.UndeclaredThrowableException;
|
import java.lang.reflect.UndeclaredThrowableException;
|
||||||
import java.sql.SQLException;
|
|
||||||
|
|
||||||
import static com.twelvemonkeys.lang.Validate.notNull;
|
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
|
* Re-throws an exception, either as-is if the exception was already unchecked, otherwise wrapped in
|
||||||
* a {@link RuntimeException}.
|
* a {@link RuntimeException} subtype.
|
||||||
* "Expected" exception types are wrapped in {@link RuntimeException}s, while
|
* "Expected" exception types are wrapped in {@link RuntimeException}s directly, while
|
||||||
* "unexpected" exception types are wrapped in {@link java.lang.reflect.UndeclaredThrowableException}s.
|
* "unexpected" exception types are wrapped in {@link java.lang.reflect.UndeclaredThrowableException}s.
|
||||||
*
|
*
|
||||||
* @param pThrowable the exception to launder
|
* @param pThrowable the exception to launder
|
||||||
@ -51,7 +48,8 @@ public final class ExceptionUtil {
|
|||||||
throwAs(RuntimeException.class, pThrowable);
|
throwAs(RuntimeException.class, pThrowable);
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings({"unchecked"})
|
/*@SafeVarargs*/
|
||||||
|
@SuppressWarnings({"unchecked", "varargs"})
|
||||||
/*public*/ static void handle(final Throwable pThrowable, final ThrowableHandler<? extends Throwable>... pHandlers) {
|
/*public*/ static void handle(final Throwable pThrowable, final ThrowableHandler<? extends Throwable>... pHandlers) {
|
||||||
handleImpl(pThrowable, (ThrowableHandler<Throwable>[]) pHandlers);
|
handleImpl(pThrowable, (ThrowableHandler<Throwable>[]) pHandlers);
|
||||||
}
|
}
|
||||||
@ -88,55 +86,4 @@ public final class ExceptionUtil {
|
|||||||
|
|
||||||
public abstract void handle(T pThrowable);
|
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"));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
@ -45,7 +45,7 @@ public interface Resource {
|
|||||||
*
|
*
|
||||||
* The id might be a {@code URL}, a {@code File} or a string
|
* The id might be a {@code URL}, a {@code File} or a string
|
||||||
* representation of some system resource.
|
* 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
|
* sent to the {@link ResourceMonitor#addResourceChangeListener} method
|
||||||
* for a given resource.
|
* for a given resource.
|
||||||
*
|
*
|
||||||
@ -61,9 +61,9 @@ public interface Resource {
|
|||||||
public URL asURL();
|
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
|
* @throws IOException if an I/O exception occurs
|
||||||
*/
|
*/
|
@ -41,8 +41,8 @@ import java.util.EventListener;
|
|||||||
public interface ResourceChangeListener extends EventListener {
|
public interface ResourceChangeListener extends EventListener {
|
||||||
/**
|
/**
|
||||||
* Invoked when a resource is changed.
|
* Invoked when a resource is changed.
|
||||||
* Implementations that listens for multiple eventes, should check that
|
* Implementations that listens for multiple events, should check that
|
||||||
* {@code Resource.getId()} is equal to the {@code reourceId} parameter
|
* {@code Resource.getId()} is equal to the {@code resourceId} parameter
|
||||||
* sent to the {@link ResourceMonitor#addResourceChangeListener} method.
|
* sent to the {@link ResourceMonitor#addResourceChangeListener} method.
|
||||||
*
|
*
|
||||||
* @param pResource changed file/url/etc.
|
* @param pResource changed file/url/etc.
|
@ -135,13 +135,13 @@ public abstract class ResourceMonitor {
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
private class ResourceMonitorTask extends TimerTask {
|
private class ResourceMonitorTask extends TimerTask {
|
||||||
ResourceChangeListener mListener;
|
ResourceChangeListener listener;
|
||||||
Resource mMonitoredResource;
|
Resource monitoredResource;
|
||||||
long mLastModified;
|
long lastModified;
|
||||||
|
|
||||||
public ResourceMonitorTask(ResourceChangeListener pListener, Object pResourceId) throws IOException {
|
public ResourceMonitorTask(ResourceChangeListener pListener, Object pResourceId) throws IOException {
|
||||||
mListener = pListener;
|
listener = pListener;
|
||||||
mLastModified = 0;
|
lastModified = 0;
|
||||||
|
|
||||||
String resourceId = null;
|
String resourceId = null;
|
||||||
File file = null;
|
File file = null;
|
||||||
@ -159,13 +159,13 @@ public abstract class ResourceMonitor {
|
|||||||
}
|
}
|
||||||
else if (pResourceId instanceof String) {
|
else if (pResourceId instanceof String) {
|
||||||
resourceId = (String) pResourceId; // This one might be looked up
|
resourceId = (String) pResourceId; // This one might be looked up
|
||||||
file = new File((String) resourceId);
|
file = new File(resourceId);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (file != null && file.exists()) {
|
if (file != null && file.exists()) {
|
||||||
// Easy, this is a file
|
// Easy, this is a file
|
||||||
mMonitoredResource = new FileResource(pResourceId, file);
|
monitoredResource = new FileResource(pResourceId, file);
|
||||||
//System.out.println("File: " + mMonitoredResource);
|
//System.out.println("File: " + monitoredResource);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
// No file there, but is it on CLASSPATH?
|
// No file there, but is it on CLASSPATH?
|
||||||
@ -179,28 +179,28 @@ public abstract class ResourceMonitor {
|
|||||||
if (url != null && "file".equals(url.getProtocol())
|
if (url != null && "file".equals(url.getProtocol())
|
||||||
&& (file = new File(url.getFile())).exists()) {
|
&& (file = new File(url.getFile())).exists()) {
|
||||||
// It's a file in classpath, so try this as an optimization
|
// It's a file in classpath, so try this as an optimization
|
||||||
mMonitoredResource = new FileResource(pResourceId, file);
|
monitoredResource = new FileResource(pResourceId, file);
|
||||||
//System.out.println("File: " + mMonitoredResource);
|
//System.out.println("File: " + monitoredResource);
|
||||||
}
|
}
|
||||||
else if (url != null) {
|
else if (url != null) {
|
||||||
// No, not a file, might even be an external resource
|
// No, not a file, might even be an external resource
|
||||||
mMonitoredResource = new URLResource(pResourceId, url);
|
monitoredResource = new URLResource(pResourceId, url);
|
||||||
//System.out.println("URL: " + mMonitoredResource);
|
//System.out.println("URL: " + monitoredResource);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
throw new FileNotFoundException(resourceId);
|
throw new FileNotFoundException(resourceId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
mLastModified = mMonitoredResource.lastModified();
|
lastModified = monitoredResource.lastModified();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void run() {
|
public void run() {
|
||||||
long lastModified = mMonitoredResource.lastModified();
|
long lastModified = monitoredResource.lastModified();
|
||||||
|
|
||||||
if (lastModified != mLastModified) {
|
if (lastModified != this.lastModified) {
|
||||||
mLastModified = lastModified;
|
this.lastModified = lastModified;
|
||||||
fireResourceChangeEvent(mListener, mMonitoredResource);
|
fireResourceChangeEvent(listener, monitoredResource);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user