001/*
002 * Portions of this software was developed by employees of the National Institute
003 * of Standards and Technology (NIST), an agency of the Federal Government and is
004 * being made available as a public service. Pursuant to title 17 United States
005 * Code Section 105, works of NIST employees are not subject to copyright
006 * protection in the United States. This software may be subject to foreign
007 * copyright. Permission in the United States and in foreign countries, to the
008 * extent that NIST may hold copyright, to use, copy, modify, create derivative
009 * works, and distribute this software and its documentation without fee is hereby
010 * granted on a non-exclusive basis, provided that this notice and disclaimer
011 * of warranty appears in all copies.
012 *
013 * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER
014 * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY
015 * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF
016 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM
017 * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE
018 * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE.  IN NO EVENT
019 * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT,
020 * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM,
021 * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY,
022 * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR
023 * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT
024 * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER.
025 */
026
027package gov.nist.secauto.metaschema.core.metapath.function.library;
028
029import gov.nist.secauto.metaschema.core.metapath.DynamicContext;
030import gov.nist.secauto.metaschema.core.metapath.ISequence;
031import gov.nist.secauto.metaschema.core.metapath.function.FunctionUtils;
032import gov.nist.secauto.metaschema.core.metapath.function.IArgument;
033import gov.nist.secauto.metaschema.core.metapath.function.IFunction;
034import gov.nist.secauto.metaschema.core.metapath.function.IFunctionExecutor;
035import gov.nist.secauto.metaschema.core.metapath.item.IItem;
036import gov.nist.secauto.metaschema.core.metapath.item.atomic.INumericItem;
037import gov.nist.secauto.metaschema.core.util.ObjectUtils;
038
039import java.net.URI;
040import java.util.List;
041
042import javax.xml.namespace.QName;
043
044import edu.umd.cs.findbugs.annotations.NonNull;
045
046/**
047 * Provides a generic implementation of methods defined by <a href=
048 * "https://www.w3.org/TR/xpath-functions-31/#numeric-value-functions">XPath 3.1
049 * Functions on numeric values</a>.
050 */
051public final class NumericFunction implements IFunctionExecutor {
052
053  @NonNull
054  private final INumericExecutor executor;
055
056  @NonNull
057  static IFunction signature(@NonNull URI namespace, @NonNull String name, @NonNull INumericExecutor executor) {
058    return signature(ObjectUtils.notNull(namespace.toASCIIString()), name, executor);
059  }
060
061  @NonNull
062  static IFunction signature(@NonNull String namespace, @NonNull String name, @NonNull INumericExecutor executor) {
063    return IFunction.builder()
064        .name(name)
065        .namespace(namespace)
066        .argument(IArgument.newBuilder()
067            .name("arg1")
068            .type(INumericItem.class)
069            .zeroOrOne()
070            .build())
071        .returnType(INumericItem.class)
072        .returnZeroOrOne()
073        .functionHandler(newFunctionHandler(executor))
074        .build();
075  }
076
077  @NonNull
078  static IFunction signature(@NonNull QName qname, @NonNull INumericExecutor executor) {
079    return signature(
080        ObjectUtils.requireNonNull(qname.getNamespaceURI(), "the namespace URI must not be null"),
081        ObjectUtils.requireNonNull(qname.getLocalPart(), "the localpart must not be null"),
082        executor);
083  }
084
085  @NonNull
086  private static NumericFunction newFunctionHandler(@NonNull INumericExecutor executor) {
087    return new NumericFunction(executor);
088  }
089
090  private NumericFunction(@NonNull INumericExecutor executor) {
091    this.executor = executor;
092  }
093
094  @Override
095  public ISequence<INumericItem> execute(@NonNull IFunction function,
096      @NonNull List<ISequence<?>> arguments,
097      @NonNull DynamicContext dynamicContext,
098      IItem focus) {
099
100    ISequence<? extends INumericItem> sequence = FunctionUtils.asType(
101        ObjectUtils.requireNonNull(arguments.get(0)));
102    if (sequence.isEmpty()) {
103      return ISequence.empty(); // NOPMD - readability
104    }
105
106    INumericItem item = FunctionUtils.getFirstItem(sequence, true);
107    if (item == null) {
108      return ISequence.empty(); // NOPMD - readability
109    }
110
111    INumericItem result = executor.execute(item);
112    return ISequence.of(result);
113  }
114
115  @FunctionalInterface
116  public interface INumericExecutor {
117    /**
118     * Perform the execution using the provided {@code item}.
119     *
120     * @param item
121     *          the item to operate on
122     * @return the numeric result from the execution
123     */
124    @NonNull
125    INumericItem execute(@NonNull INumericItem item);
126  }
127}