forked from functionaljava/functionaljava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDigit.java
More file actions
211 lines (192 loc) · 3.65 KB
/
Digit.java
File metadata and controls
211 lines (192 loc) · 3.65 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package fj;
import fj.data.Option;
import static fj.data.Option.some;
import static fj.data.Option.none;
/**
* The digits zero to nine.
*
* @version %build.number%
*/
public enum Digit {
/**
* Zero.
*/
_0,
/**
* One.
*/
_1,
/**
* Two.
*/
_2,
/**
* Three.
*/
_3,
/**
* Four.
*/
_4,
/**
* Five.
*/
_5,
/**
* Six.
*/
_6,
/**
* Seven.
*/
_7,
/**
* Eight.
*/
_8,
/**
* Nine.
*/
_9;
/**
* Converts this digit to a long.
*
* @return A long for this digit.
*/
public long toLong() {
switch (this) {
case _0:
return 0L;
case _1:
return 1L;
case _2:
return 2L;
case _3:
return 3L;
case _4:
return 4L;
case _5:
return 5L;
case _6:
return 6L;
case _7:
return 7L;
case _8:
return 8L;
default:
return 9L;
}
}
/**
* Converts this digit to a character.
*
* @return A character for this digit.
*/
public char toChar() {
switch (this) {
case _0:
return '0';
case _1:
return '1';
case _2:
return '2';
case _3:
return '3';
case _4:
return '4';
case _5:
return '5';
case _6:
return '6';
case _7:
return '7';
case _8:
return '8';
default:
return '9';
}
}
/**
* Converts the right-most digit in the given long value to a digit.
*
* @param i The long to convert.
* @return The right-most digit in the given long value as a digit.
*/
public static Digit fromLong(final long i) {
final long x = Math.abs(i) % 10L;
return x == 0L ? _0 :
x == 1L ? _1 :
x == 2L ? _2 :
x == 3L ? _3 :
x == 4L ? _4 :
x == 5L ? _5 :
x == 6L ? _6 :
x == 7L ? _7 :
x == 8L ? _8 :
_9;
}
/**
* Converts the given character in the given long value to a digit.
*
* @param c The character to convert.
* @return The character in the given long value as a digit.
*/
public static Option<Digit> fromChar(final char c) {
switch (c) {
case '0':
return some(_0);
case '1':
return some(_1);
case '2':
return some(_2);
case '3':
return some(_3);
case '4':
return some(_4);
case '5':
return some(_5);
case '6':
return some(_6);
case '7':
return some(_7);
case '8':
return some(_8);
case '9':
return some(_9);
default:
return none();
}
}
/**
* First-class conversion from digit to a long.
*/
public static final F<Digit, Long> toLong = new F<Digit, Long>() {
public Long f(final Digit d) {
return d.toLong();
}
};
/**
* First-class conversion from a long to a digit.
*/
public static final F<Long, Digit> fromLong = new F<Long, Digit>() {
public Digit f(final Long i) {
return fromLong(i);
}
};
/**
* First-class conversion from a digit to a character.
*/
public static final F<Digit, Character> toChar = new F<Digit, Character>() {
public Character f(final Digit d) {
return d.toChar();
}
};
/**
* First-class conversion from a character to a digit.
*/
public static final F<Character, Option<Digit>> fromChar = new F<Character, Option<Digit>>() {
public Option<Digit> f(final Character c) {
return fromChar(c);
}
};
}