| 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.value.*; |
| 11 | |
| 12 | /** |
| 13 | * A sequence containing exactly one item. The main use of this class is in declaring the expected arguments |
| 14 | * of reflexive method calls, where the use of One(T) rather than T emphasizes that the value must not be null/empty, |
| 15 | * and generates type-checking code to ensure that it is not empty. |
| 16 | * |
| 17 | * <p>To extract the wrapped item, use {@link #head()}.</p> |
| 18 | */ |
| 19 | public class One<T extends Item<?>> extends ZeroOrOne<T> { |
| 20 | |
| 21 | /** |
| 22 | * Create an instance of the class |
| 23 | * @param item The single item to be contained in the sequence. Must not be null. |
| 24 | * @throws NullPointerException if item is null. |
| 25 | */ |
| 26 | |
| 27 | public One(T item) { |
| 28 | super(item); |
| 29 | if (item == null) { |
| 30 | throw new NullPointerException(); |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * Convenience function to create a singleton boolean value |
| 36 | * @param value the boolean value |
| 37 | * @return the boolean value wrapped as a One<BooleanValue> |
| 38 | */ |
| 39 | |
| 40 | public static One<BooleanValue> bool (boolean value) { |
| 41 | return new One<>(BooleanValue.get(value)); |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Convenience function to create a singleton string value |
| 46 | * @param value the string value |
| 47 | * @return the boolean value wrapped as a One<StringValue> |
| 48 | */ |
| 49 | |
| 50 | public static One<StringValue> string (String value) { |
| 51 | return new One<>(new StringValue(value)); |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Convenience function to create a singleton integer value |
| 56 | * @param value the integer value |
| 57 | * @return the integer value wrapped as a One<IntegerValue> |
| 58 | */ |
| 59 | |
| 60 | public static One<IntegerValue> integer (long value) { |
| 61 | return new One<>(new Int64Value(value)); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Convenience function to create a singleton double value |
| 66 | * |
| 67 | * @param value the double value |
| 68 | * @return the double value wrapped as a One<DoubleValue> |
| 69 | */ |
| 70 | |
| 71 | public static One<DoubleValue> dbl(double value) { |
| 72 | return new One<>(new DoubleValue(value)); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 |