Path: blob/master/src/java.sql.rowset/share/classes/javax/sql/rowset/serial/SerialRef.java
40948 views
/*1* Copyright (c) 2003, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package javax.sql.rowset.serial;2627import java.sql.*;28import java.io.*;29import java.util.*;3031/**32* A serialized mapping of a <code>Ref</code> object, which is the mapping in the33* Java programming language of an SQL <code>REF</code> value.34* <p>35* The <code>SerialRef</code> class provides a constructor for36* creating a <code>SerialRef</code> instance from a <code>Ref</code>37* object and provides methods for getting and setting the <code>Ref</code> object.38*39* <h2> Thread safety </h2>40*41* A SerialRef is not safe for use by multiple concurrent threads. If a42* SerialRef is to be used by more than one thread then access to the SerialRef43* should be controlled by appropriate synchronization.44*45* @since 1.546*/47public class SerialRef implements Ref, Serializable, Cloneable {4849/**50* String containing the base type name.51* @serial52*/53private String baseTypeName;5455/**56* This will store the type <code>Ref</code> as an <code>Object</code>.57*/58@SuppressWarnings("serial") // Not statically typed as Serializable59private Object object;6061/**62* Private copy of the Ref reference.63*/64@SuppressWarnings("serial") // Not statically typed as Serializable; checked in writeObject65private Ref reference;6667/**68* Constructs a <code>SerialRef</code> object from the given <code>Ref</code>69* object.70*71* @param ref a Ref object; cannot be <code>null</code>72* @throws SQLException if a database access occurs; if <code>ref</code>73* is <code>null</code>; or if the <code>Ref</code> object returns a74* <code>null</code> value base type name.75* @throws SerialException if an error occurs serializing the <code>Ref</code>76* object77*/78public SerialRef(Ref ref) throws SerialException, SQLException {79if (ref == null) {80throw new SQLException("Cannot instantiate a SerialRef object " +81"with a null Ref object");82}83reference = ref;84object = ref;85if (ref.getBaseTypeName() == null) {86throw new SQLException("Cannot instantiate a SerialRef object " +87"that returns a null base type name");88} else {89baseTypeName = ref.getBaseTypeName();90}91}9293/**94* Returns a string describing the base type name of the <code>Ref</code>.95*96* @return a string of the base type name of the Ref97* @throws SerialException in no Ref object has been set98*/99public String getBaseTypeName() throws SerialException {100return baseTypeName;101}102103/**104* Returns an <code>Object</code> representing the SQL structured type105* to which this <code>SerialRef</code> object refers. The attributes106* of the structured type are mapped according to the given type map.107*108* @param map a <code>java.util.Map</code> object containing zero or109* more entries, with each entry consisting of 1) a <code>String</code>110* giving the fully qualified name of a UDT and 2) the111* <code>Class</code> object for the <code>SQLData</code> implementation112* that defines how the UDT is to be mapped113* @return an object instance resolved from the Ref reference and mapped114* according to the supplied type map115* @throws SerialException if an error is encountered in the reference116* resolution117*/118public Object getObject(java.util.Map<String,Class<?>> map)119throws SerialException120{121map = new Hashtable<String, Class<?>>(map);122if (object != null) {123return map.get(object);124} else {125throw new SerialException("The object is not set");126}127}128129/**130* Returns an <code>Object</code> representing the SQL structured type131* to which this <code>SerialRef</code> object refers.132*133* @return an object instance resolved from the Ref reference134* @throws SerialException if an error is encountered in the reference135* resolution136*/137public Object getObject() throws SerialException {138139if (reference != null) {140try {141return reference.getObject();142} catch (SQLException e) {143throw new SerialException("SQLException: " + e.getMessage());144}145}146147if (object != null) {148return object;149}150151152throw new SerialException("The object is not set");153154}155156/**157* Sets the SQL structured type that this <code>SerialRef</code> object158* references to the given <code>Object</code> object.159*160* @param obj an <code>Object</code> representing the SQL structured type161* to be referenced162* @throws SerialException if an error is encountered generating the163* the structured type referenced by this <code>SerialRef</code> object164*/165public void setObject(Object obj) throws SerialException {166try {167reference.setObject(obj);168} catch (SQLException e) {169throw new SerialException("SQLException: " + e.getMessage());170}171object = obj;172}173174/**175* Compares this SerialRef to the specified object. The result is {@code176* true} if and only if the argument is not {@code null} and is a {@code177* SerialRef} object that represents the same object as this178* object.179*180* @param obj The object to compare this {@code SerialRef} against181*182* @return {@code true} if the given object represents a {@code SerialRef}183* equivalent to this SerialRef, {@code false} otherwise184*185*/186public boolean equals(Object obj) {187if (this == obj) {188return true;189}190if(obj instanceof SerialRef) {191SerialRef ref = (SerialRef)obj;192return baseTypeName.equals(ref.baseTypeName) &&193object.equals(ref.object);194}195return false;196}197198/**199* Returns a hash code for this {@code SerialRef}.200* @return a hash code value for this object.201*/202public int hashCode() {203return (31 + object.hashCode()) * 31 + baseTypeName.hashCode();204}205206/**207* Returns a clone of this {@code SerialRef}.208* The underlying {@code Ref} object will be set to null.209*210* @return a clone of this SerialRef211*/212public Object clone() {213try {214SerialRef ref = (SerialRef) super.clone();215ref.reference = null;216return ref;217} catch (CloneNotSupportedException ex) {218// this shouldn't happen, since we are Cloneable219throw new InternalError();220}221222}223224/**225* readObject is called to restore the state of the SerialRef from226* a stream.227* @param s the {@code ObjectInputStream} to read from.228*229* @throws ClassNotFoundException if the class of a serialized object230* could not be found.231* @throws IOException if an I/O error occurs.232*/233private void readObject(ObjectInputStream s)234throws IOException, ClassNotFoundException {235ObjectInputStream.GetField fields = s.readFields();236object = fields.get("object", null);237baseTypeName = (String) fields.get("baseTypeName", null);238reference = (Ref) fields.get("reference", null);239}240241/**242* writeObject is called to save the state of the SerialRef243* to a stream.244* @param s the {@code ObjectOutputStream} to write to.245* @throws IOException if an I/O error occurs.246*/247private void writeObject(ObjectOutputStream s)248throws IOException {249250ObjectOutputStream.PutField fields = s.putFields();251fields.put("baseTypeName", baseTypeName);252fields.put("object", object);253// Note: this check to see if it is an instance of Serializable254// is for backwards compatibility255fields.put("reference", reference instanceof Serializable ? reference : null);256s.writeFields();257}258259/**260* The identifier that assists in the serialization of this <code>SerialRef</code>261* object.262*/263static final long serialVersionUID = -4727123500609662274L;264265266}267268269