FwF - Fixed Width Fields Formatting

Have you ever struggled with text data sent back and forth to mainframes? Were every text line have equal width and the data is hidden in there at fixed positions. I mean something similar to the snippet below

AA200401150000000150+foobar..................................0000000002
AA200401160000000250+productx................................0000000005
AA200401190000000125+whatever.product........................0000000010
AA200401220000002010+some.very.fancy.product.................0000000001
AA200401270000000030+yet.another.product.name................0000000008
            

It's a hypothetical order list, containing order date, total price, product name and quantity. This is a perfect fit for a COBOL program using a data declarations like the snippet below.

01 FILLER    PIC XX.
01 DATE      PIX 9(8).
01 PRICE     PIC 9(10).
01 SIGN      PIC X.
01 PRODUCT   PIC X(40).
01 QUANTITY  PIC 9(10).
            

In Java, on the other hand, we have to work a little bit harder. This library solves this problem and provides a simple and intuitive way to declare a Fixed width Field text row. You can then generate (format) text lines from plain ordinary Java objects (POJO) and also recognize (parse) them back into POJOs. The FwF data set above can be declared by this snippet

String  fwfSpec = "{const:value=AA} {date:pattern=yyyyMMdd} {int:width=10} {sign} {text:width=40,pad=.align=left} {int:width=10}";

The snippet below, generates the first line above of the hypothetical order list.

Date       date     = new Date();
Integer    price    = new Integer(150);
String     product  = "foobar";
Integer    quantity = new Integer(2);
Object[]   data     = {null, date, price, price, product, quantity};
Formatter  fmt      = new Formatter(fwfSpec);
String     txt      = fmt.format( data );
            

Read more about this library in the User's Guide.