Skip to content

Commit 4e6a5e5

Browse files
committed
Converted FileType to a real enum and updated related methods
1 parent 4ede8f3 commit 4e6a5e5

File tree

4 files changed

+96
-136
lines changed

4 files changed

+96
-136
lines changed

quickfixj-core/src/main/java/quickfix/DataDictionary.java

Lines changed: 51 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ public boolean isField(int field) {
188188
* @param field the tag
189189
* @return the field type
190190
*/
191-
public FieldType getFieldTypeEnum(int field) {
191+
public FieldType getFieldType(int field) {
192192
return fieldTypes.get(field);
193193
}
194194

@@ -291,17 +291,6 @@ private void addFieldType(int field, FieldType fieldType) {
291291
fieldTypes.put(field, fieldType);
292292
}
293293

294-
/**
295-
* Get the field type for a field.
296-
*
297-
* @param field a tag
298-
* @return the field type
299-
* @see #getFieldTypeEnum
300-
*/
301-
public int getFieldType(int field) {
302-
return getFieldTypeEnum(field).getOrdinal();
303-
}
304-
305294
/**
306295
* Get the field tag given a field name.
307296
*
@@ -450,12 +439,12 @@ public GroupInfo getGroup(String msg, int field) {
450439
* @return true if field is a raw data field, false otherwise
451440
*/
452441
public boolean isDataField(int field) {
453-
return fieldTypes.get(field) == FieldType.Data;
442+
return fieldTypes.get(field) == FieldType.DATA;
454443
}
455444

456445
private boolean isMultipleValueStringField(int field) {
457446
final FieldType fieldType = fieldTypes.get(field);
458-
return fieldType == FieldType.MultipleValueString || fieldType == FieldType.MultipleStringValue;
447+
return fieldType == FieldType.MULTIPLEVALUESTRING || fieldType == FieldType.MULTIPLESTRINGVALUE;
459448
}
460449

461450
/**
@@ -707,62 +696,55 @@ void checkField(Field<?> field, String msgType, boolean message) {
707696
}
708697

709698
private void checkValidFormat(StringField field) throws IncorrectDataFormat {
699+
FieldType fieldType = getFieldType(field.getTag());
700+
if (fieldType == null) {
701+
return;
702+
}
710703
try {
711-
final FieldType fieldType = getFieldTypeEnum(field.getTag());
712-
if (fieldType == FieldType.String) {
713-
// String
714-
} else if (fieldType == FieldType.Char) {
715-
if (beginString.compareTo(FixVersions.BEGINSTRING_FIX41) > 0) {
716-
CharConverter.convert(field.getValue());
717-
} else {
718-
// String, for older FIX versions
719-
}
720-
} else if (fieldType == FieldType.Price) {
721-
DoubleConverter.convert(field.getValue());
722-
} else if (fieldType == FieldType.Int) {
723-
IntConverter.convert(field.getValue());
724-
} else if (fieldType == FieldType.Amt) {
725-
DoubleConverter.convert(field.getValue());
726-
} else if (fieldType == FieldType.Qty) {
727-
DoubleConverter.convert(field.getValue());
728-
} else if (fieldType == FieldType.Qty) {
729-
// String
730-
} else if (fieldType == FieldType.MultipleValueString) {
731-
// String
732-
} else if (fieldType == FieldType.MultipleStringValue) {
733-
// String
734-
} else if (fieldType == FieldType.Exchange) {
735-
// String
736-
} else if (fieldType == FieldType.Boolean) {
737-
BooleanConverter.convert(field.getValue());
738-
} else if (fieldType == FieldType.LocalMktDate) {
739-
// String
740-
} else if (fieldType == FieldType.Data) {
741-
// String
742-
} else if (fieldType == FieldType.Float) {
743-
DoubleConverter.convert(field.getValue());
744-
} else if (fieldType == FieldType.PriceOffset) {
745-
DoubleConverter.convert(field.getValue());
746-
} else if (fieldType == FieldType.MonthYear) {
747-
// String
748-
} else if (fieldType == FieldType.DayOfMonth) {
749-
// String
750-
} else if (fieldType == FieldType.UtcDate) {
751-
UtcDateOnlyConverter.convert(field.getValue());
752-
} else if (fieldType == FieldType.UtcTimeOnly) {
753-
UtcTimeOnlyConverter.convert(field.getValue());
754-
} else if (fieldType == FieldType.UtcTimeStamp || fieldType == FieldType.Time) {
755-
UtcTimestampConverter.convert(field.getValue());
756-
} else if (fieldType == FieldType.NumInGroup) {
757-
IntConverter.convert(field.getValue());
758-
} else if (fieldType == FieldType.Percentage) {
759-
DoubleConverter.convert(field.getValue());
760-
} else if (fieldType == FieldType.SeqNum) {
761-
IntConverter.convert(field.getValue());
762-
} else if (fieldType == FieldType.Length) {
763-
IntConverter.convert(field.getValue());
764-
} else if (fieldType == FieldType.Country) {
765-
// String
704+
switch (fieldType) {
705+
case STRING:
706+
case MULTIPLEVALUESTRING:
707+
case MULTIPLESTRINGVALUE:
708+
case EXCHANGE:
709+
case LOCALMKTDATE:
710+
case DATA:
711+
case MONTHYEAR:
712+
case DAYOFMONTH:
713+
case COUNTRY:
714+
// String
715+
break;
716+
case INT:
717+
case NUMINGROUP:
718+
case SEQNUM:
719+
case LENGTH:
720+
IntConverter.convert(field.getValue());
721+
break;
722+
case PRICE:
723+
case AMT:
724+
case QTY:
725+
case FLOAT:
726+
case PRICEOFFSET:
727+
case PERCENTAGE:
728+
DoubleConverter.convert(field.getValue());
729+
break;
730+
case BOOLEAN:
731+
BooleanConverter.convert(field.getValue());
732+
break;
733+
case UTCDATE:
734+
UtcDateOnlyConverter.convert(field.getValue());
735+
break;
736+
case UTCTIMEONLY:
737+
UtcTimeOnlyConverter.convert(field.getValue());
738+
break;
739+
case UTCTIMESTAMP:
740+
case TIME:
741+
UtcTimestampConverter.convert(field.getValue());
742+
break;
743+
case CHAR:
744+
if (beginString.compareTo(FixVersions.BEGINSTRING_FIX41) > 0) {
745+
CharConverter.convert(field.getValue());
746+
} // otherwise it's a String, for older FIX versions
747+
break;
766748
}
767749
} catch (final FieldConvertError e) {
768750
throw new IncorrectDataFormat(field.getTag(), field.getValue());

quickfixj-core/src/main/java/quickfix/FieldType.java

Lines changed: 42 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -19,88 +19,66 @@
1919

2020
package quickfix;
2121

22-
import java.util.ArrayList;
2322
import java.util.Date;
24-
import java.util.HashMap;
2523

2624
/**
2725
* A field type enum class.
2826
*/
29-
public class FieldType {
30-
private final int ordinal;
31-
private final String name;
32-
private final Class<?> javaType;
33-
private static final HashMap<String, FieldType> values = new HashMap<String, FieldType>();
34-
private static final ArrayList<FieldType> ordinalToValue = new ArrayList<FieldType>();
27+
public enum FieldType {
3528

36-
private FieldType(String name) {
37-
this(name, String.class);
38-
}
29+
UNKNOWN,
30+
STRING,
31+
CHAR,
32+
PRICE(Double.class),
33+
INT(Integer.class),
34+
AMT(Double.class),
35+
QTY(Double.class),
36+
CURRENCY,
37+
MULTIPLEVALUESTRING,
38+
MULTIPLESTRINGVALUE, // QFJ-881
39+
EXCHANGE,
40+
UTCTIMESTAMP(Date.class),
41+
BOOLEAN(Boolean.class),
42+
LOCALMKTDATE,
43+
DATA,
44+
FLOAT(Double.class),
45+
PRICEOFFSET(Double.class),
46+
MONTHYEAR,
47+
DAYOFMONTH(Integer.class),
48+
UTCDATEONLY(Date.class),
49+
UTCDATE(Date.class),
50+
UTCTIMEONLY(Date.class),
51+
TIME,
52+
NUMINGROUP(Integer.class),
53+
PERCENTAGE(Double.class),
54+
SEQNUM(Integer.class),
55+
LENGTH(Integer.class),
56+
COUNTRY;
3957

40-
private FieldType(String name, Class<?> javaType) {
41-
this.javaType = javaType;
42-
this.name = name;
43-
ordinal = ordinalToValue.size();
44-
ordinalToValue.add(this);
45-
values.put(name, this);
46-
}
58+
private final Class<?> javaType;
4759

48-
public String getName() {
49-
return name;
60+
FieldType(Class<?> javaType) {
61+
this.javaType = javaType;
5062
}
5163

52-
public int getOrdinal() {
53-
return ordinal;
64+
FieldType() {
65+
this(String.class);
5466
}
5567

5668
public Class<?> getJavaType() {
5769
return javaType;
5870
}
5971

60-
public static FieldType fromOrdinal(int ordinal) {
61-
if (ordinal < 0 || ordinal >= ordinalToValue.size()) {
62-
throw new RuntimeError("invalid field type ordinal: " + ordinal);
63-
}
64-
return ordinalToValue.get(ordinal);
65-
}
66-
67-
public static FieldType fromName(String fixVersion, String name) {
68-
FieldType type = values.get(name);
69-
return type != null ? type : FieldType.Unknown;
70-
}
71-
7272
@Override
7373
public String toString() {
74-
return getClass().getSimpleName() + "[" + getName() + "," + getJavaType() + "," + getOrdinal() + "]";
74+
return getClass().getSimpleName() + "[" + name() + "," + javaType + "," + ordinal() + "]";
7575
}
7676

77-
public final static FieldType Unknown = new FieldType("UNKNOWN");
78-
public final static FieldType String = new FieldType("STRING");
79-
public final static FieldType Char = new FieldType("CHAR");
80-
public final static FieldType Price = new FieldType("PRICE", Double.class);
81-
public final static FieldType Int = new FieldType("INT", Integer.class);
82-
public final static FieldType Amt = new FieldType("AMT", Double.class);
83-
public final static FieldType Qty = new FieldType("QTY", Double.class);
84-
public final static FieldType Currency = new FieldType("CURRENCY");
85-
public final static FieldType MultipleValueString = new FieldType("MULTIPLEVALUESTRING");
86-
public final static FieldType MultipleStringValue = new FieldType("MULTIPLESTRINGVALUE"); // QFJ-881
87-
public final static FieldType Exchange = new FieldType("EXCHANGE");
88-
public final static FieldType UtcTimeStamp = new FieldType("UTCTIMESTAMP", Date.class);
89-
public final static FieldType Boolean = new FieldType("BOOLEAN", Boolean.class);
90-
public final static FieldType LocalMktDate = new FieldType("LOCALMKTDATE");
91-
public final static FieldType Data = new FieldType("DATA");
92-
public final static FieldType Float = new FieldType("FLOAT", Double.class);
93-
public final static FieldType PriceOffset = new FieldType("PRICEOFFSET", Double.class);
94-
public final static FieldType MonthYear = new FieldType("MONTHYEAR");
95-
public final static FieldType DayOfMonth = new FieldType("DAYOFMONTH", Integer.class);
96-
public final static FieldType UtcDateOnly = new FieldType("UTCDATEONLY", Date.class);
97-
public final static FieldType UtcDate = new FieldType("UTCDATE", Date.class);
98-
public final static FieldType UtcTimeOnly = new FieldType("UTCTIMEONLY", Date.class);
99-
public final static FieldType Time = new FieldType("TIME");
100-
public final static FieldType NumInGroup = new FieldType("NUMINGROUP", Integer.class);
101-
public final static FieldType Percentage = new FieldType("PERCENTAGE", Double.class);
102-
public final static FieldType SeqNum = new FieldType("SEQNUM", Integer.class);
103-
public final static FieldType Length = new FieldType("LENGTH", Integer.class);
104-
public final static FieldType Country = new FieldType("COUNTRY");
105-
77+
public static FieldType fromName(String fixVersion, String name) {
78+
try {
79+
return FieldType.valueOf(name);
80+
} catch (IllegalArgumentException iae) {
81+
return UNKNOWN;
82+
}
83+
}
10684
}

quickfixj-core/src/test/java/quickfix/DataDictionaryTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public void testDictionary() throws Exception {
7070

7171
assertEquals("wrong field name", "Currency", dd.getFieldName(15));
7272
assertEquals("wrong value description", "BUY", dd.getValueName(4, "B"));
73-
assertEquals("wrong value type", FieldType.String, dd.getFieldTypeEnum(1));
73+
assertEquals("wrong value type", FieldType.STRING, dd.getFieldType(1));
7474
assertEquals("wrong version", FixVersions.BEGINSTRING_FIX44, dd.getVersion());
7575
assertFalse("unexpected field values existence", dd.hasFieldValue(1));
7676
assertTrue("unexpected field values nonexistence", dd.hasFieldValue(4));

quickfixj-core/src/test/java/quickfix/FieldTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,8 +257,8 @@ public void testFieldhashCode() throws Exception {
257257
@Test
258258
public void testMultipleStringValue() throws Exception {
259259

260-
assertEquals(FieldType.MultipleStringValue, FieldType.fromName("notused", "MULTIPLESTRINGVALUE"));
261-
assertEquals(FieldType.MultipleValueString, FieldType.fromName("notused", "MULTIPLEVALUESTRING"));
260+
assertEquals(FieldType.MULTIPLESTRINGVALUE, FieldType.fromName("notused", "MULTIPLESTRINGVALUE"));
261+
assertEquals(FieldType.MULTIPLEVALUESTRING, FieldType.fromName("notused", "MULTIPLEVALUESTRING"));
262262

263263
MarketDataIncrementalRefresh md = new MarketDataIncrementalRefresh();
264264
MarketDataIncrementalRefresh.NoMDEntries value = new MarketDataIncrementalRefresh.NoMDEntries();

0 commit comments

Comments
 (0)