Path: blob/master/src/java.sql.rowset/share/classes/com/sun/rowset/internal/Row.java
40948 views
/*1* Copyright (c) 2003, 2011, 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 com.sun.rowset.internal;2627import java.sql.*;28import java.io.*;29import java.lang.*;30import java.util.*;3132/**33* A class that keeps track of a row's values. A <code>Row</code> object34* maintains an array of current column values and an array of original35* column values, and it provides methods for getting and setting the36* value of a column. It also keeps track of which columns have37* changed and whether the change was a delete, insert, or update.38* <P>39* Note that column numbers for rowsets start at <code>1</code>,40* whereas the first element of an array or bitset is <code>0</code>.41* The argument for the method <code>getColumnUpdated</code> refers to42* the column number in the rowset (the first column is <code>1</code>);43* the argument for <code>setColumnUpdated</code> refers to the index44* into the rowset's internal bitset (the first bit is <code>0</code>).45*/46public class Row extends BaseRow implements Serializable, Cloneable {4748static final long serialVersionUID = 5047859032611314762L;4950/**51* An array containing the current column values for this <code>Row</code>52* object.53* @serial54*/55private Object[] currentVals;5657/**58* A <code>BitSet</code> object containing a flag for each column in59* this <code>Row</code> object, with each flag indicating whether or60* not the value in the column has been changed.61* @serial62*/63private BitSet colsChanged;6465/**66* A <code>boolean</code> indicating whether or not this <code>Row</code>67* object has been deleted. <code>true</code> indicates that it has68* been deleted; <code>false</code> indicates that it has not.69* @serial70*/71private boolean deleted;7273/**74* A <code>boolean</code> indicating whether or not this <code>Row</code>75* object has been updated. <code>true</code> indicates that it has76* been updated; <code>false</code> indicates that it has not.77* @serial78*/79private boolean updated;8081/**82* A <code>boolean</code> indicating whether or not this <code>Row</code>83* object has been inserted. <code>true</code> indicates that it has84* been inserted; <code>false</code> indicates that it has not.85* @serial86*/87private boolean inserted;8889/**90* The number of columns in this <code>Row</code> object.91* @serial92*/93private int numCols;9495/**96* Creates a new <code>Row</code> object with the given number of columns.97* The newly-created row includes an array of original values,98* an array for storing its current values, and a <code>BitSet</code>99* object for keeping track of which column values have been changed.100*/101public Row(int numCols) {102origVals = new Object[numCols];103currentVals = new Object[numCols];104colsChanged = new BitSet(numCols);105this.numCols = numCols;106}107108/**109* Creates a new <code>Row</code> object with the given number of columns110* and with its array of original values initialized to the given array.111* The new <code>Row</code> object also has an array for storing its112* current values and a <code>BitSet</code> object for keeping track113* of which column values have been changed.114*/115public Row(int numCols, Object[] vals) {116origVals = new Object[numCols];117System.arraycopy(vals, 0, origVals, 0, numCols);118currentVals = new Object[numCols];119colsChanged = new BitSet(numCols);120this.numCols = numCols;121}122123/**124*125* This method is called internally by the <code>CachedRowSet.populate</code>126* methods.127*128* @param idx the number of the column in this <code>Row</code> object129* that is to be set; the index of the first column is130* <code>1</code>131* @param val the new value to be set132*/133public void initColumnObject(int idx, Object val) {134origVals[idx - 1] = val;135}136137138/**139*140* This method is called internally by the <code>CachedRowSet.updateXXX</code>141* methods.142*143* @param idx the number of the column in this <code>Row</code> object144* that is to be set; the index of the first column is145* <code>1</code>146* @param val the new value to be set147*/148public void setColumnObject(int idx, Object val) {149currentVals[idx - 1] = val;150setColUpdated(idx - 1);151}152153/**154* Retrieves the column value stored in the designated column of this155* <code>Row</code> object.156*157* @param columnIndex the index of the column value to be retrieved;158* the index of the first column is <code>1</code>159* @return an <code>Object</code> in the Java programming language that160* represents the value stored in the designated column161* @throws SQLException if there is a database access error162*/163public Object getColumnObject(int columnIndex) throws SQLException {164if (getColUpdated(columnIndex - 1)) {165return(currentVals[columnIndex - 1]); // maps to array!!166} else {167return(origVals[columnIndex - 1]); // maps to array!!168}169}170171/**172* Indicates whether the designated column of this <code>Row</code> object173* has been changed.174* @param idx the index into the <code>BitSet</code> object maintained by175* this <code>Row</code> object to keep track of which column176* values have been modified; the index of the first bit is177* <code>0</code>178* @return <code>true</code> if the designated column value has been changed;179* <code>false</code> otherwise180*181*/182public boolean getColUpdated(int idx) {183return colsChanged.get(idx);184}185186/**187* Sets this <code>Row</code> object's <code>deleted</code> field188* to <code>true</code>.189*190* @see #getDeleted191*/192public void setDeleted() { // %%% was public193deleted = true;194}195196197/**198* Retrieves the value of this <code>Row</code> object's <code>deleted</code> field,199* which will be <code>true</code> if one or more of its columns has been200* deleted.201* @return <code>true</code> if a column value has been deleted; <code>false</code>202* otherwise203*204* @see #setDeleted205*/206public boolean getDeleted() {207return(deleted);208}209210/**211* Sets the <code>deleted</code> field for this <code>Row</code> object to212* <code>false</code>.213*/214public void clearDeleted() {215deleted = false;216}217218219/**220* Sets the value of this <code>Row</code> object's <code>inserted</code> field221* to <code>true</code>.222*223* @see #getInserted224*/225public void setInserted() {226inserted = true;227}228229230/**231* Retrieves the value of this <code>Row</code> object's <code>inserted</code> field,232* which will be <code>true</code> if this row has been inserted.233* @return <code>true</code> if this row has been inserted; <code>false</code>234* otherwise235*236* @see #setInserted237*/238public boolean getInserted() {239return(inserted);240}241242243/**244* Sets the <code>inserted</code> field for this <code>Row</code> object to245* <code>false</code>.246*/247public void clearInserted() { // %%% was public248inserted = false;249}250251/**252* Retrieves the value of this <code>Row</code> object's253* <code>updated</code> field.254* @return <code>true</code> if this <code>Row</code> object has been255* updated; <code>false</code> if it has not256*257* @see #setUpdated258*/259public boolean getUpdated() {260return(updated);261}262263/**264* Sets the <code>updated</code> field for this <code>Row</code> object to265* <code>true</code> if one or more of its column values has been changed.266*267* @see #getUpdated268*/269public void setUpdated() {270// only mark something as updated if one or271// more of the columns has been changed.272for (int i = 0; i < numCols; i++) {273if (getColUpdated(i) == true) {274updated = true;275return;276}277}278}279280/**281* Sets the bit at the given index into this <code>Row</code> object's internal282* <code>BitSet</code> object, indicating that the corresponding column value283* (column <code>idx</code> + 1) has been changed.284*285* @param idx the index into the <code>BitSet</code> object maintained by286* this <code>Row</code> object; the first bit is at index287* <code>0</code>288*289*/290private void setColUpdated(int idx) {291colsChanged.set(idx);292}293294/**295* Sets the <code>updated</code> field for this <code>Row</code> object to296* <code>false</code>, sets all the column values in this <code>Row</code>297* object's internal array of current values to <code>null</code>, and clears298* all of the bits in the <code>BitSet</code> object maintained by this299* <code>Row</code> object.300*/301public void clearUpdated() {302updated = false;303for (int i = 0; i < numCols; i++) {304currentVals[i] = null;305colsChanged.clear(i);306}307}308309/**310* Sets the column values in this <code>Row</code> object's internal311* array of original values with the values in its internal array of312* current values, sets all the values in this <code>Row</code>313* object's internal array of current values to <code>null</code>,314* clears all the bits in this <code>Row</code> object's internal bitset,315* and sets its <code>updated</code> field to <code>false</code>.316* <P>317* This method is called internally by the <code>CachedRowSet</code>318* method <code>makeRowOriginal</code>.319*/320public void moveCurrentToOrig() {321for (int i = 0; i < numCols; i++) {322if (getColUpdated(i) == true) {323origVals[i] = currentVals[i];324currentVals[i] = null;325colsChanged.clear(i);326}327}328updated = false;329}330331/**332* Returns the row on which the cursor is positioned.333*334* @return the <code>Row</code> object on which the <code>CachedRowSet</code>335* implementation objects's cursor is positioned336*/337public BaseRow getCurrentRow() {338return null;339}340}341342343