| 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.s9api; |
| 9 | |
| 10 | import net.sf.saxon.value.ExternalObject; |
| 11 | import net.sf.saxon.value.ObjectValue; |
| 12 | |
| 13 | /** |
| 14 | * The class XdmExternalObject represents an XDM item that wraps an external (Java or .NET) object. |
| 15 | * As such, it is outside the scope of the XDM specification (but permitted as an extension). |
| 16 | * <p>In releases prior to 9.5, external objects in Saxon were represented as atomic values. From |
| 17 | * 9.5 they are represented as a fourth kind of item, alongside nodes, atomic values, and functions.</p> |
| 18 | */ |
| 19 | public class XdmExternalObject extends XdmItem { |
| 20 | |
| 21 | private XdmExternalObject(){} |
| 22 | |
| 23 | /** |
| 24 | * Create an XdmExternalObject that wraps a supplied Java object |
| 25 | * @param value the supplied Java object. Must not be null. |
| 26 | * @throws NullPointerException if the supplied value is null. |
| 27 | */ |
| 28 | |
| 29 | public XdmExternalObject(Object value) { |
| 30 | super(value instanceof ObjectValue ? (ObjectValue)value : new ObjectValue<>(value)); |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * Get the wrapped Java object |
| 35 | * |
| 36 | * @return the wrapped object |
| 37 | */ |
| 38 | |
| 39 | public Object getExternalObject() { |
| 40 | return ((ExternalObject) getUnderlyingValue()).getObject(); |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Get the result of converting the external value to a string. |
| 45 | * @return the result of applying toString() to the wrapped external object. |
| 46 | */ |
| 47 | |
| 48 | public String toString() { |
| 49 | return getExternalObject().toString(); |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Compare two external objects for equality. Two instances of {@code XdmExternalObject} are equal |
| 54 | * if the Java objects that they wrap are equal. |
| 55 | * @param other the object to be compared |
| 56 | * @return true if the other object is an {@code XdmExternalObject} and the two wrapped objects |
| 57 | * are equal under the {@code equals()} method. |
| 58 | */ |
| 59 | |
| 60 | public boolean equals(Object other) { |
| 61 | return other instanceof XdmExternalObject |
| 62 | && getUnderlyingValue().equals(((XdmExternalObject)other).getUnderlyingValue()); |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Return a hash code for the object. This respects the semantics of {@link #equals(Object)}; |
| 67 | * @return a suitable hash code |
| 68 | */ |
| 69 | |
| 70 | public int hashCode() { |
| 71 | return getUnderlyingValue().hashCode(); |
| 72 | } |
| 73 | |
| 74 | |
| 75 | } |
| 76 |