Coverage Report - us.daveread.utility.formatcheck.format.FieldType
 
Classes in this File Line Coverage Branch Coverage Complexity
FieldType
89%
48/54
75%
6/8
1.1
 
 1  
 package us.daveread.utility.formatcheck.format;
 2  
 
 3  
 import org.apache.log4j.Logger;
 4  
 
 5  
 /**
 6  
  * <p>Title: FieldType
 7  
  * <p>Description: Represents a data field
 8  
  * <p>Copyright: Copyright (c) 2005
 9  
  * <p>This program is free software; you can redistribute it and/or modify
 10  
  * it under the terms of the GNU General Public License as published by
 11  
  * the Free Software Foundation; either version 2 of the License, or
 12  
  * (at your option) any later version.
 13  
  * <p>This program is distributed in the hope that it will be useful,
 14  
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 15  
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 16  
  * GNU General Public License for more details.
 17  
  * <p>You should have received a copy of the GNU General Public License
 18  
  * along with this program; if not, write to the Free Software
 19  
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 20  
  * </p>
 21  
  *
 22  
  * @author David Read
 23  
  * @version $Id: FieldType.java,v 1.2 2006/06/14 23:31:50 daveread Exp $
 24  
  */
 25  
 public class FieldType {
 26  
 
 27  
     /** The field does not require any alignment */
 28  
     public final static int ALIGN_NONE = 0;
 29  
 
 30  
     /** The field requires the contents to be left-aligned */
 31  
     public final static int ALIGN_LEFT = 1;
 32  
 
 33  
     /** The field requires the contents to be right-aligned */
 34  
     public final static int ALIGN_RIGHT = 2;
 35  
 
 36  
     /** The display name of the field */
 37  
     private String name;
 38  
 
 39  
     /** The alignment required for the field */
 40  
     private int alignment;
 41  
 
 42  
     /** The start position of the field within a record  - required if fixed width */
 43  
     private int start;
 44  
 
 45  
     /** The width of the field -- required if fixed width */
 46  
     private int width;
 47  
 
 48  
     /** Whether the field must contain data */
 49  
     private String required;
 50  
 
 51  
     /** What group the field is part of (used for requirement of "Or") */
 52  
     private Integer group;
 53  
 
 54  
     /** Variable name to store field value */
 55  
     private String variableNameStoreValue;
 56  
 
 57  
     /** Variable name containing data to match value to */
 58  
     private String variableNameMatchValue;
 59  
 
 60  
     /** Logger */
 61  6
     private static final Logger logger = Logger.getLogger(FieldType.class);
 62  
 
 63  
     /**
 64  
      * Whether matching the assigned value of this field will "lock" this record
 65  
      * type as the one that must be used to compare the remainder of the data row
 66  
      */
 67  
     private boolean matchLock;
 68  
 
 69  
     /** The data type of this field */
 70  
     private DataType dataType;
 71  
 
 72  
     /**
 73  
      * Creates a field consisting of only a display name.  All other attributes
 74  
      * are defaulted to values indicating they are not used.  This is likely to
 75  
      * be used only for CSV files.
 76  
      *
 77  
      * @param aName String contains the display name of the field
 78  
      */
 79  
     public FieldType(String aName) {
 80  25
         this(aName, -1, -1, ALIGN_NONE);
 81  25
     }
 82  
 
 83  
     /**
 84  
      * Creates a field consisting of a display name and width.  All other attributes
 85  
      * are defaulted to values indicating they are not used. This is likely to
 86  
      * be used only for CSV files.
 87  
      *
 88  
      * @param aName String contains the display name of the field
 89  
      * @param aWidth int contains the fixed width of the field
 90  
      */
 91  
     public FieldType(String aName, int aWidth) {
 92  60
         this(aName, -1, aWidth, ALIGN_NONE);
 93  60
     }
 94  
 
 95  
     /**
 96  
      * Creates a field consisting of a display name, start position, and width.
 97  
      * All other attributes are defaulted to values indicating they are not used.
 98  
      *
 99  
      * @param aName String contains the display name of the field
 100  
      * @param aStart int contains the start position of the field within its record
 101  
      * @param aWidth int contains the fixed width of the field
 102  
      */
 103  
     public FieldType(String aName, int aStart, int aWidth) {
 104  11
         this(aName, aStart, aWidth, ALIGN_NONE);
 105  11
     }
 106  
 
 107  
     /**
 108  
      * Creates a field consisting of a display name, start position, width, and
 109  
      * alignment.
 110  
      *
 111  
      * @param aName String contains the display name of the field
 112  
      * @param aStart int contains the start position of the field within its record
 113  
      * @param aWidth int contains the fixed width of the field
 114  
      * @param aAlignment int contains the alignment type code
 115  
      */
 116  107
     public FieldType(String aName, int aStart, int aWidth, int aAlignment) {
 117  107
         setAlignment(aAlignment);
 118  107
         setName(aName);
 119  107
         setStart(aStart);
 120  107
         setWidth(aWidth);
 121  107
     }
 122  
 
 123  
     /**
 124  
      * Validates the data value against the field, and its data type, requirements.
 125  
      *
 126  
      * @param fieldValue String The data value being tested for validity
 127  
      * @return String The explanation of the error or null if the data is valid
 128  
      */
 129  
     public String validate(String aFieldValue) {
 130  
         String result;
 131  
 
 132  31
         result = getDataType().validate(aFieldValue);
 133  
 
 134  31
         if (result != null) {
 135  8
             result = getName() + " " + result + " [Value Found:" + aFieldValue +
 136  
                      "]";
 137  
         }
 138  
 
 139  31
         return result;
 140  
     }
 141  
 
 142  
     /**
 143  
      * Sets the data type for this field.
 144  
      *
 145  
      * @param aDataType DataType The data type for this field
 146  
      */
 147  
     public void setDataType(DataType aDataType) {
 148  64
         dataType = aDataType;
 149  64
     }
 150  
 
 151  
     /**
 152  
      * Gets the data type associated with this field.
 153  
      *
 154  
      * @return DataType The data type assigned to this field
 155  
      */
 156  
     public DataType getDataType() {
 157  37
         return dataType;
 158  
     }
 159  
 
 160  
     /**
 161  
      * Sets the alignment code for this field.
 162  
      *
 163  
      * @param aAlignment int The alignment code for this field
 164  
      */
 165  
     private void setAlignment(int aAlignment) {
 166  107
         alignment = aAlignment;
 167  107
     }
 168  
 
 169  
     /**
 170  
      * Gets the alignment code for this field.
 171  
      *
 172  
      * @return int The alignment code for this field
 173  
      */
 174  
     public int getAlignment() {
 175  4
         return alignment;
 176  
     }
 177  
 
 178  
     /**
 179  
      * Sets the display name for the field.
 180  
      *
 181  
      * @param aName String The display name for this field
 182  
      */
 183  
     private void setName(String aName) {
 184  107
         name = aName;
 185  107
     }
 186  
 
 187  
     /**
 188  
      * Gets the display name for the field.
 189  
      *
 190  
      * @return String The display name for this field
 191  
      */
 192  
     public String getName() {
 193  16
         return name;
 194  
     }
 195  
 
 196  
     /**
 197  
      * Sets the start position of the field within its record.
 198  
      *
 199  
      * @param aStart int The start position of the field in a record
 200  
      */
 201  
     private void setStart(int aStart) {
 202  107
         start = aStart;
 203  107
     }
 204  
 
 205  
     /**
 206  
      * Gets the start position of the field within its record.
 207  
      *
 208  
      * @return int The start position of the field in a record
 209  
      */
 210  
     public int getStart() {
 211  41
         return start;
 212  
     }
 213  
 
 214  
     /**
 215  
      * Sets the width of the field.
 216  
      *
 217  
      * @param aWidth int The width of the field
 218  
      */
 219  
     private void setWidth(int aWidth) {
 220  107
         width = aWidth;
 221  107
     }
 222  
 
 223  
     /**
 224  
      * Gets the width of the field.
 225  
      *
 226  
      * @return int The width of the field
 227  
      */
 228  
     public int getWidth() {
 229  189
         return width;
 230  
     }
 231  
 
 232  
     /**
 233  
      * Sets whether the field has to contain data.  The values allowed are "Yes",
 234  
      * "No", "Or".  The value "Or" relies on a group number to determine the
 235  
      * set of fields, one of which is required.
 236  
      *
 237  
      * @see setGroup
 238  
      *
 239  
      * @param aRequired boolean True if the field must contain data
 240  
      */
 241  
     public void setRequired(String aRequired) {
 242  1
         required = aRequired;
 243  1
     }
 244  
 
 245  
     /**
 246  
      * Gets whether the field has to contain data.
 247  
      *
 248  
      * @return boolean True if the field must contain data
 249  
      */
 250  
     public boolean isRequired() {
 251  2
         return required != null && required.equals("Yes");
 252  
     }
 253  
 
 254  
     /**
 255  
      * Gets whether the field has to contain data unless another field in its
 256  
      * group has data, in which case it may optionally contain data.
 257  
      *
 258  
      * @return boolean True if the field may have to contain data based on
 259  
      *     its group
 260  
      */
 261  
     public boolean isOr() {
 262  31
         return required != null && required.equals("Or");
 263  
     }
 264  
 
 265  
     /**
 266  
      * Gets whether the field has to contain data unless another field in its
 267  
      * group has data, in which case it cannot contain data.
 268  
      *
 269  
      * @return boolean True if the field may have to contain data based on
 270  
      *     its group
 271  
      */
 272  
     public boolean isXOr() {
 273  31
         return required != null && required.equals("XOr");
 274  
     }
 275  
 
 276  
     /**
 277  
      * Gets whether the field may be left empty.
 278  
      *
 279  
      * @return boolean True if the field doews not require data
 280  
      */
 281  
     public boolean isOptional() {
 282  0
         return required == null || required.equals("No");
 283  
     }
 284  
 
 285  
     /**
 286  
      * Sets whether this field locks the record type for the data row.
 287  
      *
 288  
      * @param aMatchLock boolean True if a match on this field locks the record type
 289  
      */
 290  
     public void setMatchLock(boolean aMatchLock) {
 291  15
         matchLock = aMatchLock;
 292  15
     }
 293  
 
 294  
     /**
 295  
      * Sets the variable name where the data found in this field is to be stored
 296  
      * for comparison to another field in a later record.
 297  
      *
 298  
      * @param aVariableName String The name of the variable to hold the field content
 299  
      */
 300  
     public void setVariableNameStoreValue(String aVariableName) {
 301  1
         variableNameStoreValue = aVariableName;
 302  1
     }
 303  
 
 304  
     /**
 305  
      * Gets the variable name where the data found in this field is to be stored
 306  
      * for comparison to another field in a later record.
 307  
      *
 308  
      * @return String The name of the variable to hold the field content
 309  
      */
 310  
     public String getVariableNameStoreValue() {
 311  2
         return variableNameStoreValue;
 312  
     }
 313  
 
 314  
     /**
 315  
      * Sets the variable name containing a value that this field's value must
 316  
      * match.
 317  
      *
 318  
      * @param aVariableName String The name of the variable with the matching field content
 319  
      */
 320  
     public void setVariableNameMatchValue(String aVariableName) {
 321  1
         variableNameMatchValue = aVariableName;
 322  1
     }
 323  
 
 324  
     /**
 325  
      * Gets the variable name containing a value that this field's value must
 326  
      * match.
 327  
      *
 328  
      * @return String The name of the variable with the matching field content
 329  
      */
 330  
     public String getVariableNameMatchValue() {
 331  2
         return variableNameMatchValue;
 332  
     }
 333  
 
 334  
     /**
 335  
      * Returns whether or not this field's value is to be stored in a variable.
 336  
      *
 337  
      * @return boolean True if the value is to be stored
 338  
      */
 339  
     public boolean isToBeStored() {
 340  33
         return variableNameStoreValue != null;
 341  
     }
 342  
 
 343  
     /**
 344  
      * Returns whether or not this field's value is to be matched to a value
 345  
      * in a variable.
 346  
      *
 347  
      * @return boolean True if the field's value is to match the value in a variable
 348  
      */
 349  
     public boolean isToBeMatched() {
 350  33
         return variableNameMatchValue != null;
 351  
     }
 352  
 
 353  
     /**
 354  
      * Gets whether this field locks the record type for the data row.
 355  
      *
 356  
      * @return boolean True if a match on this field locks the record type
 357  
      */
 358  
     public boolean isMatchLock() {
 359  25
         return matchLock;
 360  
     }
 361  
 
 362  
     /**
 363  
      * Sets the group for this field -- used when setting the requirement to "Or".
 364  
      *
 365  
      * @param aGroup int The group number of this field
 366  
      */
 367  
     public void setGroup(int aGroup) {
 368  0
         group = new Integer(aGroup);
 369  0
     }
 370  
 
 371  
     /**
 372  
      * Gets the group for this field -- used when setting the requirement to "Or".
 373  
      *
 374  
      * @return int The group number of this field, or -1 if unset
 375  
      */
 376  
     public int getGroup() {
 377  0
         if (group != null) {
 378  0
             return group.intValue();
 379  
         } else {
 380  0
             return -1;
 381  
         }
 382  
     }
 383  
 }