1 9 10 package org.jboss.util.coerce; 11 12 import java.util.Map ; 13 import java.util.HashMap ; 14 import java.util.Collections ; 15 16 import org.jboss.util.CoercionException; 17 import org.jboss.util.NotCoercibleException; 18 import org.jboss.util.NullArgumentException; 19 import org.jboss.util.NotImplementedException; 20 21 36 public abstract class CoercionHandler 37 { 38 47 public abstract Object coerce(Object value, Class type) 48 throws CoercionException; 49 50 57 public Class getType() { 58 throw new NotImplementedException("handler is not bound"); 59 } 60 61 65 66 private static Map handlers = Collections.synchronizedMap(new HashMap ()); 67 68 69 static { 70 install(new CharacterHandler()); 72 install(new ClassHandler()); 73 install(new FileHandler()); 74 } 75 76 83 public static void install(Class type, CoercionHandler handler) { 84 if (type == null) 85 throw new NullArgumentException("type"); 86 if (handler == null) 87 throw new NullArgumentException("handler"); 88 89 handlers.put(type, handler); 90 } 91 92 99 public static void install(BoundCoercionHandler handler) { 100 if (handler == null) 101 throw new NullArgumentException("handler"); 102 103 handlers.put(handler.getType(), handler); 104 } 105 106 113 public static void uninstall(Class type) { 114 if (type == null) 115 throw new NullArgumentException("type"); 116 117 handlers.remove(type); 118 } 119 120 126 public static boolean isInstalled(Class type) { 127 return handlers.containsKey(type); 128 } 129 130 138 public static CoercionHandler lookup(Class type) { 139 if (type == null) 140 throw new NullArgumentException("type"); 141 142 return (CoercionHandler)handlers.get(type); 143 } 144 145 153 public static CoercionHandler create(Class type) { 154 CoercionHandler handler = lookup(type); 155 if (handler == null) 156 throw new CoercionException 157 ("no installed handler for type: " + type); 158 159 return handler; 160 } 161 } 162 | Popular Tags |