package edu.calpoly.cpe205.fetter; import javax.swing.*; import java.util.*; import java.io.*; import java.lang.reflect.*; import java.lang.*; import java.awt.*; import javax.swing.event.*; import java.awt.event.*; /** * This class provides a user with a factory class that can create instances * of one of Java's primitive wrapper classes. */ // Author: Phillip Hansen // Version History: // Nov 18, 2000 - added psuedo code and description // Nov 30, 2000 - (Mike Hebron) added more detail to class description // Jan 17, 2001 - (Phillip Hansen) added stubbing and some code // Feb 10, 2001 - (Jonathon Lee) updated to coding standards // Feb 11, 2001 - (Jonathon Lee) removed stubbing public abstract class PrimitiveValueFactory { /** * returns an object containing the value *

* Pre-conditions: none
* Post-conditions: none * @param value string to be wrapped in the object type * @return Object specified by the factory */ public abstract Object getValue(String value) throws NumberFormatException; /** * returns a primitive value factory for the type specified by the type hashcode *

* Pre-condition: none
* Post-condition: none * @param typeHashcode hashcode specifying the type * @return PrimitiveValueFactory specified by the type hashcode */ public static PrimitiveValueFactory getAPrimitiveValueFactory(String typeName) { // CALL get of factoryMap with typeName // RETURN PrimitiveValueFactory which is returned from the map return (PrimitiveValueFactory)(factoryMap.get(typeName)); } /** * Returns an iterator for all the known PrimitiveValueFactories */ public static Iterator getPrimitiveValueFactories() { //CALL values on factoryMap returns col //CALL iterator on col returns itr //RETURN itr return factoryMap.values().iterator(); } /** * map of type hashcodes to factories * @link aggregation * @associates <{PrimitiveValueFactory}> */ protected final static java.util.Map factoryMap = new TreeMap(); // get hashcodes for all primitive types and use them as keys to // add the factories to them to factorymap static { factoryMap.put(Boolean.class.getName(), new BooleanFactory()); factoryMap.put(Byte.class.getName(), new ByteFactory()); factoryMap.put(Character.class.getName(), new CharFactory()); factoryMap.put(Double.class.getName(), new DoubleFactory()); factoryMap.put(Float.class.getName(), new FloatFactory()); factoryMap.put(Integer.class.getName(), new IntegerFactory()); factoryMap.put(Long.class.getName(), new LongFactory()); factoryMap.put(Short.class.getName(), new ShortFactory()); factoryMap.put(String.class.getName(), new StringFactory()); } /** * This class wraps primitive values as a Float object type. */ public static class FloatFactory extends PrimitiveValueFactory { /** * returns a Float object containing the value *

* Pre-conditions: none
* Post-conditions: none * @param value string to be wrapped in the Float object type * @return Float object */ public Object getValue(String value) throws NumberFormatException { // CONSTRUCT Float with value // RETURN constructed object try { return new Float(value); } catch(NumberFormatException exception) { if(exception.getMessage() == null) { throw new NumberFormatException(value); } throw exception; } } /** * returns the name of the factory type *

* Pre-conditions: none
* Post-conditions: none * @return String containing the name of the factory type */ public String toString() { // RETURN "float" return "float"; } } /** * This class wraps primitive values as a Byte object type. */ public static class ByteFactory extends PrimitiveValueFactory { /** * returns a Byte object containing the value *

* Pre-conditions: none
* Post-conditions: none * @param value string to be wrapped in the Byte object type * @return Byte object */ public Object getValue(String value) throws NumberFormatException { // CONSTRUCT Byte with value // RETURN constructed object try { return new Byte(value); } catch(NumberFormatException exception) { if(exception.getMessage() == null) { throw new NumberFormatException(value); } throw exception; } } /** * returns the name of the factory type *

* Pre-conditions: none
* Post-conditions: none * @return String containing the name of the factory type */ public String toString() { // RETURN "byte" return "byte"; } } /** * This class wraps primitive values as an Int object type. */ public static class IntegerFactory extends PrimitiveValueFactory { /** * returns an Integer object containing the value *

* Pre-conditions: none
* Post-conditions: none * @param value string to be wrapped in the Int object type * @return Int object */ public Object getValue(String value) throws NumberFormatException { // CONSTRUCT Int with value // RETURN constructed object try { return new Integer(value); } catch(NumberFormatException exception) { if(exception.getMessage() == null) { throw new NumberFormatException(value); } throw exception; } } /** * returns the name of the factory type *

* Pre-conditions: none
* Post-conditions: none * @return String containing the name of the factory type */ public String toString() { // RETURN "int" return "int"; } } /** * This class wraps primitive values as a String object type. */ public static class StringFactory extends PrimitiveValueFactory { /** * returns a String object containing the value *

* Pre-conditions: none
* Post-conditions: none * @param value string to be wrapped in the String object type * @return String object */ public Object getValue(String value) { // RETURN value return value; } /** * returns the name of the factory type *

* Pre-conditions: none
* Post-conditions: none * @return String containing the name of the factory type */ public String toString() { // RETURN "string" return "string"; } } /** * This class wraps primitive values as a Boolean object type. */ public static class BooleanFactory extends PrimitiveValueFactory { /** * returns a Boolean object containing the value *

* Pre-conditions: none
* Post-conditions: none * @param value string to be wrapped in the Boolean object type * @return Boolean object */ public Object getValue(String value) throws NumberFormatException { // CONSTRUCT Boolean with value // RETURN constructed object if(!value.equalsIgnoreCase("true") && !value.equalsIgnoreCase("false")) { throw new NumberFormatException(value); } return new Boolean(value); } /** * returns the name of the factory type *

* Pre-conditions: none
* Post-conditions: none * @return String containing the name of the factory type */ public String toString() { // RETURN "boolean" return "boolean"; } } /** * This class wraps primitive values as a Short object type. */ public static class ShortFactory extends PrimitiveValueFactory { /** * returns a Short object containing the value *

* Pre-conditions: none
* Post-conditions: none * @param value string to be wrapped in the Short object type * @return Short object */ public Object getValue(String value) throws NumberFormatException { // CONSTRUCT Short with value // RETURN constructed object try { return new Short(value); } catch(NumberFormatException exception) { if(exception.getMessage() == null) { throw new NumberFormatException(value); } throw exception; } } /** * returns the name of the factory type *

* Pre-conditions: none
* Post-conditions: none * @return String containing the name of the factory type */ public String toString() { // RETURN "short" return "short"; } } /** * This class wraps primitive values as a Long object type. */ public static class LongFactory extends PrimitiveValueFactory { /** * returns a Long object containing the value *

* Pre-conditions: none
* Post-conditions: none * @param value string to be wrapped in the Long object type * @return Long object */ public Object getValue(String value) throws NumberFormatException { // CONSTRUCT Long with value // RETURN constructed value try { return new Long(value); } catch(NumberFormatException exception) { if(exception.getMessage() == null) { throw new NumberFormatException(value); } throw exception; } } /** * returns the name of the factory type *

* Pre-conditions: none
* Post-conditions: none * @return String containing the name of the factory type */ public String toString() { // RETURN "long" return "long"; } } /** * This class wraps primitive values as a Char object type. */ public static class CharFactory extends PrimitiveValueFactory { /** * returns a Char object containing the value *

* Pre-conditions: none
* Post-conditions: none * @param value string to be wrapped in the Char object type * @return Char object */ public Object getValue(String value) throws NumberFormatException { // CONSTRUCT Char with value // RETURN constructed object if(value.length() != 1) { throw new NumberFormatException(value); } return new Character(value.charAt(0)); } /** * returns the name of the factory type *

* Pre-conditions: none
* Post-conditions: none * @return String containing the name of the factory type */ public String toString() { // RETURN "char" return "char"; } } /** * This class wraps primitive values as a Double object type. */ public static class DoubleFactory extends PrimitiveValueFactory { /** * returns a Double object containing the value *

* Pre-conditions: none
* Post-conditions: none * @param value string to be wrapped in the Double object type * @return Double object */ public Object getValue(String value) throws NumberFormatException { // CONSTRUCT Double with value // RETURN constructed object try { return new Double(value); } catch(NumberFormatException exception) { if(exception.getMessage() == null) { throw new NumberFormatException(value); } throw exception; } } /** * returns the name of the factory type *

* Pre-conditions: none
* Post-conditions: none * @return String containing the name of the factory type */ public String toString() { // RETURN "double" return "double"; } } }