Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/gammastream/validity/GSVNumberMethods.java


1   package com.gammastream.validity;
2   
3   import com.webobjects.foundation.*;
4   import com.webobjects.appserver.*;
5   import com.webobjects.eocontrol.*;
6   import com.webobjects.eoaccess.*;
7   import java.math.BigDecimal;
8   
9   /**
10   *  This class provides a set of predefined rules for performing
11   *  validation on <code>Numbers</code>. These rules are part of
12   *  the default set of 'QuickRules'.
13   * 
14   *  @author GammaStream Technologies, Inc.
15   */
16  public class GSVNumberMethods extends Object {
17  
18      /**
19       *  For programatic purposes, we include this constant which is used for the <code>compareTo</code> method.
20       */
21      public final static String EQUAL = "==";
22      
23      /**
24       *  For programatic purposes, we include this constant which is used for the <code>compareTo</code> method.
25       */
26      public final static String NOT_EQUAL = "!=";
27      
28      /**
29       *  For programatic purposes, we include this constant which is used for the <code>compareTo</code> method.
30       */
31      public final static String GREATER_THAN = ">";
32      
33      /**
34       *  For programatic purposes, we include this constant which is used for the <code>compareTo</code> method.
35       */
36      public final static String GREATER_EQUAL = ">=";
37      
38      /**
39       *  For programatic purposes, we include this constant which is used for the <code>compareTo</code> method.
40       */
41      public final static String LESS_THAN = "<";
42      
43      /**
44       *  For programatic purposes, we include this constant which is used for the <code>compareTo</code> method.
45       */
46      public final static String LESS_EQUAL = "<=";
47  
48  
49      /**
50       *  Compares the specified attribute to a number provided in the params dictionary.
51       *  <br>
52       *  <br>The required key-value pairs include:
53       *  <br>"Operator" = The specified operator string. (i.e. "==", "!=", ">", ">=", "<", or "<=" )
54       *  <br>"RightOperand" = The number to compare the attribute to. (i.e. 0, 20, etc.)
55       *  <br>
56       *
57       *  @param  object    The object whose attribute is being validated.
58       *  @param  attribute   The attribute being validated.
59       *  @param  key     The key used to access the attribute.
60       *  @param  params     The param dictionary which must contain the above mentioned key-value pairs.
61       *
62       *  @return  <code>true</code> if the comparision succeeds; otherwise, <code>false</code>
63       */
64      public final static boolean compareTo(Object object, Object attribute, String key, NSDictionary params){
65          if(attribute instanceof Number){
66              String sign = (String)params.objectForKey("Operator");
67              String number = (String)params.objectForKey("RightOperand");
68              BigDecimal left = new BigDecimal(((Number)attribute).doubleValue());
69              BigDecimal right = new BigDecimal(number);
70              int comparisonValue=left.compareTo(right);
71  
72              if(sign.equals(GSVNumberMethods.EQUAL)){
73                  return(comparisonValue==0);
74              } else if(sign.equals(GSVNumberMethods.NOT_EQUAL)){
75                  return(comparisonValue!=0);
76              } else if(sign.equals(GSVNumberMethods.GREATER_THAN)){
77                  return(comparisonValue==1);
78              } else if(sign.equals(GSVNumberMethods.GREATER_EQUAL)){
79                  return(comparisonValue==1 || comparisonValue==0);
80              } else if(sign.equals(GSVNumberMethods.LESS_THAN)){
81                  return(comparisonValue==-1);
82              } else if(sign.equals(GSVNumberMethods.LESS_EQUAL)){
83                  return(comparisonValue==-1 || comparisonValue==0);
84              }
85          }
86          return false;
87      }
88    
89      /**
90       *  Checks to make sure the attribute falls within the range specified in the params dictionary.
91       *  <br>The attribute is allowed to equal the 'Low' or 'High' value.
92       *  <br>
93       *  <br>The required key-value pairs include:
94       *  <br>"Low" = The lowest possible value for this attribute.
95       *  <br>"High" = The highest possible value for this attribute.
96       *  <br>
97       *
98       *  @param  object    The object whose attribute is being validated.
99       *  @param  attribute   The attribute being validated.
100      *  @param  key     The key used to access the attribute.
101      *  @param  params     The param dictionary which must contain the above mentioned key-value pairs.
102      *
103      *  @return  <code>true</code> if the provided number false within the specified range; otherwise, <code>false</code>
104      */
105     public final static boolean isInRange(Object object, Object attribute, String key, NSDictionary params){
106         if(attribute instanceof Number){
107             try {
108                 BigDecimal low = new BigDecimal((String)params.objectForKey("Low"));
109                 BigDecimal high = new BigDecimal((String)params.objectForKey("High"));
110                 BigDecimal number = new BigDecimal(((Number)attribute).doubleValue());
111                 int comparisonValueLow=low.compareTo(number);
112                 int comparisonValueHigh=high.compareTo(number);
113                 if(comparisonValueLow==-1 || comparisonValueLow==0){
114                     if(comparisonValueHigh==1 || comparisonValueHigh==0){
115                         return true;
116                     }
117                 }
118             } catch(Exception e){
119                 NSLog.err.appendln(e.getMessage());
120             }
121         }
122         return false;
123     }
124 
125 
126     /**
127      *  Verifies that attribute is a positive number.
128      *
129      *  @param  object    The object whose attribute is being validated.
130      *  @param  attribute   The attribute being validated.
131      *  @param  key     The key used to access the attribute.
132      *  @param  params     The param dictionary which must contain the above mentioned key-value pairs.
133      *
134      *  @return  <code>true</code> if the provided number is positive value; otherwise, <code>false</code>
135      */
136     public final static boolean isPositiveNumber(Object object,Object attribute,String key, NSDictionary params){
137         if(attribute instanceof Number){
138             return(((Number)attribute).intValue()>=0);
139         }
140         return false;
141     }
142     
143     /**
144      *  Verifies that attribute is a negative number.
145      *
146      *  @param  object    The object whose attribute is being validated.
147      *  @param  attribute   The attribute being validated.
148      *  @param  key     The key used to access the attribute.
149      *  @param  params     The param dictionary which must contain the above mentioned key-value pairs.
150      *
151      *  @return  <code>true</code> if the provided number is a negative value; otherwise, <code>false</code>
152      */
153     public final static boolean isNegativeNumber(Object object,Object attribute,String key, NSDictionary params){
154         if(attribute instanceof Number){
155             return(((Number)attribute).intValue()<0);
156         }
157         return false;
158     }
159 
160 }