/* * [Type.java] * * Summary: Enumeration of various data types for which we handle interconversions. * * Copyright: (c) 1997-2017 Roedy Green, Canadian Mind Products, http://mindprod.com * * Licence: This software may be copied and used freely for any purpose but military. * http://mindprod.com/contact/nonmil.html * * Requires: JDK 1.8+ * * Created with: JetBrains IntelliJ IDEA IDE http://www.jetbrains.com/idea/ * * Version History: * 5.3 2009-03-30 optimise suggested conversion code based on suggestions from Intellij Idea Inspector. * orrect table entries for unsigned conversions that could not be handled purely with autoboxing. */ package com.mindprod.converter; /** * Enumeration of various data types for which we handle interconversions. * * @author Roedy Green, Canadian Mind Products * @version 5.3 2009-03-30 optimise suggested conversion code based on suggestions from Intellij Idea Inspector. * correct table entries for unsigned conversions that could not be handled purely with autoboxing. * @since 1997 */ public enum Type { BOOLEAN( "boolean", "t", "true", "true" ), SIGNEDBYTE( " byte", "b", "1", "1" ), UNSIGNEDBYTE( "/*unsigned*/ byte", "u", "1", "1" ), SHORT( "short", "s", "1", "1" ), CHAR( "char", "c", "'1'", "'1'" ), INT( "int", "i", "1", "1" ), LONG( "long", "n", "1L", "1L" ), FLOAT( "float", "f", "1.0f", "1.0f" ), DOUBLE( "double", "d", "1.0d", "1.0d" ), STRING( "String", "g", "\"1\"", "\"1\"" ), WRAPPEDBOOLEAN( "Boolean", "tt", "Boolean.TRUE", "Boolean.TRUE" ), WRAPPEDSIGNEDBYTE( "/*signed*/ Byte", "bb", "new Byte((byte)1)", "1" ), WRAPPEDUNSIGNEDBYTE( "/*unsigned*/ Byte", "uu", "new Byte((byte)1)", "1" ), WRAPPEDSHORT( "Short", "ss", "new Short((short)1)", "1" ), WRAPPEDCHARACTER( "Character", "cc", "new Character('1')", "'1'" ), WRAPPEDINTEGER( "Integer", "ii", "new Integer(1)", "1" ), WRAPPEDLONG( "Long", "nn", "new Long(1L)", "1L" ), WRAPPEDFLOAT( "Float", "ff", "new Float(1.0f)", "1.0f" ), WRAPPEDDOUBLE( "Double", "dd", "new Double(1.0d)", "1.0d" ); /** * value to init this sort of variable */ private final String init14; /** * value to init this sort of variable */ private final String init15; /** * Java type name */ private final String name; /** * typical variable name */ private final String var; /** * constructor * * @param name Java type name * @param var typical variable name for this type * @param init14 value to init this type of variable in JDK 1.4 * @param init15 value to init this type of variable in JDK 1.5 */ Type( String name, String var, String init14, String init15 ) { this.name = name; this.var = var; this.init14 = init14; this.init15 = init15; } String getInit14() { return init14; } String getInit15() { return init15; } String getName() { return name; } String getVar() { return var; } /** * Generate a string with info on how to convert the given from/to type pair. * Used in generated HTML tables and in the Applet. * * @param from enumeration constant for the source type, for example, 0="boolean", 1="signed byte". * @param to enumeration constant for the target type, for example, 0="boolean", 1="signed byte". * @param considerAutobox true if want Java 1.5 autoboxing considered. false, just Java 1.4 methods. * * @return String sample Java source code to do that I/O task. */ public static String how( Type from, Type to, boolean considerAutobox ) { if ( from != to ) { return // generate comment: to String s from int i "// to " + to.name + " " + to.var + " from " + from.name + " " + from.var + "\n" + howCore( from, to, considerAutobox ); } else { return ( "// no conversion necessary" ); } } /** * Generate a string with info on how to convert the given from/to type pair. * * @param from enumeration constant for the source type, for example, 0="boolean", 1="signed byte". * @param to enumeration constant for the target type, for example, 0="boolean", 1="signed byte". * @param considerAutobox true if want Java 1.5 autoboxing considered. false, just Java 1.4 methods. * * @return String sample Java source code to do that I/O task. */ public static String howCore( Type from, Type to, boolean considerAutobox ) { if ( from != to ) { /* * build preamble, showing declares of two variables followed by how * to text. */ return considerAutobox ? Grid15.how15[ from.ordinal() ][ to.ordinal() ] : Grid14.how14[ from.ordinal() ][ to.ordinal() ]; } else { return ( "// no conversion necessary" ); } } public String toString() { return name; } }