Package safejdbc.holder

This package contains a variety of holder classes.

See:
          Description

Class Summary
BooleanHolder This class is designed to hold a boolean.
ByteHolder This class is designed to hold a byte.
CharHolder This class is designed to hold a char.
DoubleHolder This class is designed to hold a double.
FloatHolder This class is designed to hold a float.
IntHolder This class is designed to hold an int.
LongHolder This class is designed to hold a long.
ObjectHolder This class is designed to hold an object.
ShortHolder This class is designed to hold a short.
StringHolder This class is designed to hold a string.
 

Package safejdbc.holder Description

This package contains a variety of holder classes. The purpose of these classes is to pass data into and - much more importantly - out of code blocks such as ResultSetProcessors or FillingCommands.

They are best explained using an example. Consider the following code snippet which retrieves a name from a ResultSetIterator. (For a general introduction to using the framework, including details about the SqlExecuter and ResultSetIterator classes and examples, see the Introduction).

/** return the name of a person identified by id */
public String getNameForId (String id) {
  final StringHolder resultHolder = new StringHolder ();

  .... // initialize SqlExecuter
  sqlExecuter.query ("SELECT name FROM person WHERE id='" + id + "'",
    new ResultSetIterator () {
      public void forEachRow (Map data) {
        // resultHolder is accessible because it is final
        resultHolder.setValue ("" + data.get ("name"));
      }
    });

  // if the result set was not empty, resultHolder now contains the
  // name of the person. Otherwise, it contains null - which is an
  // appropriate return value
  return resultHolder.getValue ();
}

The key point of the example is that the StringHolder is final so that it is accessible inside the anonymous local class but that it contains a value which can be modified nonetheless. In a manner of speaking, it manges to be unchangeable and changeable at the same time. There are holder classes for all primitive types plus String and Object.

Holders are indispensable for getting data out of code blocks. They are also sometimes useful for getting data into code blocks to be modified there. To only get data into a code block it is sufficient to just make the variable itself final, there is no need for a holder.

To facilitate such changes to the data of holders, convenience methods were added to many of the holder classes. In order to collect data during the iteration through a ResultSet, a standard JDK Collection works fine.



Copyright (c) 2001, 2002 by Jan Hermanns and Arno Haase. All Rights Reserved.