| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| DataTypeFactory |
|
| 2.6;2.6 |
| 1 | package us.daveread.utility.formatcheck.format; | |
| 2 | ||
| 3 | import org.apache.log4j.Logger; | |
| 4 | ||
| 5 | /** | |
| 6 | * <p>Title: DataTypeFactory | |
| 7 | * <p>Description: Create an instance of the correct data type based on the | |
| 8 | * requested data type name. | |
| 9 | * <p>Copyright: Copyright (c) 2005 | |
| 10 | * <p>This program is free software; you can redistribute it and/or modify | |
| 11 | * it under the terms of the GNU General Public License as published by | |
| 12 | * the Free Software Foundation; either version 2 of the License, or | |
| 13 | * (at your option) any later version. | |
| 14 | * <p>This program is distributed in the hope that it will be useful, | |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 17 | * GNU General Public License for more details. | |
| 18 | * <p>You should have received a copy of the GNU General Public License | |
| 19 | * along with this program; if not, write to the Free Software | |
| 20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
| 21 | * </p> | |
| 22 | * | |
| 23 | * @author David Read | |
| 24 | * @version $Id: DataTypeFactory.java,v 1.3 2006/06/14 23:33:12 daveread Exp $ | |
| 25 | */ | |
| 26 | public class DataTypeFactory { | |
| 27 | /** Logger */ | |
| 28 | 0 | private static final Logger logger = Logger.getLogger(DataTypeFactory.class); |
| 29 | ||
| 30 | /** | |
| 31 | * No need to allow instances to be created. | |
| 32 | */ | |
| 33 | 0 | private DataTypeFactory() { |
| 34 | 0 | } |
| 35 | ||
| 36 | /** | |
| 37 | * Obtain a data type, supplying only a data type name. If the data type | |
| 38 | * cannot be found, or if it requires a formatDefinition that | |
| 39 | * is missing, an IllegalArgumentException will be thrown. | |
| 40 | * | |
| 41 | * @param aDataType String The data type name | |
| 42 | * @return DataType The resulting datatype | |
| 43 | */ | |
| 44 | public final static DataType makeDataType(String aDataType) { | |
| 45 | 0 | return makeDataType(aDataType, null, null); |
| 46 | } | |
| 47 | ||
| 48 | /** | |
| 49 | * Obtain a data type, supplying data type name and associated format | |
| 50 | * definition. If the data type cannot be found, or if it requires a | |
| 51 | * formatDefinition that is missing, an IllegalArgumentException will be thrown. | |
| 52 | * | |
| 53 | * @param aDataType String The data type name | |
| 54 | * @param aFormatDefinition The format definition for the resulting data type | |
| 55 | * @return DataType The resulting datatype | |
| 56 | */ | |
| 57 | public final static DataType makeDataType(String aDataType, | |
| 58 | String aFormatDefinition) { | |
| 59 | 0 | return makeDataType(aDataType, aFormatDefinition, null); |
| 60 | } | |
| 61 | ||
| 62 | /** | |
| 63 | * Obtain a data type, supplying data type name, associated format | |
| 64 | * definition, and a display description for the data type. If the data | |
| 65 | * type cannot be found, or if it requires a formatDefinition that | |
| 66 | * is missing, an IllegalArgumentException will be thrown. | |
| 67 | * | |
| 68 | * @param aDataType String The data type name | |
| 69 | * @param aFormatDefinition The format definition for the resulting data type | |
| 70 | * @param aDescription The display description for the data type | |
| 71 | * @return DataType The resulting datatype | |
| 72 | */ | |
| 73 | public final static DataType makeDataType(String aDataType, | |
| 74 | String aFormatDefinition, | |
| 75 | String aDescription) { | |
| 76 | DataType theDataType; | |
| 77 | int whichType; | |
| 78 | 0 | String knownTypes[] = { |
| 79 | "REGEX", "DATE", "EMAIL"}; | |
| 80 | ||
| 81 | 0 | for (whichType = 0; whichType < knownTypes.length; ++whichType) { |
| 82 | 0 | if (aDataType.toUpperCase().equals(knownTypes[whichType])) { |
| 83 | 0 | break; |
| 84 | } | |
| 85 | } | |
| 86 | ||
| 87 | 0 | logger.info("Create datatype instance:aDataType[" + |
| 88 | aDataType + "] whichType[" + whichType + "]"); | |
| 89 | ||
| 90 | 0 | switch (whichType) { |
| 91 | case 0: // RegEx | |
| 92 | 0 | checkRequired(aFormatDefinition, "REGEX", "Regular Expression"); |
| 93 | 0 | theDataType = new RegExDataType(aFormatDefinition, aDescription); |
| 94 | 0 | break; |
| 95 | case 1: // Date | |
| 96 | 0 | checkRequired(aFormatDefinition, "DATE", "Date Format Pattern"); |
| 97 | 0 | theDataType = new DateDataType(aFormatDefinition, aDescription); |
| 98 | 0 | break; |
| 99 | case 2: // Email | |
| 100 | 0 | theDataType = new EmailDataType(aDescription); |
| 101 | 0 | break; |
| 102 | default: | |
| 103 | 0 | throw new IllegalArgumentException("Unknown data type: " + |
| 104 | aDataType); | |
| 105 | } | |
| 106 | ||
| 107 | 0 | return theDataType; |
| 108 | } | |
| 109 | ||
| 110 | /** | |
| 111 | * Check that a required value was supplied. If the value is empty | |
| 112 | * the method will throw an IllegalArgumentException. | |
| 113 | * | |
| 114 | * @param aValue String The value supplied | |
| 115 | * @param aType String The data type name | |
| 116 | * @param aWhat String The definition of the value | |
| 117 | */ | |
| 118 | private final static void checkRequired(String aValue, String aType, | |
| 119 | String aWhat) { | |
| 120 | 0 | if (aValue == null || aValue.trim().length() == 0) { |
| 121 | 0 | throw new IllegalArgumentException("Data type " + aType + |
| 122 | " requires a " + | |
| 123 | aWhat); | |
| 124 | } | |
| 125 | 0 | } |
| 126 | } |