]> git.mxchange.org Git - jcore-utils.git/commitdiff
fixed validator for long/integer numbers, thanks Mr. Plischke
authorRoland Haeder <roland@mxchange.org>
Thu, 15 Oct 2015 11:45:51 +0000 (13:45 +0200)
committerRoland Haeder <roland@mxchange.org>
Thu, 15 Oct 2015 11:45:51 +0000 (13:45 +0200)
Signed-off-by:Roland Häder <roland@mxchange.org>

src/org/mxchange/jcoreee/validator/number/BaseLongValidator.java

index e45de732153720df507a7e997dc7cc7eaefb75f6..f99ad47c70a12d3f9ae230ac8ab775c75dbc23f4 100644 (file)
@@ -47,7 +47,7 @@ public abstract class BaseLongValidator extends BaseObjectValidator implements V
                // Get client id and init message + key
                String clientId = component.getClientId();
                FacesMessage facesMessage = null;
-               String requiredMessage;
+               String requiredMessage = null;
 
                // So far all fine, no check if the field is fine
                for (final String field : requiredFields) {
@@ -56,33 +56,27 @@ public abstract class BaseLongValidator extends BaseObjectValidator implements V
 
                        // Is it the same?
                        if (clientId.endsWith(field)) {
-                               // Compare value's type
-                               if ((!allowNull) && (!(value instanceof Long))) {
-                                       // Generate message
-                                       requiredMessage = MessageFormat.format("Field {0} is not Long.: {1}", field, value); //NOI18N
-
-                                       // Value is not right type
-                                       facesMessage = new FacesMessage(FacesMessage.SEVERITY_ERROR, requiredMessage, requiredMessage); //NOI18N
-                                       break;
-                               } else if ((!allowNull) && (!(value instanceof Integer))) {
-                                       // Generate message
-                                       requiredMessage = MessageFormat.format("Field {0} is not Integer.: {1}", field, value); //NOI18N
-
-                                       // Value is not right type
-                                       facesMessage = new FacesMessage(FacesMessage.SEVERITY_ERROR, requiredMessage, requiredMessage); //NOI18N
-                                       break;
-                               }
-
-                               // Declare variables
+                               // Init variables
                                Integer i = null;
                                Long l = null;
 
-                               // Which type?
-                               if (value instanceof Integer) {
-                                       // Cast to Long
+                               // Compare value's type
+                               if (value instanceof String) {
+                                       // Is a string, try long
+                                       try {
+                                               l = Long.valueOf((String) value);
+                                       } catch (final NumberFormatException ex) {
+                                               // Cannot parse string to long
+                                               requiredMessage = MessageFormat.format("Field {0} cannot be parsed to long: {1}, exception:{2}", field, value, ex);
+
+                                               // Abort processing here
+                                               break;
+                                       }
+                               } else if (value instanceof Integer) {
+                                       // Is integer
                                        i = (Integer) value;
-                               } else {
-                                       // Cast to Long
+                               } else if (value instanceof Long) {
+                                       // Is long
                                        l = (Long) value;
                                }
 
@@ -92,19 +86,23 @@ public abstract class BaseLongValidator extends BaseObjectValidator implements V
                                        requiredMessage = MessageFormat.format("Value {0} for field {1} is below zero.", i, field); //NOI18N
 
                                        // Abort processing here
-                                       facesMessage = new FacesMessage(FacesMessage.SEVERITY_ERROR, requiredMessage, requiredMessage);
                                        break;
                                } else if ((!allowNull) && ((l instanceof Long) && (l < 0))) {
                                        // Generate message
                                        requiredMessage = MessageFormat.format("Value {0} for field {1} is below zero.", l, field); //NOI18N
 
                                        // Abort processing here
-                                       facesMessage = new FacesMessage(FacesMessage.SEVERITY_ERROR, requiredMessage, requiredMessage);
                                        break;
                                }
                        }
                }
 
+               // Is requiredMessage set?
+               if (null != requiredMessage) {
+                       // Embed into FacesMessage
+                       facesMessage = new FacesMessage(FacesMessage.SEVERITY_ERROR, requiredMessage, requiredMessage);
+               }
+
                // Is facesMessage set?
                if (null != facesMessage) {
                        // Abort here