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

Quick Search    Search Deep

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


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