/*******************************************************************************
*                                                                              *
*  File:        LuaJava_08.java                         Revision:  1.0         *
*                                                                              *
*  Purpose:     accompanying Java class for LuaJava_08.lua                     *
*                                                                              *
*  Creation:    21.11.2004                     Last Modification:  21.11.2004  *
*                                                                              *
*  Platform:    IBM-compatible PC running Windows 98SE                         *
*                                                                              *
*  Environment: Java 1.4                                                       *
*                                                                              *
*  Author:      Andreas Rozek           Phone:  ++49 (7031) 222305             *
*               Bunsenstraße 80/1       Fax:    -                              *
*             D-71032 Böblingen         EMail:  Info@Andreas-Rozek.De          *
*               Germany                                                        *
*                                                                              *
*  URL:         http://www.Andreas-Rozek.de/                                   *
*                                                                              *
*  Copyright:   this software is published under the  "GNU Lesser General Pub- *
*               lic  License"  (see  "http://www.fsf.org/copyleft/lesser.html" *
*               for additional information)                                    *
*                                                                              *
*  Comments:    (none)                                                         *
*                                                                              *
*******************************************************************************/

import java.io.*;                            // Java file handling and stream IO
import java.util.*;                  // Java utility classes and data structures

public class LuaJava_08 {

/*******************************************************************************
*                                                                              *
*                          Non-Public Class Variables                          *
*                                                                              *
*******************************************************************************/

  static Boolean   BooleanValue   = new Boolean(true);
  static Character CharacterValue = new Character('a');
  static Byte      ByteValue      = new Byte((byte) 123);
  static Short     ShortValue     = new Short((short) 12345);
  static Integer   IntegerValue   = new Integer(1234567);
  static Long      LongValue      = new Long(123456789);
  static Float     FloatValue     = new Float(123.456);
  static Double    DoubleValue    = new Double(123.456e78);
  static String    StringValue    = new String("(just a test)");
  static Hashtable HashtableValue = new Hashtable();     // will be filled later
  static File      FileValue      = new File("Test.dat");        // just a dummy

/*******************************************************************************
*                                                                              *
*                              Static Initializer                              *
*                                                                              *
*******************************************************************************/

  static {
    HashtableValue.put(BooleanValue,   BooleanValue);
    HashtableValue.put(CharacterValue, CharacterValue);
    HashtableValue.put(ByteValue,      ByteValue);
    HashtableValue.put(ShortValue,     ShortValue);
    HashtableValue.put(IntegerValue,   IntegerValue);
    HashtableValue.put(LongValue,      LongValue);
    HashtableValue.put(FloatValue,     FloatValue);
    HashtableValue.put(DoubleValue,    DoubleValue);
    HashtableValue.put(StringValue,    StringValue);
    HashtableValue.put(HashtableValue, HashtableValue);
    HashtableValue.put(FileValue,      FileValue);
  };

/*******************************************************************************
*                                                                              *
*                                 Constructors                                 *
*                                                                              *
*******************************************************************************/

  public LuaJava_08 () {
    super();                                              // just to be complete
  };

/*******************************************************************************
*                                                                              *
*                             Public Class Methods                             *
*                                                                              *
*******************************************************************************/

/*******************************************************************************
*                                                                              *
* fromJava                              returns one of several possibe objects *
*                                                                              *
*******************************************************************************/

  public static Object fromJava (double LuaIndex) {
    int Index = (int) Math.round(LuaIndex);
    switch (Index) {
      case 0:  return BooleanValue;
      case 1:  return CharacterValue;
      case 2:  return ByteValue;
      case 3:  return ShortValue;
      case 4:  return IntegerValue;
      case 5:  return LongValue;
      case 6:  return FloatValue;
      case 7:  return DoubleValue;
      case 8:  return StringValue;
      case 9:  return HashtableValue;
      case 10: return FileValue;
      default: return null;                                   // invalid "Index"
    }
  };

/*******************************************************************************
*                                                                              *
* toJava         receives an object from Lua and compares it with the original *
*                                                                              *
*******************************************************************************/

  public static String toJava (double LuaIndex, Object Candidate) {
    if (Candidate == null) return "  Java got nothing (null) from Lua";

    Object Original;

    int Index = (int) Math.round(LuaIndex);
    switch (Index) {
      case 0:  Original = BooleanValue;   break;
      case 1:  Original = CharacterValue; break;
      case 2:  Original = ByteValue;      break;
      case 3:  Original = ShortValue;     break;
      case 4:  Original = IntegerValue;   break;
      case 5:  Original = LongValue;      break;
      case 6:  Original = FloatValue;     break;
      case 7:  Original = DoubleValue;    break;
      case 8:  Original = StringValue;    break;
      case 9:  Original = HashtableValue; break;
      case 10: Original = FileValue;      break;
      default: return "  (invalid \"Index\")";
    };

    if (Candidate == Original) {
      return "  Java got " + Candidate.getClass().getName() + " from Lua " +
        "(identical with original object)";
    };

    if (Candidate.equals(Original)) {
      return "  Java got " + Candidate.getClass().getName() + " from Lua " +
        "(equal to original object)";
    };

    return "  Java got " + Candidate.getClass().getName() + " from Lua " +
      "not equal to original object";
  };
};

