/* * * $Id: Coerce.java,v 1.3 2003/09/23 14:56:15 gus Exp $ * * Developed for "Rethinking CS101", a project of Lynn Andrea Stein's AP Group. * For more information, see the * CS101 homepage or email . * * Copyright (C) 1996 Massachusetts Institute of Technology. * Please do not redistribute without obtaining permission. */ package cs101.util; import java.util.Hashtable; import java.awt.Color; /** * Coerce implements some coercion utilities for a variety of java * classes.

* * This file was created for * * Rethinking CS101 * project of Lynn Andrea Stein's AP Group at the MIT * Artificial Intelligence Laboratory.

*

* Copyright 1996 Massachusetts Institute of Technology * * @see java.lang.String * @see java.lang.Double * @see java.awt.Color * * @author Lynn Andrea Stein, las@ai.mit.edu * @version $Id: Coerce.java,v 1.3 2003/09/23 14:56:15 gus Exp $ * */ public final class Coerce { // StringTodouble( String ) /** * Given a string representing a floating point number, returns the * corresponding (unwrapped) double. * * Note: this is not Double StringToDouble( String ), which * would be just Double.valueOf( s ); * * @param s A string representing a floating point number. * @return the corresponding (unwrapped) double. */ public static final double stringToDouble( String s ) { return Double.valueOf( s ).doubleValue(); } private static final Hashtable colorNameTable = new Hashtable(13); // 13 is initial capacity static { colorNameTable.put("Black", java.awt.Color.black); colorNameTable.put("Blue", java.awt.Color.blue); colorNameTable.put("Cyan", java.awt.Color.cyan); colorNameTable.put("DarkGray", java.awt.Color.darkGray); colorNameTable.put("Gray", java.awt.Color.gray); colorNameTable.put("Green", java.awt.Color.green); colorNameTable.put("LightGray", java.awt.Color.lightGray); colorNameTable.put("Magenta", java.awt.Color.magenta); colorNameTable.put("Orange", java.awt.Color.orange); colorNameTable.put("Pink", java.awt.Color.pink); colorNameTable.put("Red", java.awt.Color.red); colorNameTable.put("White", java.awt.Color.white); colorNameTable.put("Yellow", java.awt.Color.yellow); } // StringToColor( String ) /** * Given a string representing the name of one of java's built-in * colors, returns the corresponding Color object. * * @param colorName A string representing a java Color. * @return the corresponding Color object. */ public static final Color stringToColor ( String colorName ) { Object c = Coerce.colorNameTable.get(colorName); if ( c == null ) { return Color.black; } else { return (Color) c; } } // NewInstanceByClassname( String ) /** * Given a String representing the name of a class, returns an * initilized instance of the corresponding class (as an object). * Throws CreationException if the indicated class cannot be * instantianted for any reason. */ public static final Object newInstanceByClassname( String s ) throws CreationException { try { Class c = Class.forName(s); return c.newInstance(); } catch (Exception e) { throw new CreationException(s + " could not be created"); } } /** * Prevent instantiation */ private Coerce() {} } /* Comments: * * History: * $Log: Coerce.java,v $ * Revision 1.3 2003/09/23 14:56:15 gus * javadoc fix and make stringToColor do proper typechecking. * * Revision 1.2 2003/03/28 18:45:36 gus * rename methods to have consistant captializations scheme. * * Revision 1.1.1.1 2002/06/05 21:56:32 root * CS101 comes to Olin finally. * * Revision 1.5 1998/07/24 17:19:23 tparnell * Placate new javadoc behavior * * Revision 1.4 1998/07/21 19:23:21 tparnell * added private Coerce() * * Revision 1.3 1998/06/07 16:59:52 tparnell * changed name to NewInstanceByClassname * * Revision 1.2 1998/06/04 23:18:12 tparnell * added a StringToObject method in Coerce, and a generic Main wrapper so * students can avoid public static void main(String[] argv) * * Revision 1.1 1998/03/13 22:18:09 tparnell * Import from server crash. I think the src and class files match up. * * Revision 1.3 1996/08/01 18:26:18 reuben * More javadoc tweaking (hopefully the final pass) * * Revision 1.2 1996/07/30 17:25:59 reuben * Added/corrected javadoc comments. * * Revision 1.1.1.1 1996/07/18 17:38:24 sit * Import from /mit/6.096/share/classes after 6.80s session * * Revision 1.2 1996/07/02 21:47:51 las * Added Color (String name to String) coercions. * * Revision 1.1 1996/06/25 22:23:36 las * Initial revision * * */