com.billpringle.utils.wrputils
Class WrpFixedCols

java.lang.Object
  extended by com.billpringle.utils.wrputils.WrpFixedCols

public class WrpFixedCols
extends java.lang.Object

This class encapsulates a record using fixed length fields. The WrpCSV class handles CSV formatted fields, while this class handles files with fixed length columns.

Creative Commons License Creative Commons License Symbols Unless noted otherwise, all materials available for download from my site are copyrighted by Bill Pringle, and are licensed under a Creative Commons License.

Author:
Bill Pringle

Field Summary
static int ALIGN_CENTER
          center alignment flag
static int ALIGN_LEFT
          left alignment flag
static int ALIGN_RIGHT
          right alignment flag
private  int alignmentFlag
          alignment flag
private  char padChar
          padding character
private  java.lang.StringBuffer sbuf
          string buffer containing record data
 
Constructor Summary
WrpFixedCols(int recsize)
          Create empty fixed record of a given size.
WrpFixedCols(int recsize, int alignFlg, char padchar)
          Create new record and set alignment and padding.
WrpFixedCols(java.lang.String str)
          Create fixed size record and initialize to the specified string.
 
Method Summary
 java.lang.String buildField(int fldSize, java.lang.String str)
          Build fixed length field, using alignment and padding defaults.
static java.lang.String buildField(int fldSize, java.lang.String str, int alignFlg, char padchar)
          Create a fixed-length field.
 java.lang.String buildFieldLeft(int fldSize, java.lang.String str)
          Build left aligned field.
 java.lang.String buildFieldRight(int fldSize, java.lang.String str)
          Build right aligned field.
 java.lang.String dump()
          Dump contents of record.
 java.lang.String dump(java.lang.String str)
          Dump the specified string.
 int getAlignmentFlag()
          Get the default alignment flag.
static int getAlignmentFlag(java.lang.String flg)
          Get the numeric alignment flag from the string value.
 java.lang.String getField(int beg, int end)
          Get specified field.
 char getPadChar()
          Get the default padding character.
private  void init(int recsize)
          Initialize an empty WrpFixedCols object.
 int length()
          Return the length of the record.
static void main(java.lang.String[] args)
          Test driver.
 java.lang.String replace(int start, int end, java.lang.String str)
          Replace the existing substring from start to end with the specified string.
 void reset(int recsize)
          Reset (clear) the record.
 void reset(java.lang.String str)
          Reset (clear) record and initialize it with the specified string.
 void setAlignmentFlag(int alignmentFlag)
          Set the default alignment flag.
 java.lang.String setField(int beg, int end, java.lang.String fld)
          Set new value for field, returning the old value.
 java.lang.String setField(int beg, int end, java.lang.String fld, int alignFld, char padchar)
          Set the value of a field and return the old value.
 void setPadChar(char padChar)
          Set the default padding character.
 java.lang.String substring(int start, int end)
          Return substring of data record.
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

ALIGN_LEFT

public static final int ALIGN_LEFT
left alignment flag

See Also:
Constant Field Values

ALIGN_RIGHT

public static final int ALIGN_RIGHT
right alignment flag

See Also:
Constant Field Values

ALIGN_CENTER

public static final int ALIGN_CENTER
center alignment flag

See Also:
Constant Field Values

sbuf

private java.lang.StringBuffer sbuf
string buffer containing record data


alignmentFlag

private int alignmentFlag
alignment flag


padChar

private char padChar
padding character

Constructor Detail

WrpFixedCols

public WrpFixedCols(int recsize)
Create empty fixed record of a given size.

Parameters:
recsize - the record size

WrpFixedCols

public WrpFixedCols(java.lang.String str)
Create fixed size record and initialize to the specified string.

Parameters:
str - the record data

WrpFixedCols

public WrpFixedCols(int recsize,
                    int alignFlg,
                    char padchar)
Create new record and set alignment and padding. This method creates a new record of the specified size, alignment, and padding character.

Parameters:
recsize - the size of the record
alignFlg - the alignment for new fields
padchar - the padding character
Method Detail

init

private void init(int recsize)
Initialize an empty WrpFixedCols object.

Parameters:
recsize - the record size of the object

reset

public void reset(int recsize)
Reset (clear) the record. This method resets the record to all blanks. Any previous information about the record is lost.

Parameters:
recsize - the size of the record

reset

public void reset(java.lang.String str)
Reset (clear) record and initialize it with the specified string. Any previous information about the record is lost.

Parameters:
str - the data for the record

substring

public java.lang.String substring(int start,
                                  int end)
Return substring of data record.

Parameters:
start - first column of field (zero based)
end - first column past end of field (zero based)
Returns:
the substring from column start through column end-1

replace

public java.lang.String replace(int start,
                                int end,
                                java.lang.String str)
Replace the existing substring from start to end with the specified string. The replacing string must be the same size as the substring it replaces.

Parameters:
start - start of substring (zero based)
end - the first column past the end of the field (zero based)
str - the replacement string
Returns:
the string that was replaced, or null on error

buildField

public static java.lang.String buildField(int fldSize,
                                          java.lang.String str,
                                          int alignFlg,
                                          char padchar)
Create a fixed-length field.

Parameters:
fldSize - size of field
str - initial value of the field
alignFlg - alignment flag
padchar - padding character, in case string is shorter than fldSize
Returns:
a string as specified by the above parameters

buildField

public java.lang.String buildField(int fldSize,
                                   java.lang.String str)
Build fixed length field, using alignment and padding defaults. This method will create a fixed length field of the size and content as specified, with the default alignment and padding.

Parameters:
fldSize -
str -
Returns:
a string of the specified length and content

buildFieldLeft

public java.lang.String buildFieldLeft(int fldSize,
                                       java.lang.String str)
Build left aligned field. The field will be padded with spaces, if needed.

Parameters:
fldSize - size of field
str - string value of field
Returns:
a left aligned string (space padded)

buildFieldRight

public java.lang.String buildFieldRight(int fldSize,
                                        java.lang.String str)
Build right aligned field. The field will be padded with zeros, if needed.

Parameters:
fldSize - size of field
str - string value of field
Returns:
a right aligned string (zero padded)

setField

public java.lang.String setField(int beg,
                                 int end,
                                 java.lang.String fld,
                                 int alignFld,
                                 char padchar)
Set the value of a field and return the old value. This method assigns a new value to the specified field and returns the old value.

Parameters:
beg - index of start of field
end - index of first character past the field
fld - new value for field
alignFld - alignment flag
padchar - padding character
Returns:
the old value of the field

setField

public java.lang.String setField(int beg,
                                 int end,
                                 java.lang.String fld)
Set new value for field, returning the old value. This method assigns a new value to the specified field and returns the old value. The default alignment and padding will be used.

Parameters:
beg - index of start of field
end - index of first character past the field
fld - new value for field
Returns:
the old value of the field

getField

public java.lang.String getField(int beg,
                                 int end)
Get specified field. This method returns the specified substring. This method is essentially the same as the substring method, but the terminology better suits the concept of fields.

Parameters:
beg - first column of field (zero based)
end - first column past end of field (zero based)
Returns:
the substring from column start through column end-1

length

public int length()
Return the length of the record.

Returns:
the length of the record

getAlignmentFlag

public static int getAlignmentFlag(java.lang.String flg)
Get the numeric alignment flag from the string value.

Parameters:
flg - string containing 'L', 'R', or 'C'
Returns:
the numeric equivalence of the alignment string

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

dump

public java.lang.String dump()
Dump contents of record. White space characters are replaced by up arrows (^).

Returns:
the record with white space translated to up arrows

dump

public java.lang.String dump(java.lang.String str)
Dump the specified string. Substitute up arrows (^) for white space.

Parameters:
str - string to dump
Returns:
the string with white space translated to up arrows

getAlignmentFlag

public int getAlignmentFlag()
Get the default alignment flag.

Returns:
the alignmentFlag

setAlignmentFlag

public void setAlignmentFlag(int alignmentFlag)
Set the default alignment flag.

Parameters:
alignmentFlag - the alignmentFlag to set

getPadChar

public char getPadChar()
Get the default padding character.

Returns:
the padChar

setPadChar

public void setPadChar(char padChar)
Set the default padding character.

Parameters:
padChar - the padChar to set

main

public static void main(java.lang.String[] args)
Test driver.

Parameters:
args - not used