Przeglądaj źródła

Numeric - metody nie pobierające wartości domyślnej (rzucają wyjątek)

Ranides Atterwim 13 lat temu
rodzic
commit
c24397cb1d
1 zmienionych plików z 71 dodań i 59 usunięć
  1. 71 59
      src/main/java/net/ranides/assira/text/Numeric.java

+ 71 - 59
src/main/java/net/ranides/assira/text/Numeric.java

@@ -38,97 +38,109 @@ public final class Numeric {
 
     public static long asLong(String value, long ddefault) {
         try {
-            Matcher hit = PATTERN_ENG.matcher(value);
-            if(hit.matches()) {
-                return post(hit, ddefault);
-            } else {
-                return Long.parseLong( value.trim() );
-            }
-        } catch(Exception _) {
+            return asLong(value);
+        } catch(NumberFormatException _) {
             return ddefault;
         }
     }
 
     public static int asInt(String value, int ddefault) {
         try {
-            Matcher hit = PATTERN_ENG.matcher(value);
-            if(hit.matches()) {
-                return (int)post(hit, ddefault);
-            } else {
-                return Integer.parseInt( value.trim() );
-            }
-        } catch(Exception _) {
+            return asInt(value);
+        } catch(NumberFormatException _) {
             return ddefault;
         }
     }
 
     public static short asShort(String value, short ddefault) {
         try {
-            Matcher hit = PATTERN_ENG.matcher(value);
-            if(hit.matches()) {
-                return (short)post(hit, ddefault);
-            } else {
-                return Short.parseShort( value.trim() );
-            }
-        } catch(Exception _) {
+            return asShort(value);
+        } catch(NumberFormatException _) {
             return ddefault;
         }
     }
 
     public static float asFloat(String value, float ddefault) {
         try {
-            Matcher hit = PATTERN_ENG.matcher(value);
-            if(hit.matches()) {
-                return (float)post(hit, ddefault);
-            } else {
-                return Float.parseFloat( value.trim() );
-            }
-        } catch(Exception _) {
+            return asFloat(value);
+        } catch(NumberFormatException _) {
             return ddefault;
         }
     }
 
     public static double asDouble(String value, double ddefault) {
         try {
-            Matcher hit = PATTERN_ENG.matcher(value);
-            if(hit.matches()) {
-                return post(hit, ddefault);
-            } else {
-                return Double.parseDouble( value.trim() );
-            }
-        } catch(Exception _) {
+            return asDouble(value);
+        } catch(NumberFormatException _) {
             return ddefault;
         }
     }
 
-    public static double post(Matcher hit, double ddefault) {
-        try {
-            double value = Double.parseDouble(StringUtils.remove(hit.group(1),' '));
-            if(null !=hit.group(2)) {
-                value *= PREFIX.get(hit.group(2));
-            }
-            if("b".equals(hit.group(3))) {
-                value /= 8;
-            }
-            return value;
-        } catch(NumberFormatException _) {
-            return ddefault;
+    public static long asLong(String value) {
+        Matcher hit = PATTERN_ENG.matcher(value);
+        if(hit.matches()) {
+            return asLong(hit);
+        } else {
+            return Long.parseLong( value.trim() );
         }
     }
 
-    public static long post(Matcher hit, long ddefault) {
-        try {
-            long value = Long.parseLong(StringUtils.remove(hit.group(1),' '));
-            if(null !=hit.group(2)) {
-                value *= PREFIX.get(hit.group(2));
-            }
-            if("b".equals(hit.group(3))) {
-                value /= 8;
-            }
-            return value;
-        } catch(NumberFormatException _) {
-            return ddefault;
+    public static int asInt(String value) {
+        Matcher hit = PATTERN_ENG.matcher(value);
+        if(hit.matches()) {
+            return (int)asLong(hit);
+        } else {
+            return Integer.parseInt( value.trim() );
+        }
+    }
+
+    public static short asShort(String value) {
+        Matcher hit = PATTERN_ENG.matcher(value);
+        if(hit.matches()) {
+            return (short)asLong(hit);
+        } else {
+            return Short.parseShort( value.trim() );
+        }
+    }
+
+    public static float asFloat(String value) {
+        Matcher hit = PATTERN_ENG.matcher(value);
+        if(hit.matches()) {
+            return (float)asDouble(hit);
+        } else {
+            return Float.parseFloat( value.trim() );
+        }
+    }
+
+    public static double asDouble(String value) {
+        Matcher hit = PATTERN_ENG.matcher(value);
+        if(hit.matches()) {
+            return asDouble(hit);
+        } else {
+            return Double.parseDouble( value.trim() );
+        }
+    }
+
+    private static double asDouble(Matcher hit) {
+        double value = Double.parseDouble(StringUtils.remove(hit.group(1),' '));
+        if(null !=hit.group(2)) {
+            value *= PREFIX.get(hit.group(2));
+        }
+        if("b".equals(hit.group(3))) {
+            value /= 8;
+        }
+        return value;
+    }
+
+    private static long asLong(Matcher hit) {
+        long value = Long.parseLong(StringUtils.remove(hit.group(1),' '));
+        if(null !=hit.group(2)) {
+            value *= PREFIX.get(hit.group(2));
+        }
+        if("b".equals(hit.group(3))) {
+            value /= 8;
         }
+        return value;
     }
 
 }