01: /*
02: * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com>
03: * Distributed under the terms of either:
04: * - the common development and distribution license (CDDL), v1.0; or
05: * - the GNU Lesser General Public License, v2.1 or later
06: * $Id: InputStreamUser.java 3634 2007-01-08 21:42:24Z gbevin $
07: */
08: package com.uwyn.rife.tools;
09:
10: import com.uwyn.rife.tools.ExceptionUtils;
11: import java.io.InputStream;
12: import java.util.logging.Logger;
13:
14: /**
15: * By extending this class it's possible to provide the logic that should be
16: * executed by methods that allow interaction with an <code>InputStream</code>.
17: * <p>This class has both a default constructor and one that can take a data
18: * object. This can be handy when using it as an extending anonymous inner
19: * class when you need to use variables inside the inner class that are
20: * cumbersome to change to <code>final</code> in the enclosing class.
21: *
22: * @author Geert Bevin (gbevin[remove] at uwyn dot com)
23: * @version $Revision: 3634 $
24: * @since 1.0
25: */
26: public abstract class InputStreamUser<ResultType, DataType> implements
27: Cloneable {
28: protected DataType mData = null;
29:
30: public InputStreamUser() {
31: }
32:
33: public InputStreamUser(DataType data) {
34: mData = data;
35: }
36:
37: public DataType getData() {
38: return mData;
39: }
40:
41: /**
42: * Calling this method makes it possible to throw a checked exception from
43: * within this class.
44: * <p>To catch it you should surround the using method with a
45: * <code>try-catch</code> block that catching
46: * <code>InnerClassException</code>. The original exception is then
47: * available through <code>getCause()</code> and can for example be
48: * rethrown.
49: *
50: * @exception InnerClassException when a checked exception needs to be
51: * thrown from within this class and caught outside the caller.
52: * @since 1.0
53: */
54: public void throwException(Exception exception)
55: throws InnerClassException {
56: throw new InnerClassException(exception);
57: }
58:
59: /**
60: * Should be implemented by all extending classes.
61: *
62: * @since 1.0
63: */
64: public abstract ResultType useInputStream(InputStream stream)
65: throws InnerClassException;
66:
67: /**
68: * Simply clones the instance with the default clone method since this
69: * class contains no member variables.
70: *
71: * @since 1.0
72: */
73: public Object clone() {
74: try {
75: return super .clone();
76: } catch (CloneNotSupportedException e) {
77: // this should never happen
78: Logger.getLogger("com.uwyn.rife.database").severe(
79: ExceptionUtils.getExceptionStackTrace(e));
80: return null;
81: }
82: }
83: }
|