NumericFunction.java

  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. package gov.nist.secauto.metaschema.core.metapath.function.library;

  27. import gov.nist.secauto.metaschema.core.metapath.DynamicContext;
  28. import gov.nist.secauto.metaschema.core.metapath.ISequence;
  29. import gov.nist.secauto.metaschema.core.metapath.function.FunctionUtils;
  30. import gov.nist.secauto.metaschema.core.metapath.function.IArgument;
  31. import gov.nist.secauto.metaschema.core.metapath.function.IFunction;
  32. import gov.nist.secauto.metaschema.core.metapath.function.IFunctionExecutor;
  33. import gov.nist.secauto.metaschema.core.metapath.item.IItem;
  34. import gov.nist.secauto.metaschema.core.metapath.item.atomic.INumericItem;
  35. import gov.nist.secauto.metaschema.core.util.ObjectUtils;

  36. import java.net.URI;
  37. import java.util.List;

  38. import javax.xml.namespace.QName;

  39. import edu.umd.cs.findbugs.annotations.NonNull;

  40. /**
  41.  * Provides a generic implementation of methods defined by <a href=
  42.  * "https://www.w3.org/TR/xpath-functions-31/#numeric-value-functions">XPath 3.1
  43.  * Functions on numeric values</a>.
  44.  */
  45. public final class NumericFunction implements IFunctionExecutor {

  46.   @NonNull
  47.   private final INumericExecutor executor;

  48.   @NonNull
  49.   static IFunction signature(@NonNull URI namespace, @NonNull String name, @NonNull INumericExecutor executor) {
  50.     return signature(ObjectUtils.notNull(namespace.toASCIIString()), name, executor);
  51.   }

  52.   @NonNull
  53.   static IFunction signature(@NonNull String namespace, @NonNull String name, @NonNull INumericExecutor executor) {
  54.     return IFunction.builder()
  55.         .name(name)
  56.         .namespace(namespace)
  57.         .argument(IArgument.newBuilder()
  58.             .name("arg1")
  59.             .type(INumericItem.class)
  60.             .zeroOrOne()
  61.             .build())
  62.         .returnType(INumericItem.class)
  63.         .returnZeroOrOne()
  64.         .functionHandler(newFunctionHandler(executor))
  65.         .build();
  66.   }

  67.   @NonNull
  68.   static IFunction signature(@NonNull QName qname, @NonNull INumericExecutor executor) {
  69.     return signature(
  70.         ObjectUtils.requireNonNull(qname.getNamespaceURI(), "the namespace URI must not be null"),
  71.         ObjectUtils.requireNonNull(qname.getLocalPart(), "the localpart must not be null"),
  72.         executor);
  73.   }

  74.   @NonNull
  75.   private static NumericFunction newFunctionHandler(@NonNull INumericExecutor executor) {
  76.     return new NumericFunction(executor);
  77.   }

  78.   private NumericFunction(@NonNull INumericExecutor executor) {
  79.     this.executor = executor;
  80.   }

  81.   @Override
  82.   public ISequence<INumericItem> execute(@NonNull IFunction function,
  83.       @NonNull List<ISequence<?>> arguments,
  84.       @NonNull DynamicContext dynamicContext,
  85.       IItem focus) {

  86.     ISequence<? extends INumericItem> sequence = FunctionUtils.asType(
  87.         ObjectUtils.requireNonNull(arguments.get(0)));
  88.     if (sequence.isEmpty()) {
  89.       return ISequence.empty(); // NOPMD - readability
  90.     }

  91.     INumericItem item = FunctionUtils.getFirstItem(sequence, true);
  92.     if (item == null) {
  93.       return ISequence.empty(); // NOPMD - readability
  94.     }

  95.     INumericItem result = executor.execute(item);
  96.     return ISequence.of(result);
  97.   }

  98.   @FunctionalInterface
  99.   public interface INumericExecutor {
  100.     /**
  101.      * Perform the execution using the provided {@code item}.
  102.      *
  103.      * @param item
  104.      *          the item to operate on
  105.      * @return the numeric result from the execution
  106.      */
  107.     @NonNull
  108.     INumericItem execute(@NonNull INumericItem item);
  109.   }
  110. }