001    package fj;
002    
003    /**
004     * Functions that convert between Java primitive types.
005     *
006     * @version %build.number%<br>
007     *          <ul>
008     *          <li>$LastChangedRevision: 122 $</li>
009     *          <li>$LastChangedDate: 2009-04-25 08:24:38 +1000 (Sat, 25 Apr 2009) $</li>
010     *          </ul>
011     */
012    public final class Primitive {
013      private Primitive() {
014        throw new UnsupportedOperationException();
015      }
016    
017      // BEGIN Boolean ->
018    
019      /**
020       * A function that converts booleans to bytes.
021       */
022      public static final F<Boolean, Byte> Boolean_Byte = new F<Boolean, Byte>() {
023        public Byte f(final Boolean b) {
024          return (byte) (b ? 1 : 0);
025        }
026      };
027    
028      /**
029       * A function that converts booleans to characters.
030       */
031      public static final F<Boolean, Character> Boolean_Character = new F<Boolean, Character>() {
032        public Character f(final Boolean b) {
033          return (char) (b ? 1 : 0);
034        }
035      };
036    
037      /**
038       * A function that converts booleans to doubles.
039       */
040      public static final F<Boolean, Double> Boolean_Double = new F<Boolean, Double>() {
041        public Double f(final Boolean b) {
042          return b ? 1D : 0D;
043        }
044      };
045    
046      /**
047       * A function that converts booleans to floats.
048       */
049      public static final F<Boolean, Float> Boolean_Float = new F<Boolean, Float>() {
050        public Float f(final Boolean b) {
051          return b ? 1F : 0F;
052        }
053      };
054    
055      /**
056       * A function that converts booleans to integers.
057       */
058      public static final F<Boolean, Integer> Boolean_Integer = new F<Boolean, Integer>() {
059        public Integer f(final Boolean b) {
060          return b ? 1 : 0;
061        }
062      };
063    
064      /**
065       * A function that converts booleans to longs.
066       */
067      public static final F<Boolean, Long> Boolean_Long = new F<Boolean, Long>() {
068        public Long f(final Boolean b) {
069          return b ? 1L : 0L;
070        }
071      };
072    
073      /**
074       * A function that converts booleans to shorts.
075       */
076      public static final F<Boolean, Short> Boolean_Short = new F<Boolean, Short>() {
077        public Short f(final Boolean b) {
078          return (short) (b ? 1 : 0);
079        }
080      };
081    
082      // END Boolean ->
083    
084      // BEGIN Byte ->
085    
086      /**
087       * A function that converts bytes to booleans.
088       */
089      public static final F<Byte, Boolean> Byte_Boolean = new F<Byte, Boolean>() {
090        public Boolean f(final Byte b) {
091          return b != 0;
092        }
093      };
094    
095      /**
096       * A function that converts bytes to characters.
097       */
098      public static final F<Byte, Character> Byte_Character = new F<Byte, Character>() {
099        public Character f(final Byte b) {
100          return (char) (byte) b;
101        }
102      };
103    
104      /**
105       * A function that converts bytes to doubles.
106       */
107      public static final F<Byte, Double> Byte_Double = new F<Byte, Double>() {
108        public Double f(final Byte b) {
109          //No it isn't
110          //noinspection RedundantCast
111          return (double) b;
112        }
113      };
114    
115      /**
116       * A function that converts bytes to floats.
117       */
118      public static final F<Byte, Float> Byte_Float = new F<Byte, Float>() {
119        public Float f(final Byte b) {
120          //No it isn't
121          //noinspection RedundantCast
122          return (float) b;
123        }
124      };
125    
126      /**
127       * A function that converts bytes to integers.
128       */
129      public static final F<Byte, Integer> Byte_Integer = new F<Byte, Integer>() {
130        public Integer f(final Byte b) {
131          //No it isn't
132          //noinspection RedundantCast
133          return (int) b;
134        }
135      };
136    
137      /**
138       * A function that converts bytes to longs.
139       */
140      public static final F<Byte, Long> Byte_Long = new F<Byte, Long>() {
141        public Long f(final Byte b) {
142          //No it isn't
143          //noinspection RedundantCast
144          return (long) b;
145        }
146      };
147    
148      /**
149       * A function that converts bytes to shorts.
150       */
151      public static final F<Byte, Short> Byte_Short = new F<Byte, Short>() {
152        public Short f(final Byte b) {
153          //No it isn't
154          //noinspection RedundantCast
155          return (short) b;
156        }
157      };
158    
159      // END Byte ->
160    
161      // BEGIN Character ->
162    
163      /**
164       * A function that converts characters to booleans.
165       */
166      public static final F<Character, Boolean> Character_Boolean = new F<Character, Boolean>() {
167        public Boolean f(final Character c) {
168          return c != 0;
169        }
170      };
171    
172      /**
173       * A function that converts characters to bytes.
174       */
175      public static final F<Character, Byte> Character_Byte = new F<Character, Byte>() {
176        public Byte f(final Character c) {
177          return (byte) (char) c;
178        }
179      };
180    
181      /**
182       * A function that converts characters to doubles.
183       */
184      public static final F<Character, Double> Character_Double = new F<Character, Double>() {
185        public Double f(final Character c) {
186          return (double) (char) c;
187        }
188      };
189    
190      /**
191       * A function that converts characters to floats.
192       */
193      public static final F<Character, Float> Character_Float = new F<Character, Float>() {
194        public Float f(final Character c) {
195          return (float) (char) c;
196        }
197      };
198    
199      /**
200       * A function that converts characters to integers.
201       */
202      public static final F<Character, Integer> Character_Integer = new F<Character, Integer>() {
203        public Integer f(final Character c) {
204          return (int) (char) c;
205        }
206      };
207    
208      /**
209       * A function that converts characters to longs.
210       */
211      public static final F<Character, Long> Character_Long = new F<Character, Long>() {
212        public Long f(final Character c) {
213          return (long) (char) c;
214        }
215      };
216    
217      /**
218       * A function that converts characters to shorts.
219       */
220      public static final F<Character, Short> Character_Short = new F<Character, Short>() {
221        public Short f(final Character c) {
222          return (short) (char) c;
223        }
224      };
225    
226      // END Character ->
227    
228      // BEGIN Double ->
229    
230      /**
231       * A function that converts doubles to booleans.
232       */
233      public static final F<Double, Boolean> Double_Boolean = new F<Double, Boolean>() {
234        public Boolean f(final Double d) {
235          return d != 0D;
236        }
237      };
238    
239      /**
240       * A function that converts doubles to bytes.
241       */
242      public static final F<Double, Byte> Double_Byte = new F<Double, Byte>() {
243        public Byte f(final Double d) {
244          return (byte) (double) d;
245        }
246      };
247    
248      /**
249       * A function that converts doubles to characters.
250       */
251      public static final F<Double, Character> Double_Character = new F<Double, Character>() {
252        public Character f(final Double d) {
253          return (char) (double) d;
254        }
255      };
256    
257      /**
258       * A function that converts doubles to floats.
259       */
260      public static final F<Double, Float> Double_Float = new F<Double, Float>() {
261        public Float f(final Double d) {
262          return (float) (double) d;
263        }
264      };
265    
266      /**
267       * A function that converts doubles to integers.
268       */
269      public static final F<Double, Integer> Double_Integer = new F<Double, Integer>() {
270        public Integer f(final Double d) {
271          return (int) (double) d;
272        }
273      };
274    
275      /**
276       * A function that converts doubles to longs.
277       */
278      public static final F<Double, Long> Double_Long = new F<Double, Long>() {
279        public Long f(final Double d) {
280          return (long) (double) d;
281        }
282      };
283    
284      /**
285       * A function that converts doubles to shorts.
286       */
287      public static final F<Double, Short> Double_Short = new F<Double, Short>() {
288        public Short f(final Double d) {
289          return (short) (double) d;
290        }
291      };
292    
293      // END Double ->
294    
295      // BEGIN Float ->
296    
297      /**
298       * A function that converts floats to booleans.
299       */
300      public static final F<Float, Boolean> Float_Boolean = new F<Float, Boolean>() {
301        public Boolean f(final Float f) {
302          return f != 0F;
303        }
304      };
305    
306      /**
307       * A function that converts floats to bytes.
308       */
309      public static final F<Float, Byte> Float_Byte = new F<Float, Byte>() {
310        public Byte f(final Float f) {
311          return (byte) (float) f;
312        }
313      };
314    
315      /**
316       * A function that converts floats to characters.
317       */
318      public static final F<Float, Character> Float_Character = new F<Float, Character>() {
319        public Character f(final Float f) {
320          return (char) (float) f;
321        }
322      };
323    
324      /**
325       * A function that converts floats to doubles.
326       */
327      public static final F<Float, Double> Float_Double = new F<Float, Double>() {
328        public Double f(final Float f) {
329          return (double) (float) f;
330        }
331      };
332    
333      /**
334       * A function that converts floats to integers.
335       */
336      public static final F<Float, Integer> Float_Integer = new F<Float, Integer>() {
337        public Integer f(final Float f) {
338          return (int) (float) f;
339        }
340      };
341    
342      /**
343       * A function that converts floats to longs.
344       */
345      public static final F<Float, Long> Float_Long = new F<Float, Long>() {
346        public Long f(final Float f) {
347          return (long) (float) f;
348        }
349      };
350    
351      /**
352       * A function that converts floats to shorts.
353       */
354      public static final F<Float, Short> Float_Short = new F<Float, Short>() {
355        public Short f(final Float f) {
356          return (short) (float) f;
357        }
358      };
359    
360      // END Float ->
361    
362      // BEGIN Integer ->
363    
364      /**
365       * A function that converts integers to booleans.
366       */
367      public static final F<Integer, Boolean> Integer_Boolean = new F<Integer, Boolean>() {
368        public Boolean f(final Integer i) {
369          return i != 0;
370        }
371      };
372    
373      /**
374       * A function that converts integers to bytes.
375       */
376      public static final F<Integer, Byte> Integer_Byte = new F<Integer, Byte>() {
377        public Byte f(final Integer i) {
378          return (byte) (int) i;
379        }
380      };
381    
382      /**
383       * A function that converts integers to characters.
384       */
385      public static final F<Integer, Character> Integer_Character = new F<Integer, Character>() {
386        public Character f(final Integer i) {
387          return (char) (int) i;
388        }
389      };
390    
391      /**
392       * A function that converts integers to doubles.
393       */
394      public static final F<Integer, Double> Integer_Double = new F<Integer, Double>() {
395        public Double f(final Integer i) {
396          //No it isn't
397          //noinspection RedundantCast
398          return (double) i;
399        }
400      };
401    
402      /**
403       * A function that converts integers to floats.
404       */
405      public static final F<Integer, Float> Integer_Float = new F<Integer, Float>() {
406        public Float f(final Integer i) {
407          //No it isn't
408          //noinspection RedundantCast
409          return (float) i;
410        }
411      };
412    
413      /**
414       * A function that converts integers to longs.
415       */
416      public static final F<Integer, Long> Integer_Long = new F<Integer, Long>() {
417        public Long f(final Integer i) {
418          //No it isn't
419          //noinspection RedundantCast
420          return (long) i;
421        }
422      };
423    
424      /**
425       * A function that converts integers to shorts.
426       */
427      public static final F<Integer, Short> Integer_Short = new F<Integer, Short>() {
428        public Short f(final Integer i) {
429          //No it isn't
430          //noinspection RedundantCast
431          return (short) (int) i;
432        }
433      };
434    
435      // END Integer ->
436    
437      // BEGIN Long ->
438    
439      /**
440       * A function that converts longs to booleans.
441       */
442      public static final F<Long, Boolean> Long_Boolean = new F<Long, Boolean>() {
443        public Boolean f(final Long l) {
444          return l != 0L;
445        }
446      };
447    
448      /**
449       * A function that converts longs to bytes.
450       */
451      public static final F<Long, Byte> Long_Byte = new F<Long, Byte>() {
452        public Byte f(final Long l) {
453          return (byte) (long) l;
454        }
455      };
456    
457      /**
458       * A function that converts longs to characters.
459       */
460      public static final F<Long, Character> Long_Character = new F<Long, Character>() {
461        public Character f(final Long l) {
462          return (char) (long) l;
463        }
464      };
465    
466      /**
467       * A function that converts longs to doubles.
468       */
469      public static final F<Long, Double> Long_Double = new F<Long, Double>() {
470        public Double f(final Long l) {
471          return (double) (long) l;
472        }
473      };
474    
475      /**
476       * A function that converts longs to floats.
477       */
478      public static final F<Long, Float> Long_Float = new F<Long, Float>() {
479        public Float f(final Long l) {
480          return (float) (long) l;
481        }
482      };
483    
484      /**
485       * A function that converts longs to integers.
486       */
487      public static final F<Long, Integer> Long_Integer = new F<Long, Integer>() {
488        public Integer f(final Long l) {
489          return (int) (long) l;
490        }
491      };
492    
493      /**
494       * A function that converts longs to shorts.
495       */
496      public static final F<Long, Short> Long_Short = new F<Long, Short>() {
497        public Short f(final Long l) {
498          return (short) (long) l;
499        }
500      };
501    
502      // END Long ->
503    
504      // BEGIN Short ->
505    
506      /**
507       * A function that converts shorts to booleans.
508       */
509      public static final F<Short, Boolean> Short_Boolean = new F<Short, Boolean>() {
510        public Boolean f(final Short s) {
511          return s != 0;
512        }
513      };
514    
515      /**
516       * A function that converts shorts to bytes.
517       */
518      public static final F<Short, Byte> Short_Byte = new F<Short, Byte>() {
519        public Byte f(final Short s) {
520          return (byte) (short) s;
521        }
522      };
523    
524      /**
525       * A function that converts shorts to characters.
526       */
527      public static final F<Short, Character> Short_Character = new F<Short, Character>() {
528        public Character f(final Short s) {
529          return (char) (short) s;
530        }
531      };
532    
533      /**
534       * A function that converts shorts to doubles.
535       */
536      public static final F<Short, Double> Short_Double = new F<Short, Double>() {
537        public Double f(final Short s) {
538          return (double) (short) s;
539        }
540      };
541    
542      /**
543       * A function that converts shorts to floats.
544       */
545      public static final F<Short, Float> Short_Float = new F<Short, Float>() {
546        public Float f(final Short s) {
547          return (float) (short) s;
548        }
549      };
550    
551      /**
552       * A function that converts shorts to integers.
553       */
554      public static final F<Short, Integer> Short_Integer = new F<Short, Integer>() {
555        public Integer f(final Short s) {
556          return (int) (short) s;
557        }
558      };
559    
560      /**
561       * A function that converts shorts to longs.
562       */
563      public static final F<Short, Long> Short_Long = new F<Short, Long>() {
564        public Long f(final Short s) {
565          return (long) (short) s;
566        }
567      };
568    
569      // END Short
570    }