1   /*
2    * SPDX-FileCopyrightText: none
3    * SPDX-License-Identifier: CC0-1.0
4    */
5   
6   package gov.nist.secauto.metaschema.core.metapath.cst;
7   
8   import gov.nist.secauto.metaschema.core.metapath.ISequence;
9   import gov.nist.secauto.metaschema.core.metapath.TypeMetapathException;
10  import gov.nist.secauto.metaschema.core.metapath.function.library.FnData;
11  import gov.nist.secauto.metaschema.core.metapath.item.atomic.IAnyAtomicItem;
12  
13  import edu.umd.cs.findbugs.annotations.NonNull;
14  import edu.umd.cs.findbugs.annotations.Nullable;
15  
16  public abstract class AbstractExpression implements IExpression {
17    /**
18     * Get the first data item of the provided {@code sequence} cast to an
19     * {@link IAnyAtomicItem}.
20     *
21     * @param sequence
22     *          the sequence to get the data item from
23     * @param requireSingleton
24     *          if {@code true} then a {@link TypeMetapathException} is thrown if
25     *          the sequence contains more than one item
26     * @return {@code null} if the sequence is empty, or the item otherwise
27     * @throws TypeMetapathException
28     *           if the sequence contains more than one item and requireSingleton is
29     *           {@code true}, or if the data item cannot be cast
30     */
31    @Nullable
32    public static IAnyAtomicItem getFirstDataItem(@NonNull ISequence<?> sequence,
33        boolean requireSingleton) {
34      return FnData.fnData(sequence).getFirstItem(requireSingleton);
35    }
36  
37    @Override
38    public String toString() {
39      return CSTPrinter.toString(this);
40    }
41  }