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.IDayTimeDurationItem;
33  import gov.nist.secauto.metaschema.core.metapath.item.atomic.INumericItem;
34  import gov.nist.secauto.metaschema.core.metapath.item.atomic.IYearMonthDurationItem;
35  
36  import edu.umd.cs.findbugs.annotations.NonNull;
37  
38  class Division
39      extends AbstractBasicArithmeticExpression {
40  
41    /**
42     * An expression that gets the quotient result by dividing the dividend by the
43     * divisor.
44     *
45     * @param dividend
46     *          the expression whose result is to be divided
47     * @param divisor
48     *          the expression whose result is to divide by
49     */
50    protected Division(@NonNull IExpression dividend, @NonNull IExpression divisor) {
51      super(dividend, divisor);
52    }
53  
54    @Override
55    public <RESULT, CONTEXT> RESULT accept(IExpressionVisitor<RESULT, CONTEXT> visitor, CONTEXT context) {
56      return visitor.visitDivision(this, context);
57    }
58  
59    /**
60     * Get the quotient result by dividing the dividend by the divisor.
61     *
62     * @param dividend
63     *          the item to be divided
64     * @param divisor
65     *          the item to divide by
66     * @return the quotient result or an empty {@link ISequence} if either item is
67     *         {@code null}
68     */
69    @Override
70    @NonNull
71    protected IAnyAtomicItem operation(@NonNull IAnyAtomicItem dividend, @NonNull IAnyAtomicItem divisor) {
72      return divide(dividend, divisor);
73    }
74  
75    /**
76     * Get the quotient result by dividing the dividend by the divisor.
77     *
78     * @param dividend
79     *          the item to be divided
80     * @param divisor
81     *          the item to divide by
82     * @return the quotient result
83     */
84    @NonNull
85    public static IAnyAtomicItem divide(@NonNull IAnyAtomicItem dividend, // NOPMD - intentional
86        @NonNull IAnyAtomicItem divisor) {
87      IAnyAtomicItem retval = null;
88      if (dividend instanceof IYearMonthDurationItem) {
89        IYearMonthDurationItem left = (IYearMonthDurationItem) dividend;
90        if (divisor instanceof INumericItem) {
91          retval = OperationFunctions.opDivideYearMonthDuration(left, (INumericItem) divisor);
92        } else if (divisor instanceof IYearMonthDurationItem) {
93          // TODO: find a way to support this
94          throw new UnsupportedOperationException();
95        }
96      } else if (dividend instanceof IDayTimeDurationItem) {
97        IDayTimeDurationItem left = (IDayTimeDurationItem) dividend;
98        if (divisor instanceof INumericItem) {
99          retval = OperationFunctions.opDivideDayTimeDuration(left, (INumericItem) divisor);
100       } else if (divisor instanceof IDayTimeDurationItem) {
101         retval = OperationFunctions.opDivideDayTimeDurationByDayTimeDuration(left, (IDayTimeDurationItem) divisor);
102       }
103     } else {
104       // handle as numeric
105       INumericItem left = FunctionUtils.toNumeric(dividend);
106       if (divisor instanceof INumericItem) {
107         INumericItem right = FunctionUtils.toNumeric(divisor);
108         retval = OperationFunctions.opNumericDivide(left, right);
109       } else if (divisor instanceof IYearMonthDurationItem) {
110         retval = OperationFunctions.opMultiplyYearMonthDuration((IYearMonthDurationItem) divisor, left);
111       } else if (divisor instanceof IDayTimeDurationItem) {
112         retval = OperationFunctions.opMultiplyDayTimeDuration((IDayTimeDurationItem) divisor, left);
113       }
114     }
115     if (retval == null) {
116       throw new InvalidTypeMetapathException(
117           null,
118           String.format("The expression '%s / %s' is not supported", dividend.getClass().getName(),
119               divisor.getClass().getName()));
120     }
121     return retval;
122   }
123 }