alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Java example source code file (Row.java)

This example Java source code file (Row.java) is included in the alvinalexander.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Learn more about this Java project at its project page.

Java - Java tags/keywords

baserow, bitset, cloneable, jdbc, object, row, serializable, sql, sqlexception, util

The Row.java Java example source code

/*
 * Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.  Oracle designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Oracle in the LICENSE file that accompanied this code.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

package com.sun.rowset.internal;

import java.sql.*;
import java.io.*;
import java.lang.*;
import java.util.*;

/**
 * A class that keeps track of a row's values. A <code>Row object
 * maintains an array of current column values and an array of original
 * column values, and it provides methods for getting and setting the
 * value of a column.  It also keeps track of which columns have
 * changed and whether the change was a delete, insert, or update.
 * <P>
 * Note that column numbers for rowsets start at <code>1,
 * whereas the first element of an array or bitset is <code>0.
 * The argument for the method <code>getColumnUpdated refers to
 * the column number in the rowset (the first column is <code>1);
 * the argument for <code>setColumnUpdated refers to the index
 * into the rowset's internal bitset (the first bit is <code>0).
 */
public class Row extends BaseRow implements Serializable, Cloneable {

static final long serialVersionUID = 5047859032611314762L;

/**
 * An array containing the current column values for this <code>Row
 * object.
 * @serial
 */
    private Object[] currentVals;

/**
 * A <code>BitSet object containing a flag for each column in
 * this <code>Row object, with each flag indicating whether or
 * not the value in the column has been changed.
 * @serial
 */
    private BitSet colsChanged;

/**
 * A <code>boolean indicating whether or not this Row
 * object has been deleted.  <code>true indicates that it has
 * been deleted; <code>false indicates that it has not.
 * @serial
 */
    private boolean deleted;

/**
 * A <code>boolean indicating whether or not this Row
 * object has been updated.  <code>true indicates that it has
 * been updated; <code>false indicates that it has not.
 * @serial
 */
    private boolean updated;

/**
 * A <code>boolean indicating whether or not this Row
 * object has been inserted.  <code>true indicates that it has
 * been inserted; <code>false indicates that it has not.
 * @serial
 */
    private boolean inserted;

/**
 * The number of columns in this <code>Row object.
 * @serial
 */
    private int numCols;

/**
 * Creates a new <code>Row object with the given number of columns.
 * The newly-created row includes an array of original values,
 * an array for storing its current values, and a <code>BitSet
 * object for keeping track of which column values have been changed.
 */
    public Row(int numCols) {
        origVals = new Object[numCols];
        currentVals = new Object[numCols];
        colsChanged = new BitSet(numCols);
        this.numCols = numCols;
    }

/**
 * Creates a new <code>Row object with the given number of columns
 * and with its array of original values initialized to the given array.
 * The new <code>Row object also has an array for storing its
 * current values and a <code>BitSet object for keeping track
 * of which column values have been changed.
 */
    public Row(int numCols, Object[] vals) {
        origVals = new Object[numCols];
        System.arraycopy(vals, 0, origVals, 0, numCols);
        currentVals = new Object[numCols];
        colsChanged = new BitSet(numCols);
        this.numCols = numCols;
    }

/**
 *
 * This method is called internally by the <code>CachedRowSet.populate
 * methods.
 *
 * @param idx the number of the column in this <code>Row object
 *            that is to be set; the index of the first column is
 *            <code>1
 * @param val the new value to be set
 */
    public void initColumnObject(int idx, Object val) {
        origVals[idx - 1] = val;
    }


/**
 *
 * This method is called internally by the <code>CachedRowSet.updateXXX
 * methods.
 *
 * @param idx the number of the column in this <code>Row object
 *            that is to be set; the index of the first column is
 *            <code>1
 * @param val the new value to be set
 */
    public void setColumnObject(int idx, Object val) {
            currentVals[idx - 1] = val;
            setColUpdated(idx - 1);
    }

/**
 * Retrieves the column value stored in the designated column of this
 * <code>Row object.
 *
 * @param columnIndex the index of the column value to be retrieved;
 *                    the index of the first column is <code>1
 * @return an <code>Object in the Java programming language that
 *         represents the value stored in the designated column
 * @throws SQLException if there is a database access error
 */
    public Object getColumnObject(int columnIndex) throws SQLException {
        if (getColUpdated(columnIndex - 1)) {
            return(currentVals[columnIndex - 1]); // maps to array!!
        } else {
            return(origVals[columnIndex - 1]); // maps to array!!
        }
    }

/**
 * Indicates whether the designated column of this <code>Row object
 * has been changed.
 * @param idx the index into the <code>BitSet object maintained by
 *            this <code>Row object to keep track of which column
 *            values have been modified; the index of the first bit is
 *            <code>0
 * @return <code>true if the designated column value has been changed;
 *         <code>false otherwise
 *
 */
    public boolean getColUpdated(int idx) {
        return colsChanged.get(idx);
    }

/**
 * Sets this <code>Row object's deleted field
 * to <code>true.
 *
 * @see #getDeleted
 */
    public void setDeleted() { // %%% was public
        deleted = true;
    }


/**
 * Retrieves the value of this <code>Row object's deleted field,
 * which will be <code>true if one or more of its columns has been
 * deleted.
 * @return <code>true if a column value has been deleted; false
 *         otherwise
 *
 * @see #setDeleted
 */
    public boolean getDeleted() {
        return(deleted);
    }

/**
 * Sets the <code>deleted field for this Row object to
 * <code>false.
 */
    public void clearDeleted() {
        deleted = false;
    }


/**
 * Sets the value of this <code>Row object's inserted field
 * to <code>true.
 *
 * @see #getInserted
 */
    public void setInserted() {
        inserted = true;
    }


/**
 * Retrieves the value of this <code>Row object's inserted field,
 * which will be <code>true if this row has been inserted.
 * @return <code>true if this row has been inserted; false
 *         otherwise
 *
 * @see #setInserted
 */
    public boolean getInserted() {
        return(inserted);
    }


/**
 * Sets the <code>inserted field for this Row object to
 * <code>false.
 */
    public void clearInserted() { // %%% was public
        inserted = false;
    }

/**
 * Retrieves the value of this <code>Row object's
 * <code>updated field.
 * @return <code>true if this Row object has been
 *         updated; <code>false if it has not
 *
 * @see #setUpdated
 */
    public boolean getUpdated() {
        return(updated);
    }

/**
 * Sets the <code>updated field for this Row object to
 * <code>true if one or more of its column values has been changed.
 *
 * @see #getUpdated
 */
    public void setUpdated() {
        // only mark something as updated if one or
        // more of the columns has been changed.
        for (int i = 0; i < numCols; i++) {
            if (getColUpdated(i) == true) {
                updated = true;
                return;
            }
        }
    }

/**
 * Sets the bit at the given index into this <code>Row object's internal
 * <code>BitSet object, indicating that the corresponding column value
 * (column <code>idx + 1) has been changed.
 *
 * @param idx the index into the <code>BitSet object maintained by
 *            this <code>Row object; the first bit is at index
 *            <code>0
 *
 */
    private void setColUpdated(int idx) {
        colsChanged.set(idx);
    }

/**
 * Sets the <code>updated field for this Row object to
 * <code>false, sets all the column values in this Row
 * object's internal array of current values to <code>null, and clears
 * all of the bits in the <code>BitSet object maintained by this
 * <code>Row object.
 */
    public void clearUpdated() {
        updated = false;
        for (int i = 0; i < numCols; i++) {
            currentVals[i] = null;
            colsChanged.clear(i);
        }
    }

   /**
    * Sets the column values in this <code>Row object's internal
    * array of original values with the values in its internal array of
    * current values, sets all the values in this <code>Row
    * object's internal array of current values to <code>null,
    * clears all the bits in this <code>Row object's internal bitset,
    * and sets its <code>updated field to false.
    * <P>
    * This method is called internally by the <code>CachedRowSet
    * method <code>makeRowOriginal.
    */
    public void moveCurrentToOrig() {
        for (int i = 0; i < numCols; i++) {
            if (getColUpdated(i) == true) {
                origVals[i] = currentVals[i];
                currentVals[i] = null;
                colsChanged.clear(i);
            }
        }
        updated = false;
    }

   /**
    * Returns the row on which the cursor is positioned.
    *
    * @return the <code>Row object on which the CachedRowSet
    *           implementation objects's cursor is positioned
    */
    public BaseRow getCurrentRow() {
        return null;
    }
}

Other Java examples (source code examples)

Here is a short list of links related to this Java Row.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.