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

Quick Search    Search Deep

Source code: com/sun/xacml/cond/DivideFunction.java


1   
2   /*
3    * @(#)DivideFunction.java
4    *
5    * Copyright 2003-2004 Sun Microsystems, Inc. All Rights Reserved.
6    *
7    * Redistribution and use in source and binary forms, with or without
8    * modification, are permitted provided that the following conditions are met:
9    *
10   *   1. Redistribution of source code must retain the above copyright notice,
11   *      this list of conditions and the following disclaimer.
12   * 
13   *   2. Redistribution in binary form must reproduce the above copyright
14   *      notice, this list of conditions and the following disclaimer in the
15   *      documentation and/or other materials provided with the distribution.
16   *
17   * Neither the name of Sun Microsystems, Inc. or the names of contributors may
18   * be used to endorse or promote products derived from this software without
19   * specific prior written permission.
20   * 
21   * This software is provided "AS IS," without a warranty of any kind. ALL
22   * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
23   * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
24   * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN MICROSYSTEMS, INC. ("SUN")
25   * AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
26   * AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
27   * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
28   * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
29   * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
30   * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE,
31   * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
32   *
33   * You acknowledge that this software is not designed or intended for use in
34   * the design, construction, operation or maintenance of any nuclear facility.
35   */
36  
37  package com.sun.xacml.cond;
38  
39  import com.sun.xacml.EvaluationCtx;
40  
41  import com.sun.xacml.attr.AttributeValue;
42  import com.sun.xacml.attr.DoubleAttribute;
43  import com.sun.xacml.attr.IntegerAttribute;
44  
45  import java.util.HashSet;
46  import java.util.List;
47  import java.util.Set;
48  
49  
50  /**
51   * A class that implements all the *-divide functions. It takes two
52   * operands of the appropriate type and returns the quotient of the
53   * operands. If either of the operands is indeterminate, an indeterminate
54   * result is returned.
55   *
56   * @since 1.0
57   * @author Steve Hanna
58   * @author Seth Proctor
59   */
60  public class DivideFunction extends FunctionBase
61  {
62  
63      /**
64       * Standard identifier for the integer-divide function.
65       */
66      public static final String NAME_INTEGER_DIVIDE =
67          FUNCTION_NS + "integer-divide";
68  
69      /**
70       * Standard identifier for the double-divide function.
71       */
72      public static final String NAME_DOUBLE_DIVIDE =
73          FUNCTION_NS + "double-divide";
74  
75      // inernal identifiers for each of the supported functions
76      private static final int ID_INTEGER_DIVIDE = 0;
77      private static final int ID_DOUBLE_DIVIDE = 1;
78  
79      /**
80       * Creates a new <code>DivideFunction</code> object.
81       *
82       * @param functionName the standard XACML name of the function to be
83       *                     handled by this object, including the full namespace
84       *
85       * @throws IllegalArgumentException if the function is unknown
86       */
87      public DivideFunction(String functionName) {
88          super(functionName, getId(functionName), getArgumentType(functionName),
89                false, 2, getArgumentType(functionName), false);
90      }
91  
92      /**
93       * Private helper that returns the internal identifier used for the
94       * given standard function.
95       */
96      private static int getId(String functionName) {
97          if (functionName.equals(NAME_INTEGER_DIVIDE))
98              return ID_INTEGER_DIVIDE;
99          else if (functionName.equals(NAME_DOUBLE_DIVIDE))
100             return ID_DOUBLE_DIVIDE;
101         else
102             throw new IllegalArgumentException("unknown divide function " +
103                                                functionName);
104     }
105 
106     /**
107      * Private helper that returns the type used for the given standard
108      * function. Note that this doesn't check on the return value since the
109      * method always is called after getId, so we assume that the function
110      * is present.
111      */
112     private static String getArgumentType(String functionName) {
113         if (functionName.equals(NAME_INTEGER_DIVIDE))
114             return IntegerAttribute.identifier;
115         else
116             return DoubleAttribute.identifier;
117     }
118 
119     /**
120      * Returns a <code>Set</code> containing all the function identifiers
121      * supported by this class.
122      *
123      * @return a <code>Set</code> of <code>String</code>s
124      */
125     public static Set getSupportedIdentifiers() {
126         Set set = new HashSet();
127 
128         set.add(NAME_INTEGER_DIVIDE);
129         set.add(NAME_DOUBLE_DIVIDE);
130 
131         return set;
132     }
133 
134     /**
135      * Evaluate the function, using the specified parameters.
136      *
137      * @param inputs a <code>List</code> of <code>Evaluatable</code>
138      *               objects representing the arguments passed to the function
139      * @param context an <code>EvaluationCtx</code> so that the
140      *                <code>Evaluatable</code> objects can be evaluated
141      * @return an <code>EvaluationResult</code> representing the
142      *         function's result
143      */
144     public EvaluationResult evaluate(List inputs, EvaluationCtx context) {
145 
146         // Evaluate the arguments
147         AttributeValue [] argValues = new AttributeValue[inputs.size()];
148         EvaluationResult result = evalArgs(inputs, context, argValues);
149         if (result != null)
150             return result;
151 
152         // Now that we have real values, perform the divide operation
153         // in the manner appropriate for the type of the arguments.
154         switch (getFunctionId()) {
155         case ID_INTEGER_DIVIDE: {
156             long dividend = ((IntegerAttribute) argValues[0]).getValue();
157             long divisor = ((IntegerAttribute) argValues[1]).getValue();
158 
159             if (divisor == 0) {
160                 result = makeProcessingError("divide by zero");
161                 break;
162             }
163 
164             long quotient = dividend / divisor;
165 
166             result = new EvaluationResult(new IntegerAttribute(quotient));
167             break;
168         }
169         case ID_DOUBLE_DIVIDE: {
170             double dividend = ((DoubleAttribute) argValues[0]).getValue();
171             double divisor = ((DoubleAttribute) argValues[1]).getValue();
172 
173             if (divisor == 0) {
174                 result = makeProcessingError("divide by zero");
175                 break;
176             }
177 
178             double quotient = dividend / divisor;
179 
180             result = new EvaluationResult(new DoubleAttribute(quotient));
181             break;
182         }
183         }
184 
185         return result;
186     }
187 }