Changeset 3241
- Timestamp:
- Apr 12, 2007, 11:30:53 PM (16 years ago)
- Location:
- branches/2.2.3/src/core/net/sf/basedb/util/parser
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/2.2.3/src/core/net/sf/basedb/util/parser/CompoundMapper.java
r2302 r3241 24 24 package net.sf.basedb.util.parser; 25 25 26 import java.text.NumberFormat; 26 27 import java.util.List; 27 28 29 import net.sf.basedb.core.Type; 28 30 import net.sf.basedb.util.parser.FlatFileParser.Data; 29 31 … … 41 43 { 42 44 private final List<Mapper> mappers; 45 private final NumberFormat parser; 43 46 44 47 /** … … 46 49 @param mappers A list of other mappers that will be invoked in 47 50 the order they appear in the list 48 51 */ 49 52 public CompoundMapper(List<Mapper> mappers) 50 53 { 54 this(mappers, null); 55 } 56 57 /** 58 Create a new compound mapper, using a specific number formatter as it's parser. 59 @param mappers A list of other mappers that will be invoked in 60 the order they appear in the list 61 @param parser The number format to use or null to use Float.valueOf() 62 @since 2.2.3 63 */ 64 public CompoundMapper(List<Mapper> mappers, NumberFormat parser) 65 { 51 66 this.mappers = mappers; 67 this.parser = parser; 52 68 } 53 54 69 /* 55 70 From the Mapper interface … … 73 88 { 74 89 String stringValue = getValue(data); 75 return stringValue == null ? null : Integer.parseInt(stringValue); 90 Integer intValue = null; 91 if (stringValue != null) 92 { 93 if (parser != null) 94 { 95 intValue = (Integer)Type.INT.parseString(stringValue, parser); 96 } 97 else 98 { 99 intValue = Integer.parseInt(stringValue); 100 } 101 } 102 return intValue; 76 103 } 77 104 public Float getFloat(Data data) 78 105 { 79 106 String stringValue = getValue(data); 80 return stringValue == null ? null : Float.parseFloat(stringValue); 107 Float floatValue = null; 108 if (stringValue != null) 109 { 110 if (parser != null) 111 { 112 floatValue = (Float)Type.FLOAT.parseString(stringValue, parser); 113 } 114 else 115 { 116 floatValue = Float.parseFloat(stringValue); 117 } 118 } 119 return floatValue; 81 120 } 82 121 // ------------------------------------------- -
branches/2.2.3/src/core/net/sf/basedb/util/parser/ConstantMapper.java
r3023 r3241 27 27 import java.text.ParsePosition; 28 28 29 import net.sf.basedb.core.Type; 29 30 import net.sf.basedb.util.parser.FlatFileParser.Data; 30 31 … … 41 42 { 42 43 private final String constant; 44 private final NumberFormat parser; 45 private boolean needToParseNumeric; 46 private Float asFloat; 43 47 private Integer asInteger; 44 private NumberFormatException integerException; 45 private Float asFloat; 46 private NumberFormatException floatException; 48 private NumberFormatException parseException; 47 49 48 50 /** … … 63 65 { 64 66 this.constant = constant; 65 if (constant != null) 66 { 67 if (parser != null) 68 { 69 ParsePosition pos = new ParsePosition(0); 70 asFloat = parser.parse(constant, pos).floatValue(); 71 if (pos.getIndex() < constant.length()) 72 { 73 asFloat = null; 74 floatException = new NumberFormatException("For input string: \"" + constant + "\""); 75 integerException = floatException; 76 } 77 else 78 { 79 asInteger = asFloat.intValue(); 80 } 81 } 82 else 83 { 84 try 85 { 86 asFloat = Float.valueOf(constant); 87 } 88 catch (NumberFormatException ex) 89 { 90 floatException = ex; 91 } 92 if (asFloat != null) 93 { 94 asInteger = asFloat.intValue(); 95 } 96 else 97 { 98 integerException = floatException; 99 } 100 } 101 } 67 this.parser = parser; 68 this.needToParseNumeric = constant != null; 102 69 } 103 70 … … 107 74 this.asInteger = constant; 108 75 this.asFloat = new Float(constant.floatValue()); 76 this.parser = null; 77 this.needToParseNumeric = false; 109 78 } 110 79 … … 114 83 this.asFloat = constant; 115 84 this.asInteger = new Integer(constant.intValue()); 85 this.parser = null; 86 this.needToParseNumeric = false; 116 87 } 117 88 … … 129 100 public Integer getInt(Data data) 130 101 { 131 if (integerException != null) throw integerException; 102 if (parseException != null) throw parseException; 103 if (needToParseNumeric) parseNumeric(); 132 104 return asInteger; 133 105 } 134 106 public Float getFloat(Data data) 135 107 { 136 if (floatException != null) throw floatException; 108 if (parseException != null) throw parseException; 109 if (needToParseNumeric) parseNumeric(); 137 110 return asFloat; 138 111 } … … 148 121 } 149 122 // ------------------------------------------- 123 124 private void parseNumeric() 125 { 126 assert constant != null : "constant == null"; 127 needToParseNumeric = false; 128 try 129 { 130 if (parser != null) 131 { 132 asFloat = (Float)Type.FLOAT.parseString(constant, parser); 133 } 134 else 135 { 136 asFloat = Float.valueOf(constant); 137 } 138 } 139 catch (NumberFormatException ex) 140 { 141 parseException = ex; 142 } 143 if (asFloat != null) 144 { 145 asInteger = asFloat.intValue(); 146 } 147 } 150 148 } -
branches/2.2.3/src/core/net/sf/basedb/util/parser/FlatFileParser.java
r3023 r3241 871 871 else 872 872 { 873 mapper = new CompoundMapper(mappers );873 mapper = new CompoundMapper(mappers, numberFormat); 874 874 } 875 875 }
Note: See TracChangeset
for help on using the changeset viewer.