001    package fj;
002    
003    import static fj.data.List.asString;
004    import fj.pre.Show;
005    
006    /**
007     * Represents the bottom _|_ value.
008     *
009     * @version %build.number%<br>
010     *          <ul>
011     *          <li>$LastChangedRevision: 284 $</li>
012     *          <li>$LastChangedDate: 2009-09-12 09:09:53 +1000 (Sat, 12 Sep 2009) $</li>
013     *          </ul>
014     */
015    public final class Bottom {
016      private Bottom() {
017        throw new UnsupportedOperationException();
018      }
019    
020      /**
021       * Returns an error to represent undefinedness in a computation.
022       *
023       * @return An error to represent undefinedness in a computation.
024       */
025      public static Error undefined() {
026        return error("undefined");
027      }
028    
029      /**
030       * Returns an error to represent undefinedness in a computation with early failure using the given
031       * message.
032       *
033       * @param s The message to fail with.
034       * @return An error to represent undefinedness in a computation with early failure using the given
035       *         message.
036       */
037      public static Error error(final String s) {
038        throw new Error(s);
039      }
040    
041      /**
042       * Provides a thunk that throws an error using the given message when evaluated.
043       *
044       * @param s The message to fail with.
045       * @return A thunk that throws an error using the given message when evaluated.
046       */
047      public static P1<Error> error_(final String s) {
048        return new P1<Error>() {
049          @Override public Error _1() {
050            throw new Error(s);
051          }
052        };
053      }
054    
055      /**
056       * Provides a function that throws an error using the given message, ignoring its argument.
057       *
058       * @param s The message to fail with.
059       * @return A function that throws an error using the given message, ignoring its argument.
060       */
061      public static <A, B> F<A, B> errorF(final String s) {
062        return new F<A, B>() {
063          public B f(final A a) {
064            throw new Error(s);
065          }
066        };
067      }
068    
069      /**
070       * Represents a deconstruction failure that was non-exhaustive.
071       *
072       * @param a  The value being deconstructed.
073       * @param sa The rendering for the value being deconstructed.
074       * @return A deconstruction failure that was non-exhaustive.
075       */
076      public static <A> Error decons(final A a, final Show<A> sa) {
077        return error("Deconstruction failure on type " + a.getClass() + " with value " + sa.show(a).toString());
078      }
079    
080      /**
081       * Represents a deconstruction failure that was non-exhaustive.
082       *
083       * @param c The type being deconstructed.
084       * @return A deconstruction failure that was non-exhaustive.
085       */
086      public static <A> Error decons(final java.lang.Class<A> c) {
087        return error("Deconstruction failure on type " + c);
088      }
089    
090      /**
091       * A function that returns the <code>toString</code> for a throwable.
092       *
093       * @return A function that returns the <code>toString</code> for a throwable.
094       */
095      public static <T extends Throwable> F<T, String> eToString() {
096        return new F<T, String>() {
097          public String f(final Throwable t) {
098            return t.toString();
099          }
100        };
101      }
102    
103      /**
104       * A function that returns the <code>getMessage</code> for a throwable.
105       *
106       * @return A function that returns the <code>getMessage</code> for a throwable.
107       */
108      public static <T extends Throwable> F<T, String> eMessage() {
109        return new F<T, String>() {
110          public String f(final Throwable t) {
111            return t.getMessage();
112          }
113        };
114      }
115    }