KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jfun > yan > OnException


1 /*****************************************************************************
2  * Copyright (C) Zephyr Business Solution. All rights reserved. *
3  * ------------------------------------------------------------------------- *
4  * The software in this package is published under the terms of the BSD *
5  * style license a copy of which has been included with this distribution in *
6  * the LICENSE.txt file. *
7  *****************************************************************************/

8
9 /*
10  * Created on Apr 9, 2005
11  *
12  * Author Ben Yu
13  * ZBS
14  */

15 package jfun.yan;
16
17 import jfun.util.Misc;
18
19 /**
20  * Recovers from a certain exception.
21  * <p>
22  * Zephyr Business Solution
23  *
24  * @author Ben Yu
25  *
26  */

27 final class OnException<T> implements Recovery<T>, java.io.Serializable JavaDoc {
28   private final Creator<T> alt;
29   private final Class JavaDoc<? extends Throwable JavaDoc> etype;
30   
31   OnException(final Creator<T> alt, final Class JavaDoc<? extends Throwable JavaDoc> etype) {
32     this.alt = alt;
33     this.etype = etype;
34   }
35   
36   public boolean equals(Object JavaDoc obj) {
37     if(obj instanceof OnException){
38       final OnException other = (OnException)obj;
39       return etype.equals(other.etype) && alt.equals(other.alt);
40     }
41     else return false;
42   }
43   public int hashCode() {
44     return alt.hashCode()*31+etype.hashCode();
45   }
46   public String JavaDoc toString() {
47     return "recover " + Misc.getTypeName(etype)
48       + " to " + alt;
49   }
50   public Creator<T> recover(RuntimeException JavaDoc e){
51     if(etype.isInstance(e)){
52       return alt;
53     }
54     else throw e;
55   }
56
57 }
58
Popular Tags