Skip to content

Instantly share code, notes, and snippets.

@born2snipe
Created September 15, 2009 23:04
Show Gist options
  • Save born2snipe/187744 to your computer and use it in GitHub Desktop.
Save born2snipe/187744 to your computer and use it in GitHub Desktop.
package flapjack;
import flapjack.cobol.layout.AbstractCobolRecordLayout;
import flapjack.io.LineRecordReader;
import flapjack.model.ObjectMapping;
import flapjack.model.ObjectMappingStore;
import flapjack.model.RecordFactory;
import flapjack.model.SameRecordFactoryResolver;
import flapjack.parser.ParseResult;
import flapjack.parser.RecordParserImpl;
import flapjack.parser.SameRecordLayoutResolver;
import flapjack.util.TypeConverter;
import flapjack.util.ValueConverter;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.List;
import junit.framework.TestCase;
public class DecimalFields extends TestCase {
public void test_() throws IOException {
ObjectMapping decimalMapping = new ObjectMapping(Decimals.class);
decimalMapping.add("Price", "price", FloatDollarValueConverter.class);
decimalMapping.add("Quantity", "quantity", DoubleDollarValueConverter.class);
decimalMapping.add("Other", "other", DoubleDollarValueConverter.class);
ObjectMappingStore objectMappingStore = new ObjectMappingStore();
objectMappingStore.add(decimalMapping);
TypeConverter typeConverter = new TypeConverter();
typeConverter.registerConverter(new DoubleDollarValueConverter());
typeConverter.registerConverter(new FloatDollarValueConverter());
RecordParserImpl recordParser = new RecordParserImpl();
recordParser.setRecordLayoutResolver(new SameRecordLayoutResolver(DecimalsRecordLayout.class));
recordParser.setRecordFactoryResolver(new SameRecordFactoryResolver(DecimalsRecordFactory.class));
recordParser.setObjectMappingStore(objectMappingStore);
recordParser.setIgnoreUnmappedFields(true);
recordParser.setTypeConverter(typeConverter);
String records = "123451234512345";
LineRecordReader recordReader = new LineRecordReader(new ByteArrayInputStream(records.getBytes()));
ParseResult result = recordParser.parse(recordReader);
List results = result.getRecords();
assertEquals(1, results.size());
Decimals dec = (Decimals) results.get(0);
System.out.println(dec.price);
System.out.println(dec.quantity);
System.out.println(dec.other);
assertEquals(123.45f, dec.price);
assertEquals(123.45, dec.quantity);
assertEquals(123.45, dec.other);
}
private static class DecimalsRecordLayout extends AbstractCobolRecordLayout {
protected void defineFields() {
cobolField("Price", "9(3)v9(2)");
cobolField("Quantity", "999v99");
cobolField("Other", "9(3)v99");
}
}
private static class Decimals {
public float price;
public double quantity;
public Double other;
}
private static class DecimalsRecordFactory implements RecordFactory {
public Object build() {
return new Decimals();
}
}
private static class DoubleDollarValueConverter implements ValueConverter {
public Object convert(byte[] bytes) {
String value = new String(bytes);
int ghostDecimalPointIndex = value.length() - 2;
return Double.parseDouble(value.substring(0, ghostDecimalPointIndex) + "." + value.substring(ghostDecimalPointIndex));
}
}
private static class FloatDollarValueConverter implements ValueConverter {
public Object convert(byte[] bytes) {
String value = new String(bytes);
int ghostDecimalPointIndex = value.length() - 2;
return Float.parseFloat(value.substring(0, ghostDecimalPointIndex) + "." + value.substring(ghostDecimalPointIndex));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment