1 8 14 package jfun.yan; 15 16 17 38 public class Monad { 39 55 public static <From,To> Component<To> bind(Creator<From> c1, ComponentBinder<From,To> binder){ 56 return new MbindComponent<From,To>(c1, binder).label(); 57 } 58 76 public static <From,To> Component<To> bind(Creator<From> c1, Binder<From,To> binder){ 77 return bind(c1, toComponentBinder(binder)); 78 } 79 87 public static <A,B> Component<B> seq(final Creator<A> c1, final Creator<B> c2){ 88 return sequence(new Creator[]{c1, c2}); 91 } 92 93 99 public static <T> Component<T> sequence(final Creator<T>... ccs){ 100 if(ccs.length==0) return Components.value(null); 101 if(ccs.length==1) return Components.adapt(ccs[0]); 102 return new SequenceComponent<T>(ccs).label(); 103 } 104 111 public static <From,To> Component<To> map(final Creator<From> cc, final jfun.yan.Map<From,To> m){ 112 return new MappedComponent<From,To>(cc, m) 113 .label(); 114 } 115 124 public static <A,B,R> Component<R> map(Creator<A> c1, Creator<B> c2, Map2<A,B,R> m){ 125 return new MappedComponent2<A,B,R>(c1, c2, m) 126 .label(); 127 } 128 138 public static <A,B,C,R> Component<R> map(Creator<A> c1, Creator<B> c2, Creator<C> c3, 139 Map3<A,B,C,R> m){ 140 return new MappedComponent3<A,B,C,R>(c1, c2, c3, m) 141 .label(); 142 } 143 154 public static <A,B,C,D,R> Component<R> map(Creator<A> c1, Creator<B> c2, Creator<C> c3, 155 Creator<D> c4, Map4<A,B,C,D,R> m){ 156 return new MappedComponent4<A,B,C,D,R>(c1, c2, c3, c4, m) 157 .label(); 158 } 159 171 public static <A,B,C,D,E,R> Component<R> map(Creator<A> c1, Creator<B> c2, Creator<C> c3, 172 Creator<D> c4, Creator<E> c5, Map5<A,B,C,D,E,R> m){ 173 return new MappedComponent5<A,B,C,D,E,R>(c1, c2, c3, c4, c5, m) 174 .label(); 175 } 176 177 185 public static <T> Component<T> mplus(Creator<T> c1, Creator<T> c2){ 186 return recover(c1, 188 onException(ComponentResolutionException.class, c2)); 189 } 190 195 public static Component fail(String msg){ 196 return new FailComponent(msg).label(); 197 } 198 202 public static Component mzero(){ 203 return _zero; 204 } 205 206 213 public static <T> Component<T> recover(Creator<T> c1, Recovery<T> r){ 214 return new RecoveredComponent<T>(c1, r).label(); 215 } 216 224 public static <T> Recovery<T> onException(Class <? extends Throwable > type, Creator<T> creator){ 225 return new OnException<T>(creator, type); 226 } 227 228 243 public static <T> Component<T> followedBy(Component<T> c1, ComponentBinder<T,?> binder){ 244 return new StagedComponent<T>(c1, binder).guard() 245 ; 246 } 247 259 public static <T> Component<T> followedBy(Component<T> c1, Binder<T,?> binder){ 260 return followedBy(c1, toComponentBinder(binder)); 261 } 262 274 public static <T> Component<T> followedBy(Component<T> c1, Creator<?> c2){ 275 return followedBy(c1, (ComponentBinder<T,?>)toVerificationBinder(c2)); 276 } 277 282 283 288 public static ComponentBinder pass(){ 289 return _pass; 290 } 291 296 public static ComponentBinder instantiator(){ 297 return _instantiator; 298 } 299 306 public static <From,To> ComponentBinder<From,To> toComponentBinder(final Binder<From,To> b){ 307 if(b instanceof ComponentBinder){ 308 return (ComponentBinder<From,To>)b; 309 } 310 return new DynamicBinder<From,To>(b); 311 } 312 313 320 public static <x,T> ComponentBinder<x, T> toVerificationBinder(final Creator<T> c){ 321 return new ConstComponentBinder(c); 322 } 323 324 331 public static <T> Component<T> ifelse(Creator<Boolean > cond, 332 final Component<T> a, final Component<T> b){ 333 return bind(cond, new Binder<Boolean ,T>(){ 334 public Creator<T> bind(Boolean c){ 335 if(c instanceof Boolean ){ 336 final boolean v = c.booleanValue(); 337 return v?a:b; 338 } 339 else{ 340 throw new IllegalArgumentException ("boolean expected for ifelse"); 341 } 342 } 343 }).label("ifelse"); 344 } 345 private static final Component _zero = fail("mzero"); 346 private static ComponentBinder getPass(final String name){ 347 return new ComponentBinder(){ 348 public Creator bind(Object obj){ 349 return Components.value(obj); 350 } 351 public Verifiable verify(Class t){ 352 return verifyAs(t); 353 } 354 public Class bindType(Class t){ 355 return t; 356 } 357 public String toString(){ 358 return name; 359 } 360 }; 361 } 362 private static final ComponentBinder _pass = getPass("pass"); 363 private static ComponentBinder getInstantiator(final String name, final Class target_type){ 364 return new ComponentBinder(){ 365 public Creator bind(Object obj){ 366 if(obj instanceof Creator){ 367 return (Creator)obj; 368 } 369 else{ 370 return Components.value(obj); 371 } 372 } 373 public Verifiable verify(Class t){ 374 checkCreator(t); 375 return verifyAs(target_type); 376 } 377 public Class bindType(Class t){ 378 return target_type; 379 } 380 private void checkCreator(Class t){ 381 if(!t.isAssignableFrom(Creator.class)&&!Creator.class.isAssignableFrom(t)){ 382 throw new TypeMismatchException(Creator.class, t); 384 } 385 } 386 public String toString(){ 387 return name; 388 } 389 }; 390 } 391 private static final ComponentBinder _instantiator = getInstantiator("instantiator", Object .class); 392 397 public static Verifiable verifyAs(final Class t){ 398 return new Verifiable(){ 399 public Class verify(Dependency dep){ 400 return t; 401 } 402 public String toString(){ 403 return "verifyAs " + jfun.util.Misc.getTypeName(t); 404 } 405 }; 406 } 407 } 408 | Popular Tags |