new Java 7 features #32
This commit is contained in:
@@ -204,9 +204,9 @@ public class IsoMessage {
|
||||
} else {
|
||||
IsoValue<T> v = null;
|
||||
if (t.needsLength()) {
|
||||
v = new IsoValue<T>(t, value, length, encoder);
|
||||
v = new IsoValue<>(t, value, length, encoder);
|
||||
} else {
|
||||
v = new IsoValue<T>(t, value, encoder);
|
||||
v = new IsoValue<>(t, value, encoder);
|
||||
}
|
||||
v.setCharacterEncoding(encoding);
|
||||
fields[index] = v;
|
||||
|
||||
@@ -187,26 +187,26 @@ public enum IsoType {
|
||||
}
|
||||
|
||||
public <T> IsoValue<T> value(T val, int len) {
|
||||
return new IsoValue<T>(this, val, len);
|
||||
return new IsoValue<>(this, val, len);
|
||||
}
|
||||
|
||||
public <T> IsoValue<T> value(T val) {
|
||||
return new IsoValue<T>(this, val);
|
||||
return new IsoValue<>(this, val);
|
||||
}
|
||||
|
||||
public <T> IsoValue<T> call(T val, int len) {
|
||||
return new IsoValue<T>(this, val, len);
|
||||
return new IsoValue<>(this, val, len);
|
||||
}
|
||||
|
||||
public <T> IsoValue<T> call(T val) {
|
||||
return new IsoValue<T>(this, val);
|
||||
return new IsoValue<>(this, val);
|
||||
}
|
||||
|
||||
public <T> IsoValue<T> apply(T val, int len) {
|
||||
return new IsoValue<T>(this, val, len);
|
||||
return new IsoValue<>(this, val, len);
|
||||
}
|
||||
public <T> IsoValue<T> apply(T val) {
|
||||
return new IsoValue<T>(this, val);
|
||||
return new IsoValue<>(this, val);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -49,18 +49,18 @@ public class MessageFactory<T extends IsoMessage> {
|
||||
protected final Logger log = LoggerFactory.getLogger(getClass());
|
||||
|
||||
/** This map stores the message template for each message type. */
|
||||
private Map<Integer, T> typeTemplates = new HashMap<Integer, T>();
|
||||
private Map<Integer, T> typeTemplates = new HashMap<>();
|
||||
/** Stores the information needed to parse messages sorted by type. */
|
||||
protected Map<Integer, Map<Integer, FieldParseInfo>> parseMap = new HashMap<Integer, Map<Integer, FieldParseInfo>>();
|
||||
protected Map<Integer, Map<Integer, FieldParseInfo>> parseMap = new HashMap<>();
|
||||
/** Stores the field numbers to be parsed, in order of appearance. */
|
||||
protected Map<Integer, List<Integer>> parseOrder = new HashMap<Integer, List<Integer>>();
|
||||
protected Map<Integer, List<Integer>> parseOrder = new HashMap<>();
|
||||
|
||||
private TraceNumberGenerator traceGen;
|
||||
/** The ISO header to be included in each message type. */
|
||||
private Map<Integer, String> isoHeaders = new HashMap<Integer, String>();
|
||||
private Map<Integer, String> isoHeaders = new HashMap<>();
|
||||
/** A map for the custom field encoder/decoders, keyed by field number. */
|
||||
@SuppressWarnings("rawtypes")
|
||||
private Map<Integer, CustomField> customFields = new HashMap<Integer, CustomField>();
|
||||
private Map<Integer, CustomField> customFields = new HashMap<>();
|
||||
/** Indicates if the current date should be set on new messages (field 7). */
|
||||
private boolean setDate;
|
||||
/** Indicates if the factory should create binary messages and also parse binary messages. */
|
||||
@@ -576,7 +576,7 @@ public class MessageFactory<T extends IsoMessage> {
|
||||
* of field to expect. The keys will be the field numbers. */
|
||||
public void setParseMap(int type, Map<Integer, FieldParseInfo> map) {
|
||||
parseMap.put(type, map);
|
||||
ArrayList<Integer> index = new ArrayList<Integer>();
|
||||
ArrayList<Integer> index = new ArrayList<>();
|
||||
index.addAll(map.keySet());
|
||||
Collections.sort(index);
|
||||
log.trace(String.format("ISO8583 MessageFactory adding parse map for type %04x with fields %s", type, index));
|
||||
|
||||
@@ -41,14 +41,14 @@ public class CompositeField implements CustomBinaryField<CompositeField> {
|
||||
@SuppressWarnings("rawtypes")
|
||||
public CompositeField addValue(IsoValue<?> v) {
|
||||
if (values == null) {
|
||||
values = new ArrayList<IsoValue>(4);
|
||||
values = new ArrayList<>(4);
|
||||
}
|
||||
values.add(v);
|
||||
return this;
|
||||
}
|
||||
public <T> CompositeField addValue(T val, CustomField<T> encoder, IsoType t, int length) {
|
||||
return addValue(t.needsLength() ? new IsoValue<T>(t, val, length, encoder)
|
||||
: new IsoValue<T>(t, val, encoder));
|
||||
return addValue(t.needsLength() ? new IsoValue<>(t, val, length, encoder)
|
||||
: new IsoValue<>(t, val, encoder));
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@@ -78,7 +78,7 @@ public class CompositeField implements CustomBinaryField<CompositeField> {
|
||||
@Override
|
||||
public CompositeField decodeBinaryField(byte[] buf, int offset, int length) {
|
||||
@SuppressWarnings("rawtypes")
|
||||
List<IsoValue> vals = new ArrayList<IsoValue>(parsers.size());
|
||||
List<IsoValue> vals = new ArrayList<>(parsers.size());
|
||||
int pos = 0;
|
||||
try {
|
||||
for (FieldParseInfo fpi : parsers) {
|
||||
@@ -114,7 +114,7 @@ public class CompositeField implements CustomBinaryField<CompositeField> {
|
||||
@Override
|
||||
public CompositeField decodeField(String value) {
|
||||
@SuppressWarnings("rawtypes")
|
||||
List<IsoValue> vals = new ArrayList<IsoValue>(parsers.size());
|
||||
List<IsoValue> vals = new ArrayList<>(parsers.size());
|
||||
byte[] buf = value.getBytes();
|
||||
int pos = 0;
|
||||
try {
|
||||
@@ -158,23 +158,19 @@ public class CompositeField implements CustomBinaryField<CompositeField> {
|
||||
|
||||
@Override
|
||||
public String encodeField(CompositeField value) {
|
||||
final ByteArrayOutputStream bout = new ByteArrayOutputStream();
|
||||
byte[] buf = null;
|
||||
try {
|
||||
String encoding = null;
|
||||
final ByteArrayOutputStream bout = new ByteArrayOutputStream();
|
||||
for (IsoValue<?> v : value.getValues()) {
|
||||
v.write(bout, false, true);
|
||||
if (encoding == null)encoding = v.getCharacterEncoding();
|
||||
}
|
||||
buf = bout.toByteArray();
|
||||
final byte[] buf = bout.toByteArray();
|
||||
return new String(buf, encoding==null?"UTF-8":encoding);
|
||||
} catch (UnsupportedEncodingException ex) {
|
||||
log.error("Encoding text CompositeField", ex);
|
||||
} catch (IOException ex) {
|
||||
log.error("Encoding text CompositeField", ex);
|
||||
//shouldn't happen
|
||||
return "";
|
||||
}
|
||||
return new String(buf);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -58,7 +58,7 @@ public class ConfigParser {
|
||||
ClassLoader loader) throws IOException {
|
||||
if (loader.getResource("j8583.xml") == null) {
|
||||
log.warn("ISO8583 ConfigParser cannot find j8583.xml, returning empty message factory");
|
||||
return new MessageFactory<IsoMessage>();
|
||||
return new MessageFactory<>();
|
||||
} else {
|
||||
return createFromClasspathConfig(loader, "j8583.xml");
|
||||
}
|
||||
@@ -81,29 +81,23 @@ public class ConfigParser {
|
||||
* using MessageFactory's ClassLoader. */
|
||||
public static MessageFactory<IsoMessage> createFromClasspathConfig(
|
||||
ClassLoader loader, String path) throws IOException {
|
||||
InputStream ins = loader.getResourceAsStream(path);
|
||||
MessageFactory<IsoMessage> mfact = new MessageFactory<IsoMessage>();
|
||||
if (ins != null) {
|
||||
log.debug("ISO8583 Parsing config from classpath file {}", path);
|
||||
try {
|
||||
parse(mfact, ins);
|
||||
} finally {
|
||||
ins.close();
|
||||
}
|
||||
} else {
|
||||
log.warn("ISO8583 File not found in classpath: {}", path);
|
||||
}
|
||||
MessageFactory<IsoMessage> mfact = new MessageFactory<>();
|
||||
try (InputStream ins = loader.getResourceAsStream(path)) {
|
||||
if (ins != null) {
|
||||
log.debug("ISO8583 Parsing config from classpath file {}", path);
|
||||
parse(mfact, ins);
|
||||
} else {
|
||||
log.error("ISO8583 File not found in classpath: {}", path);
|
||||
}
|
||||
}
|
||||
return mfact;
|
||||
}
|
||||
|
||||
/** Creates a message factory from the file located at the specified URL. */
|
||||
public static MessageFactory<IsoMessage> createFromUrl(URL url) throws IOException {
|
||||
MessageFactory<IsoMessage> mfact = new MessageFactory<IsoMessage>();
|
||||
InputStream stream = url.openStream();
|
||||
try {
|
||||
MessageFactory<IsoMessage> mfact = new MessageFactory<>();
|
||||
try (InputStream stream = url.openStream()) {
|
||||
parse(mfact, stream);
|
||||
} finally {
|
||||
stream.close();
|
||||
}
|
||||
return mfact;
|
||||
}
|
||||
@@ -352,12 +346,9 @@ public class ConfigParser {
|
||||
}
|
||||
});
|
||||
doc = docb.parse(stream);
|
||||
} catch (ParserConfigurationException ex) {
|
||||
} catch (ParserConfigurationException | SAXException ex) {
|
||||
log.error("ISO8583 Cannot parse XML configuration", ex);
|
||||
return;
|
||||
} catch (SAXException ex) {
|
||||
log.error("ISO8583 Parsing XML configuration", ex);
|
||||
return;
|
||||
}
|
||||
final Element root = doc.getDocumentElement();
|
||||
|
||||
@@ -382,11 +373,8 @@ public class ConfigParser {
|
||||
* configure the message factory from that config. */
|
||||
public static <T extends IsoMessage> void configureFromUrl(
|
||||
MessageFactory<T> mfact, URL url) throws IOException {
|
||||
InputStream stream = url.openStream();
|
||||
try {
|
||||
try (InputStream stream = url.openStream()) {
|
||||
parse(mfact, stream);
|
||||
} finally {
|
||||
stream.close();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -395,17 +383,14 @@ public class ConfigParser {
|
||||
* Spring-bound instances of MessageFactory for example. */
|
||||
public static <T extends IsoMessage> void configureFromClasspathConfig(
|
||||
MessageFactory<T> mfact, String path) throws IOException {
|
||||
InputStream ins = mfact.getClass().getClassLoader().getResourceAsStream(path);
|
||||
if (ins != null) {
|
||||
log.debug("ISO8583 Parsing config from classpath file {}", path);
|
||||
try {
|
||||
parse(mfact, ins);
|
||||
} finally {
|
||||
ins.close();
|
||||
}
|
||||
} else {
|
||||
log.warn("ISO8583 File not found in classpath: {}", path);
|
||||
}
|
||||
try (InputStream ins = mfact.getClass().getClassLoader().getResourceAsStream(path)) {
|
||||
if (ins != null) {
|
||||
log.debug("ISO8583 Parsing config from classpath file {}", path);
|
||||
parse(mfact, ins);
|
||||
} else {
|
||||
log.warn("ISO8583 File not found in classpath: {}", path);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** Parses a message type expressed as a hex string and returns the integer number.
|
||||
|
||||
@@ -69,12 +69,12 @@ public class LllvarParseInfo extends FieldParseInfo {
|
||||
getCharacterEncoding()).substring(0, len);
|
||||
}
|
||||
if (custom == null) {
|
||||
return new IsoValue<String>(type, _v, len, null);
|
||||
return new IsoValue<>(type, _v, len, null);
|
||||
} else {
|
||||
T decoded = custom.decodeField(_v);
|
||||
//If decode fails, return string; otherwise use the decoded object and its codec
|
||||
return decoded == null ? new IsoValue<String>(type, _v, len, null) :
|
||||
new IsoValue<T>(type, decoded, len, custom);
|
||||
return decoded == null ? new IsoValue<>(type, _v, len, null) :
|
||||
new IsoValue<>(type, decoded, len, custom);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -96,12 +96,12 @@ public class LllvarParseInfo extends FieldParseInfo {
|
||||
"Insufficient data for bin LLLVAR field %d, pos %d", field, pos), pos);
|
||||
}
|
||||
if (custom == null) {
|
||||
return new IsoValue<String>(type, new String(buf, pos + 2, len, getCharacterEncoding()), null);
|
||||
return new IsoValue<>(type, new String(buf, pos + 2, len, getCharacterEncoding()), null);
|
||||
} else {
|
||||
IsoValue<T> v = new IsoValue<T>(type, custom.decodeField(
|
||||
IsoValue<T> v = new IsoValue<>(type, custom.decodeField(
|
||||
new String(buf, pos + 2, len, getCharacterEncoding())), custom);
|
||||
if (v.getValue() == null) {
|
||||
return new IsoValue<String>(type,
|
||||
return new IsoValue<>(type,
|
||||
new String(buf, pos + 2, len, getCharacterEncoding()), null);
|
||||
}
|
||||
return v;
|
||||
|
||||
Reference in New Issue
Block a user