1 8 9 15 package jfun.yan; 16 17 import jfun.util.Misc; 18 19 27 final class OnException<T> implements Recovery<T>, java.io.Serializable { 28 private final Creator<T> alt; 29 private final Class <? extends Throwable > etype; 30 31 OnException(final Creator<T> alt, final Class <? extends Throwable > etype) { 32 this.alt = alt; 33 this.etype = etype; 34 } 35 36 public boolean equals(Object 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 toString() { 47 return "recover " + Misc.getTypeName(etype) 48 + " to " + alt; 49 } 50 public Creator<T> recover(RuntimeException e){ 51 if(etype.isInstance(e)){ 52 return alt; 53 } 54 else throw e; 55 } 56 57 } 58 | Popular Tags |