Debian Codegraph @

net/sf/saxon/value/ObjectValue.java

raw symbols SCIP
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.value;
9
10 import net.sf.saxon.om.Genre;
11 import net.sf.saxon.trans.Err;
12 import net.sf.saxon.type.ItemType;
13 import net.sf.saxon.type.TypeHierarchy;
14
15 import java.util.Objects;
16
17
18 /**
19 * An XPath value that encapsulates a Java object. Such a value can only be constructed from
20 * Java code either in a Java extension function, or in the calling application.
21 * <p>Until Saxon 9.4, ObjectValue was a subclass of AtomicValue, and external Java objects were
22 * considered to be atomic. From 9.5, this has been changed so that an ObjectValue is another
23 * kind of Item: a fourth kind, at the same level as nodes, atomic values, and function items.</p>
24 * <p>In the same way as ObjectValue is a wrapper around a Java instance object, the type of the
25 * value is a wrapper around the corresponding Java class. These types have subtype-supertype
26 * relationships that correspond to the Java class hierarchy. The top type in this branch of
27 * the class hierarchy, which wraps the Java class "Object", has item() as its superclass.
28 * These types can be referred to in the SequenceType syntax using QNames of the form
29 * jt:full.package.name.ClassName, where the conventional prefix jt maps to the namespace
30 * URI http://saxon.sf.net/java-type.</p>
31 * <p>An ObjectValue is no longer permitted to wrap a Java null. An empty sequence should be
32 * used in this situation.</p>
33 * <p>The effective boolean value of a wrapped Java object, like that of a node, is always true.</p>
34 * <p>Atomizing a wrapped Java object, or applying the {@code fn:string()} function, returns
35 * an {@code xs:string} whose value is the result of calling {@code toString()} on the wrapped
36 * Java object.</p>
37 */
38
39 public class ObjectValue<T> implements ExternalObject<T> {
40
41 /*@NotNull*/ private T value;
42
43 /**
44 * Constructor
45 *
46 * @param object the object to be encapsulated
47 */
48
49 public ObjectValue(/*@NotNull*/ T object) {
50 value = Objects.requireNonNull(object, "External object cannot wrap a Java null");
51 }
52
53 /**
54 * Get the genre of this item
55 *
56 * @return the genre
57 */
58 @Override
59 public Genre getGenre() {
60 return Genre.EXTERNAL;
61 }
62
63 /**
64 * Get the value of the item as a string. For nodes, this is the string value of the
65 * node as defined in the XPath 2.0 data model, except that all nodes are treated as being
66 * untyped: it is not an error to get the string value of a node with a complex type.
67 * For atomic values, the method returns the result of casting the atomic value to a string.
68 * <p>If the calling code can handle any CharSequence, the method {@link #getStringValueCS} should
69 * be used. If the caller requires a string, this method is preferred.</p>
70 *
71 * @return the string value of the item
72 * @throws UnsupportedOperationException if the item is a function item (an unchecked exception
73 * is used here to avoid introducing exception handling to a large number of paths where it is not
74 * needed)
75 * @see #getStringValueCS
76 * @since 8.4
77 */
78 public String getStringValue() {
79 return value.toString();
80 }
81
82 /**
83 * Get the string value of the item as a CharSequence. This is in some cases more efficient than
84 * the version of the method that returns a String. The method satisfies the rule that
85 * <code>X.getStringValueCS().toString()</code> returns a string that is equal to
86 * <code>X.getStringValue()</code>.
87 * <p>Note that two CharSequence values of different types should not be compared using equals(), and
88 * for the same reason they should not be used as a key in a hash table.</p>
89 * <p>If the calling code can handle any CharSequence, this method should
90 * be used. If the caller requires a string, the {@link #getStringValue} method is preferred.</p>
91 *
92 * @return the string value of the item
93 * @throws UnsupportedOperationException if the item is a function item (an unchecked exception
94 * is used here to avoid introducing exception handling to a large number of paths where it is not
95 * needed)
96 * @see #getStringValue
97 * @since 8.4
98 */
99 public CharSequence getStringValueCS() {
100 return value.toString();
101 }
102
103 /**
104 * Atomize the item.
105 *
106 * @return the result of atomization. For an external object this is defined
107 * to be the result of the toString() method, as an xs:string value.
108 */
109 public StringValue atomize() {
110 return new StringValue(getStringValue());
111 }
112
113 /**
114 * Determine the data type of the items in the expression, if possible
115 *
116 * @param th The TypeHierarchy.
117 * @return for the default implementation: AnyItemType (not known)
118 */
119
120 /*@NotNull*/
121 public ItemType getItemType(/*@Nullable*/ TypeHierarchy th) {
122 return th.getConfiguration().getJavaExternalObjectType(value.getClass());
123 }
124
125 /**
126 * Display the type name for use in error messages
127 *
128 * @return the type name. This will be in the form "java-type:" followed by the full class name of the
129 * wrapped Java object.
130 */
131
132 /*@NotNull*/
133 public static String displayTypeName(Object value) {
134 return "java-type:" + value.getClass().getName();
135 }
136
137 /**
138 * Get the effective boolean value of the value
139 *
140 * @return true (always)
141 */
142 public boolean effectiveBooleanValue() {
143 return true;
144 }
145
146 /**
147 * Get the encapsulated object
148 *
149 * @return the Java object that this external object wraps
150 */
151
152 @Override
153 public T getObject() {
154 return value;
155 }
156
157 /**
158 * Determine if two ObjectValues are equal
159 *
160 * @return true if the other object is also a wrapped Java object and if the two wrapped objects compare
161 * equal using the Java equals() method.
162 */
163
164 public boolean equals(/*@NotNull*/ Object other) {
165 if (other instanceof ObjectValue) {
166 Object o = ((ObjectValue) other).value;
167 return value.equals(o);
168 } else {
169 return false;
170 }
171 }
172
173 public int hashCode() {
174 return value.hashCode();
175 }
176
177 @Override
178 public String toShortString() {
179 String v = value.toString();
180 if (v.startsWith(value.getClass().getName())) {
181 return v;
182 } else {
183 return "(" + value.getClass().getSimpleName() + ")" + Err.truncate30(value.toString());
184 }
185 }
186 }
187