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

Quick Search    Search Deep

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


1   
2   /*
3    * @(#)FloorFunction.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 the floor function. It takes one double
52   * operand, chooses the largest integer less than or equal to that
53   * value, and returns that integer (as a double). If the operand
54   * is indeterminate, an indeterminate result is returned.
55   *
56   * @since 1.0
57   * @author Steve Hanna
58   * @author Seth Proctor
59   */
60  public class FloorFunction extends FunctionBase
61  {
62  
63      /**
64       * Standard identifier for the floor function.
65       */
66      public static final String NAME_FLOOR = FUNCTION_NS + "floor";
67  
68      /**
69       * Creates a new <code>FloorFunction</code> object.
70       *
71       * @param functionName the standard XACML name of the function to be
72       *                     handled by this object, including the full namespace
73       *
74       * @throws IllegalArgumentException if the function is unknown
75       */
76      public FloorFunction(String functionName) {
77          super(NAME_FLOOR, 0, DoubleAttribute.identifier, false, 1,
78                DoubleAttribute.identifier, false);
79  
80          if (! functionName.equals(NAME_FLOOR))
81              throw new IllegalArgumentException("unknown floor function: "
82                                                 + functionName);
83      }
84  
85      /**
86       * Returns a <code>Set</code> containing all the function identifiers
87       * supported by this class.
88       *
89       * @return a <code>Set</code> of <code>String</code>s
90       */
91      public static Set getSupportedIdentifiers() {
92          Set set = new HashSet();
93  
94          set.add(NAME_FLOOR);
95  
96          return set;
97      }
98  
99      /**
100      * Evaluate the function, using the specified parameters.
101      *
102      * @param inputs a <code>List</code> of <code>Evaluatable</code>
103      *               objects representing the arguments passed to the function
104      * @param context an <code>EvaluationCtx</code> so that the
105      *                <code>Evaluatable</code> objects can be evaluated
106      * @return an <code>EvaluationResult</code> representing the
107      *         function's result
108      */
109     public EvaluationResult evaluate(List inputs, EvaluationCtx context) {
110 
111         // Evaluate the arguments
112         AttributeValue [] argValues = new AttributeValue[inputs.size()];
113         EvaluationResult result = evalArgs(inputs, context, argValues);
114         if (result != null)
115             return result;
116 
117         // Now that we have real values, perform the floor operation
118         double arg = ((DoubleAttribute) argValues[0]).getValue();
119 
120         return new EvaluationResult(new DoubleAttribute(Math.floor(arg)));
121     }
122 }