All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.sf.staccatocommons.control.monad.AbstractMonad Maven / Gradle / Ivy

The newest version!
/**
 *  Copyright (c) 2010-2012, The StaccatoCommons Team
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation; version 3 of the License.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 */

package net.sf.staccatocommons.control.monad;

import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;

import net.sf.staccatocommons.control.monad.internal.AppendMonad;
import net.sf.staccatocommons.defs.Applicable;
import net.sf.staccatocommons.defs.Evaluable;
import net.sf.staccatocommons.defs.Executable;
import net.sf.staccatocommons.defs.tuple.Tuple2;
import net.sf.staccatocommons.lang.AbstractProtoMonad;

/**
 * @author flbulgarelli
 * @since 1.2
 */
public abstract class AbstractMonad extends AbstractProtoMonad, Monad, A> implements
  Monad {

  public final Monad filter(final Evaluable predicate) {
    return bind(Monads.filter(predicate));
  }

  /* fmap f xs == xs >>= return . f */
  public final  Monad map(final Applicable function) {
    return bind(Monads.map(function));
  }

  public final  Monad flatMap(
    final Applicable> function) {
    return bind(Monads.flatMap(function));
  }


  public  Monad bind(Applicable> function) {
    return new BoundMonad(monadicValue(), function);
  }

  public Monad incorporate(final Applicable> function) {
    return bind(Monads.incorporate(function));
  }


  public Monad fork(ExecutorService executor) {
    return bind(Monads. async(executor));
  }

  public final Monad each(final Executable block) {
    return bind(Monads.each(block));
  }

  public Monad discard() {
    return (Monad) this;
  }

  public  Monad> clone(Applicable function) {
    return bind(Monads.clone(function));
  }

  public  Monad> branch(Applicable function0,
    Applicable function1) {
    return bind(Monads.branch(function0, function1));
  }

  public final void forEach(Executable block) {
    each(block).run();
  }

  public Monad append(Monad other) {
    return new AppendMonad(this, other);
  }

  public void run(Executor executor) {
    executor.execute(this);
  }

  public final Void value() {
    run();
    return null;
  }

}