View Javadoc
1   /*
2    * Portions of this software was developed by employees of the National Institute
3    * of Standards and Technology (NIST), an agency of the Federal Government and is
4    * being made available as a public service. Pursuant to title 17 United States
5    * Code Section 105, works of NIST employees are not subject to copyright
6    * protection in the United States. This software may be subject to foreign
7    * copyright. Permission in the United States and in foreign countries, to the
8    * extent that NIST may hold copyright, to use, copy, modify, create derivative
9    * works, and distribute this software and its documentation without fee is hereby
10   * granted on a non-exclusive basis, provided that this notice and disclaimer
11   * of warranty appears in all copies.
12   *
13   * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER
14   * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY
15   * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF
16   * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM
17   * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE
18   * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE.  IN NO EVENT
19   * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT,
20   * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM,
21   * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY,
22   * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR
23   * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT
24   * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER.
25   */
26  
27  package gov.nist.secauto.metaschema.core.metapath;
28  
29  import gov.nist.secauto.metaschema.core.metapath.item.atomic.IAnyAtomicItem;
30  import gov.nist.secauto.metaschema.core.metapath.item.atomic.IBooleanItem;
31  
32  import edu.umd.cs.findbugs.annotations.NonNull;
33  import edu.umd.cs.findbugs.annotations.Nullable;
34  
35  class ValueComparison
36      extends AbstractComparison {
37  
38    /**
39     * Create a new value comparison expression.
40     *
41     * @param left
42     *          the expression to compare against
43     * @param operator
44     *          the comparison operator
45     * @param right
46     *          the expression to compare with
47     */
48    protected ValueComparison(@NonNull IExpression left, @NonNull Operator operator, @NonNull IExpression right) {
49      super(left, operator, right);
50    }
51  
52    @Override
53    public <RESULT, CONTEXT> RESULT accept(IExpressionVisitor<RESULT, CONTEXT> visitor, CONTEXT context) {
54      return visitor.visitValueComparison(this, context);
55    }
56  
57    @Override
58    public ISequence<? extends IBooleanItem> accept(DynamicContext dynamicContext, ISequence<?> focus) {
59      IAnyAtomicItem left = getFirstDataItem(getLeft().accept(dynamicContext, focus), false);
60      IAnyAtomicItem right = getFirstDataItem(getRight().accept(dynamicContext, focus), false);
61  
62      return resultOrEmpty(left, right);
63    }
64  
65    /**
66     * Compare the two atomic items.
67     *
68     * @param leftItem
69     *          the first item to compare
70     * @param rightItem
71     *          the second item to compare
72     * @return a or an empty {@link ISequence} if either item is {@code null}
73     */
74    @NonNull
75    protected ISequence<? extends IBooleanItem> resultOrEmpty(@Nullable IAnyAtomicItem leftItem,
76        @Nullable IAnyAtomicItem rightItem) {
77      ISequence<? extends IBooleanItem> retval;
78      if (leftItem == null || rightItem == null) {
79        retval = ISequence.empty();
80      } else {
81        IBooleanItem result = valueCompairison(leftItem, getOperator(), rightItem);
82        retval = ISequence.of(result);
83      }
84      return retval;
85    }
86  
87    /**
88     * Compare the two items using the provided {@code operator}.
89     *
90     * @param leftItem
91     *          the first item to compare
92     * @param operator
93     *          the comparison operator
94     * @param rightItem
95     *          the second item to compare
96     * @return the result of the comparison
97     */
98    @NonNull
99    protected IBooleanItem valueCompairison(@NonNull IAnyAtomicItem leftItem, @NonNull Operator operator,
100       @NonNull IAnyAtomicItem rightItem) {
101     return compare(leftItem, operator, rightItem);
102   }
103 }