From e74fb1b51cb0a6b516cbcb2cadbfbdb732d7bf85 Mon Sep 17 00:00:00 2001 From: Enrique Zamudio Date: Fri, 28 Nov 2014 12:50:09 -0600 Subject: [PATCH] new Java 7 features #32 --- .../java/com/solab/iso8583/IsoMessage.java | 4 +- src/main/java/com/solab/iso8583/IsoType.java | 12 ++-- .../com/solab/iso8583/MessageFactory.java | 12 ++-- .../solab/iso8583/codecs/CompositeField.java | 20 +++---- .../com/solab/iso8583/parse/ConfigParser.java | 59 +++++++------------ .../solab/iso8583/parse/LllvarParseInfo.java | 12 ++-- 6 files changed, 50 insertions(+), 69 deletions(-) diff --git a/src/main/java/com/solab/iso8583/IsoMessage.java b/src/main/java/com/solab/iso8583/IsoMessage.java index 8360c3f..52bf6e0 100644 --- a/src/main/java/com/solab/iso8583/IsoMessage.java +++ b/src/main/java/com/solab/iso8583/IsoMessage.java @@ -204,9 +204,9 @@ public class IsoMessage { } else { IsoValue v = null; if (t.needsLength()) { - v = new IsoValue(t, value, length, encoder); + v = new IsoValue<>(t, value, length, encoder); } else { - v = new IsoValue(t, value, encoder); + v = new IsoValue<>(t, value, encoder); } v.setCharacterEncoding(encoding); fields[index] = v; diff --git a/src/main/java/com/solab/iso8583/IsoType.java b/src/main/java/com/solab/iso8583/IsoType.java index eb1b1d6..fed34b9 100644 --- a/src/main/java/com/solab/iso8583/IsoType.java +++ b/src/main/java/com/solab/iso8583/IsoType.java @@ -187,26 +187,26 @@ public enum IsoType { } public IsoValue value(T val, int len) { - return new IsoValue(this, val, len); + return new IsoValue<>(this, val, len); } public IsoValue value(T val) { - return new IsoValue(this, val); + return new IsoValue<>(this, val); } public IsoValue call(T val, int len) { - return new IsoValue(this, val, len); + return new IsoValue<>(this, val, len); } public IsoValue call(T val) { - return new IsoValue(this, val); + return new IsoValue<>(this, val); } public IsoValue apply(T val, int len) { - return new IsoValue(this, val, len); + return new IsoValue<>(this, val, len); } public IsoValue apply(T val) { - return new IsoValue(this, val); + return new IsoValue<>(this, val); } } diff --git a/src/main/java/com/solab/iso8583/MessageFactory.java b/src/main/java/com/solab/iso8583/MessageFactory.java index 02cd841..160e329 100644 --- a/src/main/java/com/solab/iso8583/MessageFactory.java +++ b/src/main/java/com/solab/iso8583/MessageFactory.java @@ -49,18 +49,18 @@ public class MessageFactory { protected final Logger log = LoggerFactory.getLogger(getClass()); /** This map stores the message template for each message type. */ - private Map typeTemplates = new HashMap(); + private Map typeTemplates = new HashMap<>(); /** Stores the information needed to parse messages sorted by type. */ - protected Map> parseMap = new HashMap>(); + protected Map> parseMap = new HashMap<>(); /** Stores the field numbers to be parsed, in order of appearance. */ - protected Map> parseOrder = new HashMap>(); + protected Map> parseOrder = new HashMap<>(); private TraceNumberGenerator traceGen; /** The ISO header to be included in each message type. */ - private Map isoHeaders = new HashMap(); + private Map isoHeaders = new HashMap<>(); /** A map for the custom field encoder/decoders, keyed by field number. */ @SuppressWarnings("rawtypes") - private Map customFields = new HashMap(); + private Map 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 { * of field to expect. The keys will be the field numbers. */ public void setParseMap(int type, Map map) { parseMap.put(type, map); - ArrayList index = new ArrayList(); + ArrayList 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)); diff --git a/src/main/java/com/solab/iso8583/codecs/CompositeField.java b/src/main/java/com/solab/iso8583/codecs/CompositeField.java index 6cbfe24..5b6e2aa 100644 --- a/src/main/java/com/solab/iso8583/codecs/CompositeField.java +++ b/src/main/java/com/solab/iso8583/codecs/CompositeField.java @@ -41,14 +41,14 @@ public class CompositeField implements CustomBinaryField { @SuppressWarnings("rawtypes") public CompositeField addValue(IsoValue v) { if (values == null) { - values = new ArrayList(4); + values = new ArrayList<>(4); } values.add(v); return this; } public CompositeField addValue(T val, CustomField encoder, IsoType t, int length) { - return addValue(t.needsLength() ? new IsoValue(t, val, length, encoder) - : new IsoValue(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 { @Override public CompositeField decodeBinaryField(byte[] buf, int offset, int length) { @SuppressWarnings("rawtypes") - List vals = new ArrayList(parsers.size()); + List vals = new ArrayList<>(parsers.size()); int pos = 0; try { for (FieldParseInfo fpi : parsers) { @@ -114,7 +114,7 @@ public class CompositeField implements CustomBinaryField { @Override public CompositeField decodeField(String value) { @SuppressWarnings("rawtypes") - List vals = new ArrayList(parsers.size()); + List vals = new ArrayList<>(parsers.size()); byte[] buf = value.getBytes(); int pos = 0; try { @@ -158,23 +158,19 @@ public class CompositeField implements CustomBinaryField { @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 diff --git a/src/main/java/com/solab/iso8583/parse/ConfigParser.java b/src/main/java/com/solab/iso8583/parse/ConfigParser.java index 054fea7..ed05ec2 100644 --- a/src/main/java/com/solab/iso8583/parse/ConfigParser.java +++ b/src/main/java/com/solab/iso8583/parse/ConfigParser.java @@ -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(); + return new MessageFactory<>(); } else { return createFromClasspathConfig(loader, "j8583.xml"); } @@ -81,29 +81,23 @@ public class ConfigParser { * using MessageFactory's ClassLoader. */ public static MessageFactory createFromClasspathConfig( ClassLoader loader, String path) throws IOException { - InputStream ins = loader.getResourceAsStream(path); - MessageFactory mfact = new MessageFactory(); - 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 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 createFromUrl(URL url) throws IOException { - MessageFactory mfact = new MessageFactory(); - InputStream stream = url.openStream(); - try { + MessageFactory 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 void configureFromUrl( MessageFactory 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 void configureFromClasspathConfig( MessageFactory 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. diff --git a/src/main/java/com/solab/iso8583/parse/LllvarParseInfo.java b/src/main/java/com/solab/iso8583/parse/LllvarParseInfo.java index daec5fc..0def11b 100644 --- a/src/main/java/com/solab/iso8583/parse/LllvarParseInfo.java +++ b/src/main/java/com/solab/iso8583/parse/LllvarParseInfo.java @@ -69,12 +69,12 @@ public class LllvarParseInfo extends FieldParseInfo { getCharacterEncoding()).substring(0, len); } if (custom == null) { - return new IsoValue(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(type, _v, len, null) : - new IsoValue(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(type, new String(buf, pos + 2, len, getCharacterEncoding()), null); + return new IsoValue<>(type, new String(buf, pos + 2, len, getCharacterEncoding()), null); } else { - IsoValue v = new IsoValue(type, custom.decodeField( + IsoValue v = new IsoValue<>(type, custom.decodeField( new String(buf, pos + 2, len, getCharacterEncoding())), custom); if (v.getValue() == null) { - return new IsoValue(type, + return new IsoValue<>(type, new String(buf, pos + 2, len, getCharacterEncoding()), null); } return v;