Revision 276
org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.lib/src/main/java/org/gvsig/tools/util/Callable.java | ||
---|---|---|
1 |
package org.gvsig.tools.util; |
|
2 |
|
|
3 |
/** |
|
4 |
* A task that returns a result and may throw an exception. |
|
5 |
* Implementors define a single method with no arguments called call. |
|
6 |
* |
|
7 |
* This interface is similar to the same interface of java 1.5 and this |
|
8 |
* is here for java 1.4 compatibility. |
|
9 |
* |
|
10 |
* @author gvSIG Team - jjdelcerro |
|
11 |
* |
|
12 |
*/ |
|
13 |
public interface Callable { |
|
14 |
|
|
15 |
/** |
|
16 |
* Computes a result, or throws an exception if unable to do so. |
|
17 |
* |
|
18 |
* @return computed result |
|
19 |
* @throws Exception if unable to compute a result |
|
20 |
*/ |
|
21 |
public Object call() throws Exception; |
|
22 |
|
|
23 |
} |
|
0 | 24 |
org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.lib/src/main/java/org/gvsig/tools/util/impl/DefaultCaller.java | ||
---|---|---|
1 |
package org.gvsig.tools.util.impl; |
|
2 |
|
|
3 |
import java.util.ArrayList; |
|
4 |
import java.util.Iterator; |
|
5 |
import java.util.List; |
|
6 |
|
|
7 |
import org.gvsig.tools.util.Callable; |
|
8 |
import org.gvsig.tools.util.Caller; |
|
9 |
|
|
10 |
public class DefaultCaller implements Caller { |
|
11 |
|
|
12 |
private boolean collectExceptions; |
|
13 |
private List callables; |
|
14 |
private List exceptions; |
|
15 |
|
|
16 |
public DefaultCaller() { |
|
17 |
this.collectExceptions = true; |
|
18 |
this.callables = new ArrayList(); |
|
19 |
this.exceptions = null; |
|
20 |
} |
|
21 |
|
|
22 |
public boolean call() { |
|
23 |
List exceptions = new ArrayList(); |
|
24 |
Iterator it = this.callables.iterator(); |
|
25 |
while( it.hasNext() ) { |
|
26 |
Callable callable = (Callable)(it.next()); |
|
27 |
try { |
|
28 |
callable.call(); |
|
29 |
} catch (Exception e) { |
|
30 |
exceptions.add(e); |
|
31 |
} |
|
32 |
} |
|
33 |
if( exceptions.size()>0 ) { |
|
34 |
this.exceptions = exceptions; |
|
35 |
return false; |
|
36 |
} |
|
37 |
return true; |
|
38 |
} |
|
39 |
|
|
40 |
public boolean getCollectExceptions() { |
|
41 |
return this.collectExceptions; |
|
42 |
} |
|
43 |
|
|
44 |
public void setCollectExceptions(boolean collectExceptions) { |
|
45 |
this.collectExceptions = collectExceptions; |
|
46 |
} |
|
47 |
|
|
48 |
public void add(Callable callable) { |
|
49 |
this.callables.add(callable); |
|
50 |
} |
|
51 |
|
|
52 |
public List getExceptions() { |
|
53 |
return this.exceptions; |
|
54 |
} |
|
55 |
|
|
56 |
} |
|
0 | 57 |
org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.lib/src/main/java/org/gvsig/tools/util/Caller.java | ||
---|---|---|
1 |
package org.gvsig.tools.util; |
|
2 |
|
|
3 |
import java.util.List; |
|
4 |
|
|
5 |
/** |
|
6 |
* |
|
7 |
* This is a utility class for call a list of callables. |
|
8 |
* |
|
9 |
* It can collect exceptions and continue to call the next |
|
10 |
* callable in the list. |
|
11 |
* |
|
12 |
* @author gvSIG Team - jjdelcerro |
|
13 |
* |
|
14 |
*/ |
|
15 |
public interface Caller { |
|
16 |
|
|
17 |
/** |
|
18 |
* Get if the Caller can collect exceptions or |
|
19 |
* exit in the first exception produced. |
|
20 |
* |
|
21 |
* @return boolean that indicate that collect exceptions. |
|
22 |
*/ |
|
23 |
public boolean getCollectExceptions(); |
|
24 |
|
|
25 |
/** |
|
26 |
* Set in the caller the behabior of collect al exceptions |
|
27 |
* and continue calling the next callable in the list of our |
|
28 |
* callables. |
|
29 |
* |
|
30 |
* @param collectExceptions |
|
31 |
*/ |
|
32 |
public void setCollectExceptions(boolean collectExceptions); |
|
33 |
|
|
34 |
/** |
|
35 |
* Add a callable to the list of callables of the caller. |
|
36 |
* @param callable |
|
37 |
*/ |
|
38 |
public void add(Callable callable); |
|
39 |
|
|
40 |
/** |
|
41 |
* Return the list of collected exceptions or null. |
|
42 |
* |
|
43 |
* @return list of exceptions |
|
44 |
*/ |
|
45 |
public List getExceptions(); |
|
46 |
|
|
47 |
/** |
|
48 |
* Call all callables in the list of callables of the caller. |
|
49 |
* |
|
50 |
* @return true if no exceptions produced |
|
51 |
*/ |
|
52 |
public boolean call(); |
|
53 |
|
|
54 |
} |
|
0 | 55 |
Also available in: Unified diff