forked from functionaljava/functionaljava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBottom.java
More file actions
109 lines (100 loc) · 3.02 KB
/
Bottom.java
File metadata and controls
109 lines (100 loc) · 3.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package fj;
/**
* Represents the bottom _|_ value.
*
* @version %build.number%
*/
public final class Bottom {
private Bottom() {
throw new UnsupportedOperationException();
}
/**
* Returns an error to represent undefinedness in a computation.
*
* @return An error to represent undefinedness in a computation.
*/
public static Error undefined() {
return error("undefined");
}
/**
* Returns an error to represent undefinedness in a computation with early failure using the given
* message.
*
* @param s The message to fail with.
* @return An error to represent undefinedness in a computation with early failure using the given
* message.
*/
public static Error error(final String s) {
throw new Error(s);
}
/**
* Provides a thunk that throws an error using the given message when evaluated.
*
* @param s The message to fail with.
* @return A thunk that throws an error using the given message when evaluated.
*/
public static <A> P1<A> error_(final String s) {
return new P1<A>() {
@Override public A _1() {
throw new Error(s);
}
};
}
/**
* Provides a function that throws an error using the given message, ignoring its argument.
*
* @param s The message to fail with.
* @return A function that throws an error using the given message, ignoring its argument.
*/
public static <A, B> F<A, B> errorF(final String s) {
return new F<A, B>() {
public B f(final A a) {
throw new Error(s);
}
};
}
/**
* Represents a deconstruction failure that was non-exhaustive.
*
* @param a The value being deconstructed.
* @param sa The rendering for the value being deconstructed.
* @return A deconstruction failure that was non-exhaustive.
*/
public static <A> Error decons(final A a, final Show<A> sa) {
return error("Deconstruction failure on type " + a.getClass() + " with value " + sa.show(a).toString());
}
/**
* Represents a deconstruction failure that was non-exhaustive.
*
* @param c The type being deconstructed.
* @return A deconstruction failure that was non-exhaustive.
*/
@SuppressWarnings({"UnnecessaryFullyQualifiedName"})
public static <A> Error decons(final java.lang.Class<A> c) {
return error("Deconstruction failure on type " + c);
}
/**
* A function that returns the <code>toString</code> for a throwable.
*
* @return A function that returns the <code>toString</code> for a throwable.
*/
public static <T extends Throwable> F<T, String> eToString() {
return new F<T, String>() {
public String f(final Throwable t) {
return t.toString();
}
};
}
/**
* A function that returns the <code>getMessage</code> for a throwable.
*
* @return A function that returns the <code>getMessage</code> for a throwable.
*/
public static <T extends Throwable> F<T, String> eMessage() {
return new F<T, String>() {
public String f(final Throwable t) {
return t.getMessage();
}
};
}
}