One of the primary strengths of JGL is the ability
to apply its generic algorithms to JGL containers, native Java
arrays, and JDK data structures. The way that it does this is
to define the algorithms to accept data structures that implement
the Container
interface and then to supply special adapter classes that allow
native Java arrays and JDK data structures to act as if they were
a JGL Container
.
Here is a diagram that shows all of the JGL array adapters:
This chapter describes the adapter classes for native
Java arrays and JDK containers.
There is a separate adapter class for each kind of
native Java array. When you enumerate a native Java array of primitives
using the adapter class, each primitive is automatically retrieved
as its closest object equivalent as defined in java.lang
.
Here is a table that shows this information for each type:
Native Array Type | Adapter Class | Retrieved As |
boolean[]
| BooleanArray
| Boolean
|
byte[]
| ByteArray
| Integer
|
char[]
| CharArray
| Character
|
short[]
| ShortArray
| Integer
|
int[]
| IntArray
| Integer
|
long[]
| LongArray
| Long
|
float[]
| FloatArray
| Float
|
double[]
| DoubleArray
| Double
|
Object[]
| ObjectArray
| N/A
|
The adapter classes implement all of the methods defined by the
Sequence
interface. Because the size of a native Java array is fixed, the
add()
,
clear(), pushBack(),
popBack(), pushFront(), popFront(),
and
remove()
methods
will always throw an InvalidOperationException
.
The following example uses an IntArray
adapter class to conveniently print, sort, and shuffle a native
Java array of ints
.
// Copyright(c) 1996,1997 ObjectSpace, Inc.
import COM.objectspace.jgl.*;
public class Adapters1
{
public static void main( String[] args )
{
int ints[] = { 3, -1, 2, -3, 4 };
IntArray intArray = new IntArray( ints );
System.out.println( "Unsorted native int array = " + intArray );
Sorting.sort( intArray );
System.out.println( "Sorted = " + intArray );
Shuffling.randomShuffle( intArray );
System.out.println( "Randomized = " + intArray );
for ( int i = 0; i < ints.length; i++ )
System.out.print( ints[ i ] + " " );
System.out.println();
}
}
Output
Unsorted native int array = int[]( 3, -1, 2, -3, 4 )
Sorted = int[]( -3, -1, 2, 3, 4 )
Randomized = int[]( -1, -3, 4, 2, 3 )
-1 -3 4 2 3
The VectorArray
adapter class allows a JDK Vector
to act as if it were a JGL Container
.
The add()
,
clear()
, pushFront()
,
pushback()
, popFront()
, popBack()
, and
remove()
methods work correctly, and
are automatically converted into their JDK equivalents. The following
example uses a VectorArray
to sort and shuffle a JDK Vector.
// Copyright(c) 1996,1997 ObjectSpace, Inc.
import COM.objectspace.jgl.*;
import java.util.Vector;
public class Adapters2
{
public static void main( String[] args )
{
Vector vector = new Vector();
vector.addElement( new Integer( 3 ) );
vector.addElement( new Integer( -1 ) );
vector.addElement( new Integer( 2 ) );
vector.addElement( new Integer( -3 ) );
vector.addElement( new Integer( 4 ) );
VectorArray vectorArray = new VectorArray( vector );
System.out.println( "Unsorted JDK Vector = " + vectorArray );
Sorting.sort( vectorArray );
System.out.println( "Sorted = " + vectorArray );
Shuffling.randomShuffle( vectorArray );
System.out.println( "Randomized = " + vectorArray );
System.out.println( "JDK vector = " + vector );
}
}
Output
Unsorted JDK Vector = [3, -1, 2, -3, 4]
Sorted = [-3, -1, 2, 3, 4]
Randomized = [2, -1, 4, -3, 3]
JDK vector = [2, -1, 4, -3, 3]