From 2cbdd7fd824822d9ad38f7b89c20ee5a1a940c6a Mon Sep 17 00:00:00 2001 From: Harald Kuhr Date: Thu, 21 Jun 2012 16:37:27 +0200 Subject: [PATCH] Clean up: Moving obsolete stuff to sandbox. --- .../com/twelvemonkeys/lang/ExceptionUtil.java | 61 +----------- .../twelvemonkeys/util/AbstractResource.java | 0 .../com/twelvemonkeys/util/FileResource.java | 0 .../java/com/twelvemonkeys/util/Resource.java | 6 +- .../util/ResourceChangeListener.java | 4 +- .../twelvemonkeys/util/ResourceMonitor.java | 34 +++---- .../com/twelvemonkeys/util/URLResource.java | 0 .../twelvemonkeys/lang/ExceptionUtilTest.java | 99 +++++++++++++++++++ 8 files changed, 125 insertions(+), 79 deletions(-) rename {common/common-lang => sandbox/sandbox-common}/src/main/java/com/twelvemonkeys/lang/ExceptionUtil.java (61%) rename {common/common-lang => sandbox/sandbox-common}/src/main/java/com/twelvemonkeys/util/AbstractResource.java (100%) rename {common/common-lang => sandbox/sandbox-common}/src/main/java/com/twelvemonkeys/util/FileResource.java (100%) rename {common/common-lang => sandbox/sandbox-common}/src/main/java/com/twelvemonkeys/util/Resource.java (91%) rename {common/common-lang => sandbox/sandbox-common}/src/main/java/com/twelvemonkeys/util/ResourceChangeListener.java (91%) rename {common/common-lang => sandbox/sandbox-common}/src/main/java/com/twelvemonkeys/util/ResourceMonitor.java (88%) rename {common/common-lang => sandbox/sandbox-common}/src/main/java/com/twelvemonkeys/util/URLResource.java (100%) create mode 100644 sandbox/sandbox-common/src/test/java/com/twelvemonkeys/lang/ExceptionUtilTest.java diff --git a/common/common-lang/src/main/java/com/twelvemonkeys/lang/ExceptionUtil.java b/sandbox/sandbox-common/src/main/java/com/twelvemonkeys/lang/ExceptionUtil.java similarity index 61% rename from common/common-lang/src/main/java/com/twelvemonkeys/lang/ExceptionUtil.java rename to sandbox/sandbox-common/src/main/java/com/twelvemonkeys/lang/ExceptionUtil.java index 32a8ffc9..9d1f89cf 100755 --- a/common/common-lang/src/main/java/com/twelvemonkeys/lang/ExceptionUtil.java +++ b/sandbox/sandbox-common/src/main/java/com/twelvemonkeys/lang/ExceptionUtil.java @@ -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... pHandlers) { handleImpl(pThrowable, (ThrowableHandler[]) 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.class) { - public void handle(final IOException pThrowable) { - System.out.println("IOException: " + pThrowable + " handled"); - } - }, - new ThrowableHandler(SQLException.class, NumberFormatException.class) { - public void handle(final Exception pThrowable) { - System.out.println("Exception: " + pThrowable + " handled"); - } - }, - new ThrowableHandler(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")); - } - } } diff --git a/common/common-lang/src/main/java/com/twelvemonkeys/util/AbstractResource.java b/sandbox/sandbox-common/src/main/java/com/twelvemonkeys/util/AbstractResource.java similarity index 100% rename from common/common-lang/src/main/java/com/twelvemonkeys/util/AbstractResource.java rename to sandbox/sandbox-common/src/main/java/com/twelvemonkeys/util/AbstractResource.java diff --git a/common/common-lang/src/main/java/com/twelvemonkeys/util/FileResource.java b/sandbox/sandbox-common/src/main/java/com/twelvemonkeys/util/FileResource.java similarity index 100% rename from common/common-lang/src/main/java/com/twelvemonkeys/util/FileResource.java rename to sandbox/sandbox-common/src/main/java/com/twelvemonkeys/util/FileResource.java diff --git a/common/common-lang/src/main/java/com/twelvemonkeys/util/Resource.java b/sandbox/sandbox-common/src/main/java/com/twelvemonkeys/util/Resource.java similarity index 91% rename from common/common-lang/src/main/java/com/twelvemonkeys/util/Resource.java rename to sandbox/sandbox-common/src/main/java/com/twelvemonkeys/util/Resource.java index 5fec4a5b..18f04563 100755 --- a/common/common-lang/src/main/java/com/twelvemonkeys/util/Resource.java +++ b/sandbox/sandbox-common/src/main/java/com/twelvemonkeys/util/Resource.java @@ -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 */ diff --git a/common/common-lang/src/main/java/com/twelvemonkeys/util/ResourceChangeListener.java b/sandbox/sandbox-common/src/main/java/com/twelvemonkeys/util/ResourceChangeListener.java similarity index 91% rename from common/common-lang/src/main/java/com/twelvemonkeys/util/ResourceChangeListener.java rename to sandbox/sandbox-common/src/main/java/com/twelvemonkeys/util/ResourceChangeListener.java index f510ca61..17208c81 100755 --- a/common/common-lang/src/main/java/com/twelvemonkeys/util/ResourceChangeListener.java +++ b/sandbox/sandbox-common/src/main/java/com/twelvemonkeys/util/ResourceChangeListener.java @@ -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. diff --git a/common/common-lang/src/main/java/com/twelvemonkeys/util/ResourceMonitor.java b/sandbox/sandbox-common/src/main/java/com/twelvemonkeys/util/ResourceMonitor.java similarity index 88% rename from common/common-lang/src/main/java/com/twelvemonkeys/util/ResourceMonitor.java rename to sandbox/sandbox-common/src/main/java/com/twelvemonkeys/util/ResourceMonitor.java index 97778d5b..c594de70 100755 --- a/common/common-lang/src/main/java/com/twelvemonkeys/util/ResourceMonitor.java +++ b/sandbox/sandbox-common/src/main/java/com/twelvemonkeys/util/ResourceMonitor.java @@ -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); } } } diff --git a/common/common-lang/src/main/java/com/twelvemonkeys/util/URLResource.java b/sandbox/sandbox-common/src/main/java/com/twelvemonkeys/util/URLResource.java similarity index 100% rename from common/common-lang/src/main/java/com/twelvemonkeys/util/URLResource.java rename to sandbox/sandbox-common/src/main/java/com/twelvemonkeys/util/URLResource.java diff --git a/sandbox/sandbox-common/src/test/java/com/twelvemonkeys/lang/ExceptionUtilTest.java b/sandbox/sandbox-common/src/test/java/com/twelvemonkeys/lang/ExceptionUtilTest.java new file mode 100644 index 00000000..90b5aed5 --- /dev/null +++ b/sandbox/sandbox-common/src/test/java/com/twelvemonkeys/lang/ExceptionUtilTest.java @@ -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 Harald Kuhr + * @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.class) { + public void handle(final IOException pThrowable) { + System.out.println("IOException: " + pThrowable + " handled"); + } + }, + new ExceptionUtil.ThrowableHandler(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); + } + } +}