| 1 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 2 | // Copyright (c) 2018 Saxonica Limited. |
| 3 | // This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. |
| 4 | // If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 5 | // This Source Code Form is "Incompatible With Secondary Licenses", as defined by the Mozilla Public License, v. 2.0. |
| 6 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 7 | |
| 8 | package net.sf.saxon.om; |
| 9 | |
| 10 | import net.sf.saxon.trans.XPathException; |
| 11 | import net.sf.saxon.value.SequenceExtent; |
| 12 | |
| 13 | import java.util.ArrayList; |
| 14 | import java.util.List; |
| 15 | |
| 16 | /** |
| 17 | * A value that is a sequence containing one or more items. The main use is in declarations of reflexive extension |
| 18 | * functions, where declaring an argument of type <OneOrMore<IntegerValue>> triggers automatic type |
| 19 | * checking in the same way as for a native XSLT/XQuery function declaring the type as xs:integer+. |
| 20 | */ |
| 21 | |
| 22 | public class OneOrMore<T extends Item<?>> extends SequenceExtent<T> { |
| 23 | |
| 24 | /** |
| 25 | * Create a sequence containing zero or one items |
| 26 | * |
| 27 | * @param content The content of the sequence |
| 28 | */ |
| 29 | |
| 30 | public OneOrMore(T[] content) { |
| 31 | super(content); |
| 32 | if (content.length == 0) { |
| 33 | throw new IllegalArgumentException(); |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | public OneOrMore(List<T> content) { |
| 38 | super(content); |
| 39 | if (content.isEmpty()) { |
| 40 | throw new IllegalArgumentException(); |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | public static <T extends Item<?>> OneOrMore<T> makeOneOrMore(Sequence<T> sequence) throws XPathException { |
| 45 | List<T> content = new ArrayList<>(); |
| 46 | sequence.iterate().forEachOrFail(content::add); |
| 47 | if (content.isEmpty()) { |
| 48 | throw new IllegalArgumentException(); |
| 49 | } |
| 50 | return new OneOrMore<>(content); |
| 51 | } |
| 52 | |
| 53 | } |