FnRound.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.MetapathConstants;
  30. import gov.nist.secauto.metaschema.core.metapath.function.FunctionUtils;
  31. import gov.nist.secauto.metaschema.core.metapath.function.IArgument;
  32. import gov.nist.secauto.metaschema.core.metapath.function.IFunction;
  33. import gov.nist.secauto.metaschema.core.metapath.item.IItem;
  34. import gov.nist.secauto.metaschema.core.metapath.item.atomic.IIntegerItem;
  35. import gov.nist.secauto.metaschema.core.metapath.item.atomic.INumericItem;
  36. import gov.nist.secauto.metaschema.core.util.ObjectUtils;

  37. import java.util.List;

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

  39. public final class FnRound {
  40.   private static final String NAME = "round";

  41.   @NonNull
  42.   static final IFunction SIGNATURE = NumericFunction.signature(
  43.       MetapathConstants.NS_XPATH_FUNCTIONS,
  44.       NAME,
  45.       INumericItem::round);

  46.   @NonNull
  47.   static final IFunction SIGNATURE_WITH_PRECISION = IFunction.builder()
  48.       .name(NAME)
  49.       .namespace(MetapathConstants.NS_XPATH_FUNCTIONS)
  50.       .argument(IArgument.newBuilder()
  51.           .name("arg1")
  52.           .type(INumericItem.class)
  53.           .zeroOrOne()
  54.           .build())
  55.       .argument(IArgument.newBuilder()
  56.           .name("precision")
  57.           .type(IIntegerItem.class)
  58.           .one()
  59.           .build())
  60.       .returnType(INumericItem.class)
  61.       .returnZeroOrOne()
  62.       .functionHandler(FnRound::executeTwoArg)
  63.       .build();

  64.   private FnRound() {
  65.     // disable construction
  66.   }

  67.   @SuppressWarnings("unused")
  68.   @NonNull
  69.   private static ISequence<INumericItem> executeTwoArg(
  70.       @NonNull IFunction function,
  71.       @NonNull List<ISequence<?>> arguments,
  72.       @NonNull DynamicContext dynamicContext,
  73.       IItem focus) {
  74.     ISequence<? extends INumericItem> sequence = FunctionUtils.asType(
  75.         ObjectUtils.requireNonNull(arguments.get(0)));
  76.     if (sequence.isEmpty()) {
  77.       return ISequence.empty(); // NOPMD - readability
  78.     }

  79.     INumericItem item = FunctionUtils.getFirstItem(sequence, true);
  80.     if (item == null) {
  81.       return ISequence.empty(); // NOPMD - readability
  82.     }

  83.     @NonNull IIntegerItem precision = FunctionUtils.asType(
  84.         FunctionUtils.requireFirstItem(
  85.             ObjectUtils.requireNonNull(arguments.get(1)),
  86.             true));

  87.     INumericItem result = item.round(precision);
  88.     return ISequence.of(result);
  89.   }
  90. }