Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/javax/sql/rowset/serial/SerialRef.java
38918 views
/*1* Copyright (c) 2003, 2013, 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* <h3> Thread safety </h3>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*/46public class SerialRef implements Ref, Serializable, Cloneable {4748/**49* String containing the base type name.50* @serial51*/52private String baseTypeName;5354/**55* This will store the type <code>Ref</code> as an <code>Object</code>.56*/57private Object object;5859/**60* Private copy of the Ref reference.61*/62private Ref reference;6364/**65* Constructs a <code>SerialRef</code> object from the given <code>Ref</code>66* object.67*68* @param ref a Ref object; cannot be <code>null</code>69* @throws SQLException if a database access occurs; if <code>ref</code>70* is <code>null</code>; or if the <code>Ref</code> object returns a71* <code>null</code> value base type name.72* @throws SerialException if an error occurs serializing the <code>Ref</code>73* object74*/75public SerialRef(Ref ref) throws SerialException, SQLException {76if (ref == null) {77throw new SQLException("Cannot instantiate a SerialRef object " +78"with a null Ref object");79}80reference = ref;81object = ref;82if (ref.getBaseTypeName() == null) {83throw new SQLException("Cannot instantiate a SerialRef object " +84"that returns a null base type name");85} else {86baseTypeName = ref.getBaseTypeName();87}88}8990/**91* Returns a string describing the base type name of the <code>Ref</code>.92*93* @return a string of the base type name of the Ref94* @throws SerialException in no Ref object has been set95*/96public String getBaseTypeName() throws SerialException {97return baseTypeName;98}99100/**101* Returns an <code>Object</code> representing the SQL structured type102* to which this <code>SerialRef</code> object refers. The attributes103* of the structured type are mapped according to the given type map.104*105* @param map a <code>java.util.Map</code> object containing zero or106* more entries, with each entry consisting of 1) a <code>String</code>107* giving the fully qualified name of a UDT and 2) the108* <code>Class</code> object for the <code>SQLData</code> implementation109* that defines how the UDT is to be mapped110* @return an object instance resolved from the Ref reference and mapped111* according to the supplied type map112* @throws SerialException if an error is encountered in the reference113* resolution114*/115public Object getObject(java.util.Map<String,Class<?>> map)116throws SerialException117{118map = new Hashtable<String, Class<?>>(map);119if (object != null) {120return map.get(object);121} else {122throw new SerialException("The object is not set");123}124}125126/**127* Returns an <code>Object</code> representing the SQL structured type128* to which this <code>SerialRef</code> object refers.129*130* @return an object instance resolved from the Ref reference131* @throws SerialException if an error is encountered in the reference132* resolution133*/134public Object getObject() throws SerialException {135136if (reference != null) {137try {138return reference.getObject();139} catch (SQLException e) {140throw new SerialException("SQLException: " + e.getMessage());141}142}143144if (object != null) {145return object;146}147148149throw new SerialException("The object is not set");150151}152153/**154* Sets the SQL structured type that this <code>SerialRef</code> object155* references to the given <code>Object</code> object.156*157* @param obj an <code>Object</code> representing the SQL structured type158* to be referenced159* @throws SerialException if an error is encountered generating the160* the structured type referenced by this <code>SerialRef</code> object161*/162public void setObject(Object obj) throws SerialException {163try {164reference.setObject(obj);165} catch (SQLException e) {166throw new SerialException("SQLException: " + e.getMessage());167}168object = obj;169}170171/**172* Compares this SerialRef to the specified object. The result is {@code173* true} if and only if the argument is not {@code null} and is a {@code174* SerialRef} object that represents the same object as this175* object.176*177* @param obj The object to compare this {@code SerialRef} against178*179* @return {@code true} if the given object represents a {@code SerialRef}180* equivalent to this SerialRef, {@code false} otherwise181*182*/183public boolean equals(Object obj) {184if (this == obj) {185return true;186}187if(obj instanceof SerialRef) {188SerialRef ref = (SerialRef)obj;189return baseTypeName.equals(ref.baseTypeName) &&190object.equals(ref.object);191}192return false;193}194195/**196* Returns a hash code for this {@code SerialRef}.197* @return a hash code value for this object.198*/199public int hashCode() {200return (31 + object.hashCode()) * 31 + baseTypeName.hashCode();201}202203/**204* Returns a clone of this {@code SerialRef}.205* The underlying {@code Ref} object will be set to null.206*207* @return a clone of this SerialRef208*/209public Object clone() {210try {211SerialRef ref = (SerialRef) super.clone();212ref.reference = null;213return ref;214} catch (CloneNotSupportedException ex) {215// this shouldn't happen, since we are Cloneable216throw new InternalError();217}218219}220221/**222* readObject is called to restore the state of the SerialRef from223* a stream.224*/225private void readObject(ObjectInputStream s)226throws IOException, ClassNotFoundException {227ObjectInputStream.GetField fields = s.readFields();228object = fields.get("object", null);229baseTypeName = (String) fields.get("baseTypeName", null);230reference = (Ref) fields.get("reference", null);231}232233/**234* writeObject is called to save the state of the SerialRef235* to a stream.236*/237private void writeObject(ObjectOutputStream s)238throws IOException, ClassNotFoundException {239240ObjectOutputStream.PutField fields = s.putFields();241fields.put("baseTypeName", baseTypeName);242fields.put("object", object);243// Note: this check to see if it is an instance of Serializable244// is for backwards compatibiity245fields.put("reference", reference instanceof Serializable ? reference : null);246s.writeFields();247}248249/**250* The identifier that assists in the serialization of this <code>SerialRef</code>251* object.252*/253static final long serialVersionUID = -4727123500609662274L;254255256}257258259