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.function.FunctionUtils;
30  import gov.nist.secauto.metaschema.core.metapath.function.OperationFunctions;
31  import gov.nist.secauto.metaschema.core.metapath.item.atomic.IAnyAtomicItem;
32  import gov.nist.secauto.metaschema.core.metapath.item.atomic.IDateItem;
33  import gov.nist.secauto.metaschema.core.metapath.item.atomic.IDateTimeItem;
34  import gov.nist.secauto.metaschema.core.metapath.item.atomic.IDayTimeDurationItem;
35  import gov.nist.secauto.metaschema.core.metapath.item.atomic.INumericItem;
36  import gov.nist.secauto.metaschema.core.metapath.item.atomic.IYearMonthDurationItem;
37  
38  import edu.umd.cs.findbugs.annotations.NonNull;
39  
40  class Addition
41      extends AbstractBasicArithmeticExpression {
42  
43    /**
44     * An expression that sums two atomic data items.
45     *
46     * @param left
47     *          an expression whose result is summed
48     * @param right
49     *          an expression whose result is summed
50     */
51    protected Addition(@NonNull IExpression left, @NonNull IExpression right) {
52      super(left, right);
53    }
54  
55    @Override
56    public <RESULT, CONTEXT> RESULT accept(IExpressionVisitor<RESULT, CONTEXT> visitor, CONTEXT context) {
57      return visitor.visitAddition(this, context);
58    }
59  
60    /**
61     * Get the sum of two atomic items.
62     *
63     * @param left
64     *          the first item to sum
65     * @param right
66     *          the second item to sum
67     * @return the sum of both items or an empty {@link ISequence} if either item is
68     *         {@code null}
69     */
70    @Override
71    protected IAnyAtomicItem operation(@NonNull IAnyAtomicItem left, @NonNull IAnyAtomicItem right) {
72      return sum(left, right);
73    }
74  
75    /**
76     * Get the sum of two atomic items.
77     *
78     * @param leftItem
79     *          the first item to sum
80     * @param rightItem
81     *          the second item to sum
82     * @return the sum of both items
83     */
84    @SuppressWarnings({ "PMD.CyclomaticComplexity", "PMD.CognitiveComplexity" })
85    @NonNull
86    public static IAnyAtomicItem sum(
87        @NonNull IAnyAtomicItem leftItem, // NOPMD - intentional
88        @NonNull IAnyAtomicItem rightItem) {
89      IAnyAtomicItem retval = null;
90      if (leftItem instanceof IDateItem) {
91        IDateItem left = (IDateItem) leftItem;
92        if (rightItem instanceof IYearMonthDurationItem) {
93          retval = OperationFunctions.opAddYearMonthDurationToDate(left, (IYearMonthDurationItem) rightItem);
94        } else if (rightItem instanceof IDayTimeDurationItem) {
95          retval = OperationFunctions.opAddDayTimeDurationToDate(left, (IDayTimeDurationItem) rightItem);
96        }
97      } else if (leftItem instanceof IDateTimeItem) {
98        IDateTimeItem left = (IDateTimeItem) leftItem;
99        if (rightItem instanceof IYearMonthDurationItem) {
100         retval = OperationFunctions.opAddYearMonthDurationToDateTime(left, (IYearMonthDurationItem) rightItem);
101       } else if (rightItem instanceof IDayTimeDurationItem) {
102         retval = OperationFunctions.opAddDayTimeDurationToDateTime(left, (IDayTimeDurationItem) rightItem);
103       }
104     } else if (leftItem instanceof IYearMonthDurationItem) {
105       IYearMonthDurationItem left = (IYearMonthDurationItem) leftItem;
106       if (rightItem instanceof IDateItem) {
107         retval = OperationFunctions.opAddYearMonthDurationToDate((IDateItem) rightItem, left);
108       } else if (rightItem instanceof IDateTimeItem) {
109         retval = OperationFunctions.opAddYearMonthDurationToDateTime((IDateTimeItem) rightItem, left);
110       } else if (rightItem instanceof IYearMonthDurationItem) {
111         retval = OperationFunctions.opSubtractYearMonthDurations(left, (IYearMonthDurationItem) rightItem);
112       }
113     } else if (leftItem instanceof IDayTimeDurationItem) {
114       IDayTimeDurationItem left = (IDayTimeDurationItem) leftItem;
115       if (rightItem instanceof IDateItem) {
116         retval = OperationFunctions.opAddDayTimeDurationToDate((IDateItem) rightItem, left);
117       } else if (rightItem instanceof IDateTimeItem) {
118         retval = OperationFunctions.opAddDayTimeDurationToDateTime((IDateTimeItem) rightItem, left);
119       } else if (rightItem instanceof IDayTimeDurationItem) {
120         retval = OperationFunctions.opAddDayTimeDurations(left, (IDayTimeDurationItem) rightItem);
121       }
122     } else {
123       // handle as numeric
124       INumericItem left = FunctionUtils.toNumeric(leftItem);
125       INumericItem right = FunctionUtils.toNumeric(rightItem);
126       retval = OperationFunctions.opNumericAdd(left, right);
127     }
128     if (retval == null) {
129       throw new UnsupportedOperationException(
130           String.format("The expression '%s + %s' is not supported", leftItem.getClass().getName(),
131               rightItem.getClass().getName()));
132     }
133     return retval;
134   }
135 }