⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 comparisonfunction.java

📁 sunxacml源码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:

        if (i == null)
            throw new IllegalArgumentException("unknown comparison function " +
                                               functionName);

        return i.intValue();
    }

    /**
     * Private helper that returns the type used for the given standard
     * function. Note that this doesn't check on the return value since the
     * method always is called after getId, so we assume that the function
     * is present.
     */
    private static String getArgumentType(String functionName) {
        return (String)(typeMap.get(functionName));
    }

    /**
     * Returns a <code>Set</code> containing all the function identifiers
     * supported by this class.
     *
     * @return a <code>Set</code> of <code>String</code>s
     */
    public static Set getSupportedIdentifiers() {
        return Collections.unmodifiableSet(idMap.keySet());
    }

    /**
     * Evaluate the function, using the specified parameters.
     *
     * @param inputs a <code>List</code> of <code>Evaluatable</code>
     *               objects representing the arguments passed to the function
     * @param context an <code>EvaluationCtx</code> so that the
     *                <code>Evaluatable</code> objects can be evaluated
     * @return an <code>EvaluationResult</code> representing the
     *         function's result
     */
    public EvaluationResult evaluate(List inputs, EvaluationCtx context) {
        // Evaluate the arguments
        AttributeValue [] argValues = new AttributeValue [inputs.size()];
        EvaluationResult result = evalArgs(inputs, context, argValues);
        if (result != null)
            return result;

        // Now that we have real values, perform the comparison operation

        boolean boolResult = false;

        switch (getFunctionId()) {

        case ID_INTEGER_GREATER_THAN: {
            long arg0 = ((IntegerAttribute)(argValues[0])).getValue();
            long arg1 = ((IntegerAttribute)(argValues[1])).getValue();

            boolResult = (arg0 > arg1);

            break;
        }

        case ID_INTEGER_GREATER_THAN_OR_EQUAL: {
            long arg0 = ((IntegerAttribute)(argValues[0])).getValue();
            long arg1 = ((IntegerAttribute)(argValues[1])).getValue();

            boolResult = (arg0 >= arg1);

            break;
        }

        case ID_INTEGER_LESS_THAN: {
            long arg0 = ((IntegerAttribute)(argValues[0])).getValue();
            long arg1 = ((IntegerAttribute)(argValues[1])).getValue();

            boolResult = (arg0 < arg1);

            break;
        }

        case ID_INTEGER_LESS_THAN_OR_EQUAL: {
            long arg0 = ((IntegerAttribute)(argValues[0])).getValue();
            long arg1 = ((IntegerAttribute)(argValues[1])).getValue();

            boolResult = (arg0 <= arg1);

            break;
        }

        case ID_DOUBLE_GREATER_THAN: {
            double arg0 = ((DoubleAttribute)(argValues[0])).getValue();
            double arg1 = ((DoubleAttribute)(argValues[1])).getValue();

            boolResult = (doubleCompare(arg0, arg1) > 0);

            break;
        }

        case ID_DOUBLE_GREATER_THAN_OR_EQUAL: {
            double arg0 = ((DoubleAttribute)(argValues[0])).getValue();
            double arg1 = ((DoubleAttribute)(argValues[1])).getValue();

            boolResult = (doubleCompare(arg0, arg1) >= 0);

            break;
        }

        case ID_DOUBLE_LESS_THAN: {
            double arg0 = ((DoubleAttribute)(argValues[0])).getValue();
            double arg1 = ((DoubleAttribute)(argValues[1])).getValue();

            boolResult = (doubleCompare(arg0, arg1) < 0);

            break;
        }

        case ID_DOUBLE_LESS_THAN_OR_EQUAL: {
            double arg0 = ((DoubleAttribute)(argValues[0])).getValue();
            double arg1 = ((DoubleAttribute)(argValues[1])).getValue();

            boolResult = (doubleCompare(arg0, arg1) <= 0);

            break;
        }

        case ID_STRING_GREATER_THAN: {
            String arg0 = ((StringAttribute)(argValues[0])).getValue();
            String arg1 = ((StringAttribute)(argValues[1])).getValue();

            boolResult = (arg0.compareTo(arg1) > 0);

            break;
        }

        case ID_STRING_GREATER_THAN_OR_EQUAL: {
            String arg0 = ((StringAttribute)(argValues[0])).getValue();
            String arg1 = ((StringAttribute)(argValues[1])).getValue();

            boolResult = (arg0.compareTo(arg1) >= 0);

            break;
        }

        case ID_STRING_LESS_THAN: {
            String arg0 = ((StringAttribute)(argValues[0])).getValue();
            String arg1 = ((StringAttribute)(argValues[1])).getValue();

            boolResult = (arg0.compareTo(arg1) < 0);

            break;
        }

        case ID_STRING_LESS_THAN_OR_EQUAL: {
            String arg0 = ((StringAttribute)(argValues[0])).getValue();
            String arg1 = ((StringAttribute)(argValues[1])).getValue();

            boolResult = (arg0.compareTo(arg1) <= 0);

            break;
        }

        case ID_TIME_GREATER_THAN: {
            TimeAttribute arg0 = (TimeAttribute)(argValues[0]);
            TimeAttribute arg1 = (TimeAttribute)(argValues[1]);

            boolResult =
                (dateCompare(arg0.getValue(), arg0.getNanoseconds(),
                             arg1.getValue(), arg1.getNanoseconds()) > 0);

            break;
        }

        case ID_TIME_GREATER_THAN_OR_EQUAL: {
            TimeAttribute arg0 = (TimeAttribute)(argValues[0]);
            TimeAttribute arg1 = (TimeAttribute)(argValues[1]);

            boolResult =
                (dateCompare(arg0.getValue(), arg0.getNanoseconds(),
                             arg1.getValue(), arg1.getNanoseconds()) >= 0);

            break;
        }

        case ID_TIME_LESS_THAN: {
            TimeAttribute arg0 = (TimeAttribute)(argValues[0]);
            TimeAttribute arg1 = (TimeAttribute)(argValues[1]);

            boolResult =
                (dateCompare(arg0.getValue(), arg0.getNanoseconds(),
                             arg1.getValue(), arg1.getNanoseconds()) < 0);

            break;
        }

        case ID_TIME_LESS_THAN_OR_EQUAL: {
            TimeAttribute arg0 = (TimeAttribute)(argValues[0]);
            TimeAttribute arg1 = (TimeAttribute)(argValues[1]);

            boolResult =
                (dateCompare(arg0.getValue(), arg0.getNanoseconds(),
                             arg1.getValue(), arg1.getNanoseconds()) <= 0);

            break;
        }

        case ID_DATETIME_GREATER_THAN: {
            DateTimeAttribute arg0 = (DateTimeAttribute)(argValues[0]);
            DateTimeAttribute arg1 = (DateTimeAttribute)(argValues[1]);

            boolResult =
                (dateCompare(arg0.getValue(), arg0.getNanoseconds(),
                             arg1.getValue(), arg1.getNanoseconds()) > 0);

            break;
        }

        case ID_DATETIME_GREATER_THAN_OR_EQUAL: {
            DateTimeAttribute arg0 = (DateTimeAttribute)(argValues[0]);
            DateTimeAttribute arg1 = (DateTimeAttribute)(argValues[1]);

            boolResult =
                (dateCompare(arg0.getValue(), arg0.getNanoseconds(),
                             arg1.getValue(), arg1.getNanoseconds()) >= 0);

            break;
        }

        case ID_DATETIME_LESS_THAN: {
            DateTimeAttribute arg0 = (DateTimeAttribute)(argValues[0]);
            DateTimeAttribute arg1 = (DateTimeAttribute)(argValues[1]);

            boolResult =
                (dateCompare(arg0.getValue(), arg0.getNanoseconds(),
                             arg1.getValue(), arg1.getNanoseconds()) < 0);

            break;
        }

        case ID_DATETIME_LESS_THAN_OR_EQUAL: {
            DateTimeAttribute arg0 = (DateTimeAttribute)(argValues[0]);
            DateTimeAttribute arg1 = (DateTimeAttribute)(argValues[1]);

            boolResult =
                (dateCompare(arg0.getValue(), arg0.getNanoseconds(),
                             arg1.getValue(), arg1.getNanoseconds()) <= 0);

            break;
        }

        case ID_DATE_GREATER_THAN: {
            Date arg0 = ((DateAttribute)(argValues[0])).getValue();
            Date arg1 = ((DateAttribute)(argValues[1])).getValue();

            boolResult = (arg0.compareTo(arg1) > 0);

            break;
        }

        case ID_DATE_GREATER_THAN_OR_EQUAL: {
            Date arg0 = ((DateAttribute)(argValues[0])).getValue();
            Date arg1 = ((DateAttribute)(argValues[1])).getValue();

            boolResult = (arg0.compareTo(arg1) >= 0);

            break;
        }

        case ID_DATE_LESS_THAN: {
            Date arg0 = ((DateAttribute)(argValues[0])).getValue();
            Date arg1 = ((DateAttribute)(argValues[1])).getValue();

            boolResult = (arg0.compareTo(arg1) < 0);

            break;
        }

        case ID_DATE_LESS_THAN_OR_EQUAL: {
            Date arg0 = ((DateAttribute)(argValues[0])).getValue();
            Date arg1 = ((DateAttribute)(argValues[1])).getValue();

            boolResult = (arg0.compareTo(arg1) <= 0);

            break;
        }

        }

        // Return the result as a BooleanAttribute.
        return EvaluationResult.getInstance(boolResult);
    }

    /**
     * Helper function that does a comparison of the two doubles using the
     * rules of XMLSchema. Like all compare methods, this returns 0 if they're
     * equal, a positive value if d1 > d2, and a negative value if d1 < d2.
     */
    private int doubleCompare(double d1, double d2) {
        // see if the numbers equal each other
        if (d1 == d2) {
            // these are not NaNs, and therefore we just need to check that
            // that they're not zeros, which may have different signs
            if (d1 != 0)
                return 0;

            // they're both zeros, so we compare strings to figure out
            // the significance of any signs
            return Double.toString(d1).compareTo(Double.toString(d2));
        }

        // see if d1 is NaN
        if (Double.isNaN(d1)) {
            // d1 is NaN, so see if d2 is as well
            if (Double.isNaN(d2)) {
                // they're both NaNs, so they're equal
                return 0;
            } else {
                // d1 is always bigger than d2 since it's a NaN
                return 1;
            }
        }

        // see if d2 is NaN
        if (Double.isNaN(d2)) {
            // d2 is a NaN, though d1 isn't, so d2 is always bigger
            return -1;
        }

        // if we got here then neither is a NaN, and the numbers aren't
        // equal...given those facts, basic comparison works the same in
        // java as it's defined in XMLSchema, so now we can do the simple
        // comparison and return whatever we find
        return ((d1 > d2) ? 1 : -1);
    }

    /**
     * Helper function to compare two Date objects and their associated
     * nanosecond values. Like all compare methods, this returns 0 if they're
     * equal, a positive value if d1 > d2, and a negative value if d1 < d2.
     */
    private int dateCompare(Date d1, int n1, Date d2, int n2) {
        int compareResult = d1.compareTo(d2);

        // we only worry about the nanosecond values if the Dates are equal
        if (compareResult != 0)
            return compareResult;

        // see if there's any difference
        if (n1 == n2)
            return 0;
        
        // there is some difference in the nanoseconds, and that's how
        // we'll determine the comparison
        return ((n1 > n2) ? 1 : -1);
    }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -