ObjectSpace Homepage

JGL - The Generic Collection Library for Java
All Packages  Class Hierarchy  This Package  Previous  Next  Index

Adapters examples

Adapters1 - Adapting built in Java arrays of Integers with algorithm usage.

Adapters2 - Adapting java.util.Vector with algorithm usage.

Adapters3 - Adapt a primitive array to algorithms and to a buffer.


Adapters1 Example Code

// Copyright(c) 1996,1997 ObjectSpace, Inc.
import com.objectspace.jgl.*;
import com.objectspace.jgl.adapters.*;
import com.objectspace.jgl.algorithms.*;

/**
 * Adapting built in Java arrays of Integers with algorithm usage.
 *
 * @see com.objectspace.jgl.util.ArrayAdapter
 * @version 3.0.0
 * @author ObjectSpace, Inc.
 */

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();
    }
  }

Adapters1 Example Output

Unsorted native int array = int[]( 3, -1, 2, -3, 4 )
Sorted = int[]( -3, -1, 2, 3, 4 )
Randomized = int[]( -3, -1, 3, 4, 2 )
-3 -1 3 4 2

Adapters2 Example Code

// Copyright(c) 1996,1997 ObjectSpace, Inc.
import com.objectspace.jgl.*;
import com.objectspace.jgl.adapters.*;
import com.objectspace.jgl.algorithms.*;
import java.util.Vector;

/**
 * Adapting java.util.Vector with algorithm usage.
 *
 * @see com.objectspace.jgl.util.ArrayAdapter
 * @version 3.0.0
 * @author ObjectSpace, Inc.
 */

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 );
    }
  }

Adapters2 Example Output

Unsorted JDK Vector = [3, -1, 2, -3, 4]
Sorted = [-3, -1, 2, 3, 4]
Randomized = [-3, 4, 3, 2, -1]
JDK vector = [-3, 4, 3, 2, -1]

Adapters3 Example Code

// Copyright(c) 1997 ObjectSpace, Inc.
import com.objectspace.jgl.*;
import com.objectspace.jgl.adapters.*;
import com.objectspace.jgl.algorithms.*;

/**
 * Adapt a primitive array to algorithms and to a buffer.
 *
 * @see com.objectspace.jgl.util.ArrayAdapter
 * @version 3.0.0
 * @author ObjectSpace, Inc.
 */

public class Adapters3
  {
  public static void main( String[] args )
    {
    // start with a native array
    float floats[] = { 3.0f, -1.1f, 2.0f, -3.1f, 4.0f };
    System.out.print( "native = " );
    Printing.println( FloatIterator.begin( floats ), FloatIterator.end( floats ) );

    // FloatArray affects the underlying array
    FloatArray floatArray = new FloatArray( floats );
    System.out.println( "Unsorted = " + floatArray );
    Sorting.sort( floatArray );
    System.out.println( "Sorted = " + floatArray );
    Shuffling.randomShuffle( floatArray );
    System.out.println( "Randomized = " + floatArray );
    System.out.print( "native = " );
    Printing.println( FloatIterator.begin( floats ), FloatIterator.end( floats ) );

    // FloatBuffer does not affect the underlying array
    FloatBuffer floatBuffer = new FloatBuffer( floats );
    System.out.println( "Unsorted = " + floatBuffer );
    Sorting.sort( floatBuffer );
    System.out.println( "Sorted = " + floatBuffer );
    Shuffling.randomShuffle( floatBuffer );
    System.out.println( "Randomized = " + floatBuffer );
    System.out.print( "native = " );
    Printing.println( FloatIterator.begin( floats ), FloatIterator.end( floats ) );

    // Buffers are growable
    floatBuffer.insert( 3, 5.6f );
    System.out.println( "Inserted = " + floatBuffer );
    floatBuffer.remove( 1, 3 );
    System.out.println( "Removed = " + floatBuffer );

    // and return to a native array
    floats = floatBuffer.get();
    System.out.print( "native = " );
    Printing.println( FloatIterator.begin( floats ), FloatIterator.end( floats ) );
    }
  }

Adapters3 Example Output

native = ( 3.0, -1.1, 2.0, -3.1, 4.0 )
Unsorted = float[]( 3.0, -1.1, 2.0, -3.1, 4.0 )
Sorted = float[]( -1.1, -3.1, 2.0, 3.0, 4.0 )
Randomized = float[]( 3.0, 2.0, 4.0, -1.1, -3.1 )
native = ( 3.0, 2.0, 4.0, -1.1, -3.1 )
Unsorted = FloatBuffer( 3.0, 2.0, 4.0, -1.1, -3.1 )
Sorted = FloatBuffer( -1.1, -3.1, 2.0, 3.0, 4.0 )
Randomized = FloatBuffer( -3.1, 3.0, -1.1, 2.0, 4.0 )
native = ( 3.0, 2.0, 4.0, -1.1, -3.1 )
Inserted = FloatBuffer( -3.1, 3.0, -1.1, 5.6, 2.0, 4.0 )
Removed = FloatBuffer( -3.1, 2.0, 4.0 )
native = ( -3.1, 2.0, 4.0 )

All Packages  Class Hierarchy  This Package  Previous  Next  Index