Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

QFJ-962: FieldMap.setField(int key, Field<?> field) does not properly convert values #226

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions quickfixj-core/src/main/java/quickfix/BooleanField.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

package quickfix;

import quickfix.field.converter.BooleanConverter;

import java.lang.Boolean;

public class BooleanField extends Field<Boolean> {
Expand Down Expand Up @@ -54,4 +56,10 @@ public boolean valueEquals(Boolean value) {
public boolean valueEquals(boolean value) {
return getObject().equals(value);
}

@Override
public String convertToString() {
return BooleanConverter.convert(getValue());
}

}
15 changes: 7 additions & 8 deletions quickfixj-core/src/main/java/quickfix/BytesField.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@

package quickfix;

import java.io.UnsupportedEncodingException;

import org.quickfixj.QFJException;
import java.nio.charset.StandardCharsets;

/**
* BytesField enables better handling of binary data. With BytesFields binary data can
Expand All @@ -47,11 +45,12 @@ public byte[] getValue() {

@Override
protected String objectAsString() {
try {
return new String(getObject(), "UTF8");
} catch (UnsupportedEncodingException e) {
throw new QFJException(e);
}
return new String(getObject(), StandardCharsets.UTF_8);
}

@Override
public String convertToString() {
return objectAsString();
}

}
8 changes: 8 additions & 0 deletions quickfixj-core/src/main/java/quickfix/CharField.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

package quickfix;

import quickfix.field.converter.CharConverter;

import java.lang.Character;

/**
Expand Down Expand Up @@ -57,4 +59,10 @@ public boolean valueEquals(Character value) {
public boolean valueEquals(char value) {
return getObject().equals(value);
}

@Override
public String convertToString() {
return CharConverter.convert(getValue());
}

}
8 changes: 8 additions & 0 deletions quickfixj-core/src/main/java/quickfix/DateField.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

package quickfix;

import quickfix.field.converter.UtcTimestampConverter;

import java.util.Date;

/**
Expand All @@ -45,4 +47,10 @@ public Date getValue() {
public boolean valueEquals(Date value) {
return getValue().equals(value);
}

@Override
public String convertToString() {
return UtcTimestampConverter.convert(getValue(), true);
}

}
8 changes: 8 additions & 0 deletions quickfixj-core/src/main/java/quickfix/DecimalField.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

package quickfix;

import quickfix.field.converter.DecimalConverter;

import java.math.BigDecimal;

/**
Expand Down Expand Up @@ -68,4 +70,10 @@ public boolean valueEquals(BigDecimal value) {
public boolean valueEquals(double value) {
return getValue().compareTo(new BigDecimal(value)) == 0;
}

@Override
public String convertToString() {
return DecimalConverter.convert(getValue(), getPadding());
}

}
8 changes: 8 additions & 0 deletions quickfixj-core/src/main/java/quickfix/DoubleField.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

package quickfix;

import quickfix.field.converter.DoubleConverter;

/**
* A double-values message field.
*/
Expand Down Expand Up @@ -78,4 +80,10 @@ private void checkForValidDouble(Double value) throws NumberFormatException {
throw new NumberFormatException("Tried to set NaN or infinite value.");
}
}

@Override
public String convertToString() {
return DoubleConverter.convert(getValue(), getPadding());
}

}
9 changes: 5 additions & 4 deletions quickfixj-core/src/main/java/quickfix/Field.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,9 @@
import java.io.Serializable;

/**
* Base class for FIX message fields. This class should be
* abstract but that would break compatibility with the QF JNI
* classes.
* Base class for FIX message fields.
*/
public /*abstract*/ class Field<T> implements Serializable {
public abstract class Field<T> implements Serializable {
static final long serialVersionUID = 7098326013456432197L;
private int tag;
private T object;
Expand Down Expand Up @@ -152,4 +150,7 @@ public void setTag(int tag) {
isCalculated = false;
calculate();
}

public abstract String convertToString();

}
42 changes: 27 additions & 15 deletions quickfixj-core/src/main/java/quickfix/FieldMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ public void clear() {

public void reset() {
fields.clear();
for(List<Group> groupList : groups.values()) {
for(Group group : groupList)
for (List<Group> groupList : groups.values()) {
for (Group group : groupList)
group.reset();
}
groups.clear();
Expand Down Expand Up @@ -194,7 +194,9 @@ public void setUtcTimeStamp(int field, LocalDateTime value) {
}

public void setUtcTimeStamp(int field, LocalDateTime value, boolean includeMilliseconds) {
setField(new StringField(field, UtcTimestampConverter.convert(value, includeMilliseconds ? UtcTimestampPrecision.MILLIS : UtcTimestampPrecision.SECONDS)));
setField(new StringField(field, UtcTimestampConverter.convert(value, includeMilliseconds ?
UtcTimestampPrecision.MILLIS :
UtcTimestampPrecision.SECONDS)));
}

public void setUtcTimeStamp(int field, LocalDateTime value, UtcTimestampPrecision precision) {
Expand All @@ -206,7 +208,9 @@ public void setUtcTimeOnly(int field, LocalTime value) {
}

public void setUtcTimeOnly(int field, LocalTime value, boolean includeMilliseconds) {
setField(new StringField(field, UtcTimeOnlyConverter.convert(value, includeMilliseconds ? UtcTimestampPrecision.MILLIS : UtcTimestampPrecision.SECONDS)));
setField(new StringField(field, UtcTimeOnlyConverter.convert(value, includeMilliseconds ?
UtcTimestampPrecision.MILLIS :
UtcTimestampPrecision.SECONDS)));
}

public void setUtcTimeOnly(int field, LocalTime value, UtcTimestampPrecision precision) {
Expand Down Expand Up @@ -319,6 +323,10 @@ public LocalDate getUtcDateOnly(int field) throws FieldNotFound {
}

public void setField(int key, Field<?> field) {
setString(key, field.convertToString());
}

public void setField(int key, BytesField field) {
fields.put(key, field);
}

Expand Down Expand Up @@ -509,7 +517,8 @@ && getGroupCount(tag) > 0) {
}
}

private static final boolean IS_STRING_EQUIVALENT = CharsetSupport.isStringEquivalent(CharsetSupport.getCharsetInstance());
private static final boolean IS_STRING_EQUIVALENT = CharsetSupport
.isStringEquivalent(CharsetSupport.getCharsetInstance());

int calculateLength() {
int result = 0;
Expand All @@ -524,11 +533,14 @@ int calculateLength() {
for (Entry<Integer, List<Group>> entry : groups.entrySet()) {
final List<Group> groupList = entry.getValue();
if (!groupList.isEmpty()) {
if(IS_STRING_EQUIVALENT) {
result += getStringLength(entry.getKey()) + getStringLength(groupList.size()) + 2;
if (IS_STRING_EQUIVALENT) {
result +=
getStringLength(entry.getKey()) + getStringLength(groupList.size()) + 2;
} else {
result += MessageUtils.length(CharsetSupport.getCharsetInstance(), NumbersCache.get(entry.getKey()));
result += MessageUtils.length(CharsetSupport.getCharsetInstance(), NumbersCache.get(groupList.size()));
result += MessageUtils.length(CharsetSupport.getCharsetInstance(),
NumbersCache.get(entry.getKey()));
result += MessageUtils.length(CharsetSupport.getCharsetInstance(),
NumbersCache.get(groupList.size()));
result += 2;
}
for (int i = 0; i < groupList.size(); i++) {
Expand All @@ -540,9 +552,10 @@ int calculateLength() {
}

private static int getStringLength(int num) {
if(num == 0)
if (num == 0) {
return 1;
return (int)(num > 0 ? Math.log10(num) + 1 : Math.log10(-num) + 2);
}
return (int) (num > 0 ? Math.log10(num) + 1 : Math.log10(-num) + 2);
}

int calculateChecksum() {
Expand All @@ -556,12 +569,12 @@ int calculateChecksum() {
for (Entry<Integer, List<Group>> entry : groups.entrySet()) {
final List<Group> groupList = entry.getValue();
if (!groupList.isEmpty()) {
if(IS_STRING_EQUIVALENT) {
if (IS_STRING_EQUIVALENT) {
String value = NumbersCache.get(entry.getKey());
for (int i = value.length(); i-- != 0;)
for (int i = value.length(); i-- != 0; )
result += value.charAt(i);
value = NumbersCache.get(groupList.size());
for (int i = value.length(); i-- != 0;)
for (int i = value.length(); i-- != 0; )
result += value.charAt(i);
result += '=' + 1;
} else {
Expand Down Expand Up @@ -694,5 +707,4 @@ public boolean hasGroup(Group group) {
return hasGroup(group.getFieldTag());
}


}
8 changes: 8 additions & 0 deletions quickfixj-core/src/main/java/quickfix/IntField.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

package quickfix;

import quickfix.field.converter.IntConverter;

import java.lang.Integer;

/**
Expand Down Expand Up @@ -57,4 +59,10 @@ public boolean valueEquals(Integer value) {
public boolean valueEquals(int value) {
return getObject().equals(value);
}

@Override
public String convertToString() {
return IntConverter.convert(getValue());
}

}
6 changes: 6 additions & 0 deletions quickfixj-core/src/main/java/quickfix/StringField.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,10 @@ public String getValue() {
public boolean valueEquals(String value) {
return getValue().equals(value);
}

@Override
public String convertToString() {
return getValue();
}

}
9 changes: 8 additions & 1 deletion quickfixj-core/src/main/java/quickfix/UtcDateField.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

package quickfix;

import quickfix.field.converter.UtcDateOnlyConverter;

import java.time.LocalDate;
import java.time.ZoneOffset;

Expand Down Expand Up @@ -46,5 +48,10 @@ public LocalDate getValue() {
public boolean valueEquals(LocalDate value) {
return getValue().equals(value);
}


@Override
public String convertToString() {
return UtcDateOnlyConverter.convert(getValue());
}

}
9 changes: 8 additions & 1 deletion quickfixj-core/src/main/java/quickfix/UtcDateOnlyField.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

package quickfix;

import quickfix.field.converter.UtcDateOnlyConverter;

import java.time.LocalDate;
import java.time.ZoneOffset;

Expand All @@ -34,7 +36,7 @@ public UtcDateOnlyField(int field) {
protected UtcDateOnlyField(int field, LocalDate data) {
super(field, data);
}

public void setValue(LocalDate value) {
setObject(value);
}
Expand All @@ -47,4 +49,9 @@ public boolean valueEquals(LocalDate value) {
return getValue().equals(value);
}

@Override
public String convertToString() {
return UtcDateOnlyConverter.convert(getValue());
}

}
10 changes: 9 additions & 1 deletion quickfixj-core/src/main/java/quickfix/UtcTimeField.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

package quickfix;

import quickfix.field.converter.UtcTimeOnlyConverter;

import java.time.LocalTime;
import java.time.ZoneOffset;

Expand Down Expand Up @@ -53,8 +55,14 @@ public LocalTime getValue() {
public boolean valueEquals(LocalTime value) {
return getValue().equals(value);
}

public UtcTimestampPrecision getPrecision() {
return precision;
}

@Override
public String convertToString() {
return UtcTimeOnlyConverter.convert(getValue(), getPrecision());
}

}
Loading