001 /* 002 * 003 * $Id: Coerce.java,v 1.3 2003/09/23 14:56:15 gus Exp $ 004 * 005 * Developed for "Rethinking CS101", a project of Lynn Andrea Stein's AP Group. 006 * For more information, see <a href="http://www.ai.mit.edu/projects/cs101/">the 007 * CS101 homepage</a> or email <las@ai.mit.edu>. 008 * 009 * Copyright (C) 1996 Massachusetts Institute of Technology. 010 * Please do not redistribute without obtaining permission. 011 */ 012 013 package cs101.util; 014 015 import java.util.Hashtable; 016 import java.awt.Color; 017 018 /** 019 * Coerce implements some coercion utilities for a variety of java 020 * classes. <p> 021 * 022 * This file was created for 023 * <a href="http://www.ai.mit.edu/projects/cs101/"> 024 * Rethinking CS101 025 * </a> project of Lynn Andrea Stein's AP Group at the MIT 026 * Artificial Intelligence Laboratory.<p> 027 * <p> 028 * Copyright 1996 Massachusetts Institute of Technology 029 * 030 * @see java.lang.String 031 * @see java.lang.Double 032 * @see java.awt.Color 033 * 034 * @author Lynn Andrea Stein, las@ai.mit.edu 035 * @version $Id: Coerce.java,v 1.3 2003/09/23 14:56:15 gus Exp $ 036 * 037 */ 038 public final class Coerce { 039 040 // StringTodouble( String ) 041 /** 042 * Given a string representing a floating point number, returns the 043 * corresponding (unwrapped) double. 044 * 045 * Note: this is <i>not</i> Double StringToDouble( String ), which 046 * would be just Double.valueOf( s ); 047 * 048 * @param s A string representing a floating point number. 049 * @return the corresponding (unwrapped) double. 050 */ 051 public static final double stringToDouble( String s ) { 052 return Double.valueOf( s ).doubleValue(); 053 } 054 055 056 private static final Hashtable colorNameTable = new Hashtable(13); 057 // 13 is initial capacity 058 static { 059 colorNameTable.put("Black", java.awt.Color.black); 060 colorNameTable.put("Blue", java.awt.Color.blue); 061 colorNameTable.put("Cyan", java.awt.Color.cyan); 062 colorNameTable.put("DarkGray", java.awt.Color.darkGray); 063 colorNameTable.put("Gray", java.awt.Color.gray); 064 colorNameTable.put("Green", java.awt.Color.green); 065 colorNameTable.put("LightGray", java.awt.Color.lightGray); 066 colorNameTable.put("Magenta", java.awt.Color.magenta); 067 colorNameTable.put("Orange", java.awt.Color.orange); 068 colorNameTable.put("Pink", java.awt.Color.pink); 069 colorNameTable.put("Red", java.awt.Color.red); 070 colorNameTable.put("White", java.awt.Color.white); 071 colorNameTable.put("Yellow", java.awt.Color.yellow); 072 } 073 074 // StringToColor( String ) 075 /** 076 * Given a string representing the name of one of java's built-in 077 * colors, returns the corresponding Color object. 078 * 079 * @param colorName A string representing a java Color. 080 * @return the corresponding Color object. 081 */ 082 public static final Color stringToColor ( String colorName ) { 083 Object c = Coerce.colorNameTable.get(colorName); 084 if ( c == null ) { 085 return Color.black; 086 } else { 087 return (Color) c; 088 } 089 } 090 091 // NewInstanceByClassname( String ) 092 /** 093 * Given a String representing the name of a class, returns an 094 * initilized instance of the corresponding class (as an object). 095 * Throws CreationException if the indicated class cannot be 096 * instantianted for any reason. 097 */ 098 public static final Object newInstanceByClassname( String s ) throws CreationException { 099 try { 100 Class c = Class.forName(s); 101 return c.newInstance(); 102 } catch (Exception e) { 103 throw new CreationException(s + " could not be created"); 104 } 105 } 106 107 /** 108 * Prevent instantiation 109 */ 110 private Coerce() {} 111 } 112 /* Comments: 113 * 114 * History: 115 * $Log: Coerce.java,v $ 116 * Revision 1.3 2003/09/23 14:56:15 gus 117 * javadoc fix and make stringToColor do proper typechecking. 118 * 119 * Revision 1.2 2003/03/28 18:45:36 gus 120 * rename methods to have consistant captializations scheme. 121 * 122 * Revision 1.1.1.1 2002/06/05 21:56:32 root 123 * CS101 comes to Olin finally. 124 * 125 * Revision 1.5 1998/07/24 17:19:23 tparnell 126 * Placate new javadoc behavior 127 * 128 * Revision 1.4 1998/07/21 19:23:21 tparnell 129 * added private Coerce() 130 * 131 * Revision 1.3 1998/06/07 16:59:52 tparnell 132 * changed name to NewInstanceByClassname 133 * 134 * Revision 1.2 1998/06/04 23:18:12 tparnell 135 * added a StringToObject method in Coerce, and a generic Main wrapper so 136 * students can avoid public static void main(String[] argv) 137 * 138 * Revision 1.1 1998/03/13 22:18:09 tparnell 139 * Import from server crash. I think the src and class files match up. 140 * 141 * Revision 1.3 1996/08/01 18:26:18 reuben 142 * More javadoc tweaking (hopefully the final pass) 143 * 144 * Revision 1.2 1996/07/30 17:25:59 reuben 145 * Added/corrected javadoc comments. 146 * 147 * Revision 1.1.1.1 1996/07/18 17:38:24 sit 148 * Import from /mit/6.096/share/classes after 6.80s session 149 * 150 * Revision 1.2 1996/07/02 21:47:51 las 151 * Added Color (String name to String) coercions. 152 * 153 * Revision 1.1 1996/06/25 22:23:36 las 154 * Initial revision 155 * 156 * */