FieldParseInfo is now an abstract class, with a concrete subclass for each IsoType. This greatly improves the parsing speed.

git-svn-id: https://j8583.svn.sourceforge.net/svnroot/j8583/j8583@66 db60a039-f72e-0410-b1d2-baa000846441
This commit is contained in:
chochos
2011-02-21 19:53:47 +00:00
parent 3b36a1dfe9
commit d291d41bbc
16 changed files with 781 additions and 395 deletions
@@ -0,0 +1,64 @@
package com.solab.iso8583.parse;
import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;
import com.solab.iso8583.CustomField;
import com.solab.iso8583.IsoType;
import com.solab.iso8583.IsoValue;
/** This class is used to parse fields of type DATE4.
*
* @author Enrique Zamudio
*/
public class Date4ParseInfo extends FieldParseInfo {
public Date4ParseInfo() {
super(IsoType.DATE4, 4);
}
@Override
public <T> IsoValue<?> parse(byte[] buf, int pos, CustomField<T> custom)
throws ParseException {
if (pos < 0) {
throw new ParseException(String.format("Invalid position %d", pos), pos);
}
if (pos+4 > buf.length) {
throw new ParseException(String.format("Insufficient data for DATE4 field, pos %d", pos), pos);
}
Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
//Set the month in the date
cal.set(Calendar.MONTH, ((buf[pos] - 48) * 10) + buf[pos + 1] - 49);
cal.set(Calendar.DATE, ((buf[pos + 2] - 48) * 10) + buf[pos + 3] - 48);
if (cal.getTime().after(new Date())) {
cal.add(Calendar.YEAR, -1);
}
return new IsoValue<Date>(type, cal.getTime(), null);
}
@Override
public <T> IsoValue<?> parseBinary(byte[] buf, int pos,
CustomField<T> custom) throws ParseException {
int[] tens = new int[2];
int start = 0;
for (int i = pos; i < pos + tens.length; i++) {
tens[start++] = (((buf[i] & 0xf0) >> 4) * 10) + (buf[i] & 0x0f);
}
Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
//Set the month in the date
cal.set(Calendar.MONTH, tens[0] - 1);
cal.set(Calendar.DATE, tens[1]);
if (cal.getTime().after(new Date())) {
cal.add(Calendar.YEAR, -1);
}
return new IsoValue<Date>(type, cal.getTime(), null);
}
}