/** <CODE>
  ****************************************************************
  * Copyright (C) 1997-2000 Micro Focus International Ltd.       *
  * All Rights Reserved.                                         *
  ****************************************************************
  *                                                              *
  * This COBOL program demonstrates how to read and update Java  *
  * Arrays from COBOL.                                           *
  *                                                              *
  *  Version History:                                            *
  *  @author            Micro Focus International Development    *
  *  @version           1.0, Created                             *
  ****************************************************************
  *</CODE>
  */

import java.awt.List;
import com.microfocus.cobol.*;

/*
 * Java wrapper class for COBOL program
 */
class CobolArrays
{
	void increment(List before, List after, int intArray[])
	{
		Object myParms[] = { intArray };
		int count;

	        if (before == null) System.out.println("before call");
		for (count=0; count<intArray.length; count++)
		{
			if (before == null)
				System.out.println("Java: Element " + count +
					"=" + intArray[count]);	
			else
				before.add(String.valueOf(intArray[count]));
		}

		try
		{
			int a=RuntimeSystem.cobcall("incints",myParms);
		}
		catch(Exception e)
		{
			System.err.println("increment(int[]): caught a unexpected exception ("
						+ e.getMessage() + ")" );
		}

	        if (after == null) System.out.println("after call");
		for (count=0; count<intArray.length; count++)
		{
			if (before == null)
				System.out.println("Java: Element " + count +
					"=" + intArray[count]);	
			else
				after.add(String.valueOf(intArray[count]));
		}

	}


	void increment(List before, List after, boolean booleanArray[])
	{
		Object myParms[] = { booleanArray };
		int count;

	        if (before == null) System.out.println("before call");
		for (count=0; count<booleanArray.length; count++)
		{
			if (before == null)
				System.out.println("Java: Element " + count +
					"=" + booleanArray[count]);	
			else
				before.add(String.valueOf(booleanArray[count]));
		}

		try
		{
			int a=RuntimeSystem.cobcall("incbools",myParms);
		}
		catch(Exception e)
		{
			System.err.println("increment(boolean[]): caught a unexpected exception ("
						+ e.getMessage() + ")" );
		}

	        if (after == null) System.out.println("after call");
		for (count=0; count<booleanArray.length; count++)
		{
			if (before == null)
				System.out.println("Java: Element " + count +
					"=" + booleanArray[count]);	
			else
				after.add(String.valueOf(booleanArray[count]));
		}

	}
	
	void increment(List before, List after, float floatArray[])
	{
		Object myParms[] = { floatArray };
		int count;

	        if (before == null) System.out.println("before call");
		for (count=0; count<floatArray.length; count++)
		{
			if (before == null)
				System.out.println("Java: Element " + count +
					"=" + floatArray[count]);	
			else
				before.add(String.valueOf(floatArray[count]));
		}

		try
		{
			int a=RuntimeSystem.cobcall("incfloats",myParms);
		}
		catch(Exception e)
		{
			System.err.println("increment(float[]): caught a unexpected exception ("
						+ e.getMessage() + ")" );
		}

	        if (after == null) System.out.println("after call");
		for (count=0; count<floatArray.length; count++)
		{
			if (before == null)
				System.out.println("Java: Element " + count +
					"=" + floatArray[count]);	
			else
				after.add(String.valueOf(floatArray[count]));
		}

	}
	
	void increment(List before, List after, double doubleArray[])
	{
		Object myParms[] = { doubleArray };
		int count;

	        if (before == null) System.out.println("before call");
		for (count=0; count<doubleArray.length; count++)
		{
			if (before == null)
				System.out.println("Java: Element " + count +
					"=" + doubleArray[count]);	
			else
				before.add(String.valueOf(doubleArray[count]));
		}

		try
		{
			int a=RuntimeSystem.cobcall("incdobs",myParms);
		}
		catch(Exception e)
		{
			System.err.println("increment(double[]): caught a unexpected exception ("
						+ e.getMessage() + ")" );
		}

	        if (after == null) System.out.println("after call");
		for (count=0; count<doubleArray.length; count++)
		{
			if (before == null)
				System.out.println("Java: Element " + count +
					"=" + doubleArray[count]);	
			else
				after.add(String.valueOf(doubleArray[count]));
		}

	}	


	void increment(List before, List after, short shortArray[])
	{
		Object myParms[] = { shortArray };
		int count;

	        if (before == null) System.out.println("before call");
		for (count=0; count<shortArray.length; count++)
		{
			if (before == null)
				System.out.println("Java: Element " + count +
					"=" + shortArray[count]);	
			else
				before.add(String.valueOf(shortArray[count]));
		}

		try
		{
			int a=RuntimeSystem.cobcall("incshort",myParms);
		}
		catch(Exception e)
		{
			System.err.println("increment(double[]): caught a unexpected exception ("
						+ e.getMessage() + ")" );
		}

	        if (after == null) System.out.println("after call");
		for (count=0; count<shortArray.length; count++)
		{
			if (before == null)
				System.out.println("Java: Element " + count +
					"=" + shortArray[count]);	
			else
				after.add(String.valueOf(shortArray[count]));
		}
	}
	
	void increment(List before, List after, long longArray[])
	{
		Object myParms[] = { longArray };
		int count;

	        if (before == null) System.out.println("before call");
		for (count=0; count<longArray.length; count++)
		{
			if (before == null)
				System.out.println("Java: Element " + count +
					"=" + longArray[count]);	
			else
				before.add(String.valueOf(longArray[count]));
		}

		try
		{
			int a=RuntimeSystem.cobcall("inclong",myParms);
		}
		catch(Exception e)
		{
			System.err.println("increment(double[]): caught a unexpected exception ("
						+ e.getMessage() + ")" );
		}

	        if (after == null) System.out.println("after call");
		for (count=0; count<longArray.length; count++)
		{
			if (before == null)
				System.out.println("Java: Element " + count +
					"=" + longArray[count]);	
			else
				after.add(String.valueOf(longArray[count]));
		}
	}	
	
	void increment(List before, List after, byte byteArray[])
	{
		Object myParms[] = { byteArray };
		int count;
		String ourString = new String(byteArray);

	        if (before == null) System.out.println("before call");
		for (count=0; count<ourString.length(); count++)
		{
			if (before == null)
				System.out.println("Java: Element " + count +
					"=" + ourString.substring(count,count+1));	
			else
				before.add(ourString.substring(count,count+1));
		}

		try
		{
			int a=RuntimeSystem.cobcall("revbytes",myParms);
		}
		catch(Exception e)
		{
			System.err.println("increment(double[]): caught a unexpected exception ("
						+ e.getMessage() + ")" );
		}

	        if (after == null) System.out.println("after call");
		ourString = new String(byteArray);
		for (count=0; count<ourString.length(); count++)
		{
			if (before == null)
				System.out.println("Java: Element " + count +
					"=" + ourString.substring(count,count+1));	
			else
				after.add(ourString.substring(count,count+1));
		}	
	}

	void increment(List before, List after, char charArray[])
	{
		Object myParms[] = { charArray };
		int count;
		String ourString = new String(charArray);

	        if (before == null) System.out.println("before call");
		for (count=0; count<ourString.length(); count++)
		{
			if (before == null)
				System.out.println("Java: Element " + count +
					"=" + ourString.substring(count,count+1));	
			else
				before.add(ourString.substring(count,count+1));
		}

		try
		{
			int a=RuntimeSystem.cobcall("revchars",myParms);
		}
		catch(Exception e)
		{
			System.err.println("increment(double[]): caught a unexpected exception ("
						+ e.getMessage() + ")" );
		}

	        if (after == null) System.out.println("after call");
		ourString = new String(charArray);
		for (count=0; count<ourString.length(); count++)
		{
			if (before == null)
				System.out.println("Java: Element " + count +
					"=" + ourString.substring(count,count+1));	
			else
				after.add(ourString.substring(count,count+1));
		}	
	}
	
	public static void main(String argv[]) throws Exception
	{
		int count;

		int intArray[] = { 10,9,8,7,6,5,4,3,2,1 };
		boolean booleanArray[] = { true,false,true };
		float floatArray[] = { (float)0.1,(float)1.1,(float)2.2 };
		double doubleArray[] = { (double)0.1,(double)1.1,(double)2.2 };
		short shortArray[] = { (short)300,(short)400,(short)500 };
		long longArray[] = { (long)1000,(long)1100,(long)1200 };
		byte byteArray[] = { (byte)'M',(byte)'F'};
		char charArray[] = { (char)'M',(char)'i',(char)'c',(char)'r',(char)'o',
				     (char)'F',(char)'o',(char)'c',(char)'u',(char)'s'};

		CobolArrays ourDemo = new CobolArrays();

		System.out.println("Passing a int[] to COBOL Example");
		ourDemo.increment(null,null,intArray);
		System.out.println();

		System.out.println("Passing a boolean[] to COBOL Example");
		ourDemo.increment(null,null,booleanArray);
		System.out.println();

		System.out.println("Passing a float[] to COBOL Example");
		ourDemo.increment(null,null,floatArray);
		System.out.println();

		System.out.println("Passing a double[] to COBOL Example");
		ourDemo.increment(null,null,doubleArray);
		System.out.println();

		System.out.println("Passing a short[] to COBOL Example");
		ourDemo.increment(null,null,shortArray);
		System.out.println();

		System.out.println("Passing a long[] to COBOL Example");
		ourDemo.increment(null,null,longArray);
		System.out.println();

		System.out.println("Passing a byte[] to COBOL Example");
		ourDemo.increment(null,null,byteArray);
		System.out.println();

		System.out.println("Passing a char[] to COBOL Example");
		ourDemo.increment(null,null,charArray);
		System.out.println();

	}

	static 
	{
		RuntimeSystem.cobload("cobolarray");
	}

}


