Bukkit  1.4.7-R1.0
 All Classes Namespaces Files Functions Variables Enumerator Pages
NumberConversions.java
Go to the documentation of this file.
1 package org.bukkit.util;
2 
6 public final class NumberConversions {
7  private NumberConversions() {}
8 
9  public static int floor(double num) {
10  final int floor = (int) num;
11  return floor == num ? floor : floor - (int) (Double.doubleToRawLongBits(num) >>> 63);
12  }
13 
14  public static int ceil(final double num) {
15  final int floor = (int) num;
16  return floor == num ? floor : floor + (int) (~Double.doubleToRawLongBits(num) >>> 63);
17  }
18 
19  public static int round(double num) {
20  return floor(num + 0.5d);
21  }
22 
23  public static int toInt(Object object) {
24  if (object instanceof Number) {
25  return ((Number) object).intValue();
26  }
27 
28  try {
29  return Integer.valueOf(object.toString());
30  } catch (NumberFormatException e) {
31  } catch (NullPointerException e) {
32  }
33  return 0;
34  }
35 
36  public static float toFloat(Object object) {
37  if (object instanceof Number) {
38  return ((Number) object).floatValue();
39  }
40 
41  try {
42  return Float.valueOf(object.toString());
43  } catch (NumberFormatException e) {
44  } catch (NullPointerException e) {
45  }
46  return 0;
47  }
48 
49  public static double toDouble(Object object) {
50  if (object instanceof Number) {
51  return ((Number) object).doubleValue();
52  }
53 
54  try {
55  return Double.valueOf(object.toString());
56  } catch (NumberFormatException e) {
57  } catch (NullPointerException e) {
58  }
59  return 0;
60  }
61 
62  public static long toLong(Object object) {
63  if (object instanceof Number) {
64  return ((Number) object).longValue();
65  }
66 
67  try {
68  return Long.valueOf(object.toString());
69  } catch (NumberFormatException e) {
70  } catch (NullPointerException e) {
71  }
72  return 0;
73  }
74 
75  public static short toShort(Object object) {
76  if (object instanceof Number) {
77  return ((Number) object).shortValue();
78  }
79 
80  try {
81  return Short.valueOf(object.toString());
82  } catch (NumberFormatException e) {
83  } catch (NullPointerException e) {
84  }
85  return 0;
86  }
87 
88  public static byte toByte(Object object) {
89  if (object instanceof Number) {
90  return ((Number) object).byteValue();
91  }
92 
93  try {
94  return Byte.valueOf(object.toString());
95  } catch (NumberFormatException e) {
96  } catch (NullPointerException e) {
97  }
98  return 0;
99  }
100 }