From e91a272f7343bc90ce4ef6114cc187da26a4b8e6 Mon Sep 17 00:00:00 2001 From: Enrique Zamudio Date: Tue, 23 Apr 2013 10:50:32 -0500 Subject: [PATCH] Fix #7 - add new setter that only updates existing values --- .../java/com/solab/iso8583/IsoMessage.java | 21 ++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/solab/iso8583/IsoMessage.java b/src/main/java/com/solab/iso8583/IsoMessage.java index 5f5dc74..f06b590 100644 --- a/src/main/java/com/solab/iso8583/IsoMessage.java +++ b/src/main/java/com/solab/iso8583/IsoMessage.java @@ -205,6 +205,25 @@ public class IsoMessage { return this; } + /** A convenience method to set new values in fields that already contain values. + * The field's type, length and custom encoder are taken from the current value. + * This method can only be used with fields that have been previously set, + * usually from a template in the MessageFactory. + * @param index The field's index + * @param value The new value to be set in that field. + * @return The message itself. + * @throws IllegalArgumentException if there is no current field at the specified index. */ + public IsoMessage updateValue(int index, T value) { + IsoValue current = getField(index); + if (current == null) { + throw new IllegalArgumentException("Value-only field setter can only be used on existing fields"); + } else { + setValue(index, value, current.getEncoder(), current.getType(), current.getLength()); + getField(index).setCharacterEncoding(current.getCharacterEncoding()); + } + return this; + } + /** Returns true is the message has a value in the specified field. * @param idx The field number. */ public boolean hasField(int idx) { @@ -399,7 +418,7 @@ public class IsoMessage { * not present in the source message it is simply ignored. */ public void copyFieldsFrom(IsoMessage src, int...idx) { for (int i : idx) { - IsoValue v = src.getField(i); + IsoValue v = src.getField(i); if (v != null) { setValue(i, v.getValue(), v.getEncoder(), v.getType(), v.getLength()); }