Mercurial > dive4elements > river
comparison backend/src/main/java/org/dive4elements/river/importer/parsers/MeasurementStationsParser.java @ 8989:2693bfaf503d
Fixed several BigDecimal(double) creations by BigDecimal(String) parsing to avoid unnecessary decimal digits
author | mschaefer |
---|---|
date | Mon, 09 Apr 2018 09:07:00 +0200 |
parents | 8fbc0649da13 |
children | c43d8c1a4455 |
comparison
equal
deleted
inserted
replaced
8988:ae76f618d990 | 8989:2693bfaf503d |
---|---|
6 * documentation coming with Dive4Elements River for details. | 6 * documentation coming with Dive4Elements River for details. |
7 */ | 7 */ |
8 | 8 |
9 package org.dive4elements.river.importer.parsers; | 9 package org.dive4elements.river.importer.parsers; |
10 | 10 |
11 import java.math.BigDecimal; | |
12 import java.text.ParseException; | 11 import java.text.ParseException; |
13 import java.util.ArrayList; | 12 import java.util.ArrayList; |
14 import java.util.Date; | 13 import java.util.Date; |
15 import java.util.List; | 14 import java.util.List; |
16 | 15 |
17 import org.apache.log4j.Logger; | 16 import org.apache.log4j.Logger; |
18 | |
19 import org.dive4elements.river.model.MeasurementStation; | |
20 | |
21 import org.dive4elements.river.importer.ImportMeasurementStation; | 17 import org.dive4elements.river.importer.ImportMeasurementStation; |
22 import org.dive4elements.river.importer.ImportRange; | 18 import org.dive4elements.river.importer.ImportRange; |
23 import org.dive4elements.river.importer.ImportTimeInterval; | 19 import org.dive4elements.river.importer.ImportTimeInterval; |
20 import org.dive4elements.river.importer.common.AbstractParser; | |
21 import org.dive4elements.river.model.MeasurementStation; | |
24 | 22 |
25 | 23 |
26 public class MeasurementStationsParser extends LineParser { | 24 public class MeasurementStationsParser extends LineParser { |
27 | 25 |
28 public static class MeasurementStationParserException extends Exception { | 26 public static class MeasurementStationParserException extends Exception { |
29 | 27 |
30 private static final long serialVersionUID = 1L; | 28 private static final long serialVersionUID = 1L; |
31 | 29 |
32 public MeasurementStationParserException(String msg) { | 30 public MeasurementStationParserException(final String msg) { |
33 super(msg); | 31 super(msg); |
34 } | 32 } |
35 } | 33 } |
36 | 34 |
37 public static final int MIN_COLUMNS = 9; | 35 public static final int MIN_COLUMNS = 9; |
38 | 36 |
39 public static final int MAX_COMMENT_LENGTH = 512; | 37 public static final int MAX_COMMENT_LENGTH = 512; |
40 | 38 |
41 private static final Logger log = Logger | 39 private static final Logger log = Logger |
42 .getLogger(MeasurementStationsParser.class); | 40 .getLogger(MeasurementStationsParser.class); |
43 | 41 |
44 private List<ImportMeasurementStation> measurementStations; | 42 private List<ImportMeasurementStation> measurementStations; |
45 private ImportMeasurementStation current; | 43 private ImportMeasurementStation current; |
46 | 44 |
47 @Override | 45 @Override |
48 protected void reset() { | 46 protected void reset() { |
49 this.measurementStations = new ArrayList<ImportMeasurementStation>(); | 47 this.measurementStations = new ArrayList<>(); |
50 } | 48 } |
51 | 49 |
52 @Override | 50 @Override |
53 protected void finish() { | 51 protected void finish() { |
54 } | 52 } |
55 | 53 |
56 @Override | 54 @Override |
57 protected void handleLine(int lineNum, String line) { | 55 protected void handleLine(final int lineNum, final String line) { |
58 if (line == null || line.startsWith(START_META_CHAR)) { | 56 if (line == null || line.startsWith(START_META_CHAR)) { |
59 log.info("skip meta information at line " + lineNum); | 57 log.info("skip meta information at line " + lineNum); |
60 return; | 58 return; |
61 } | 59 } |
62 | 60 |
63 try { | 61 try { |
64 current = new ImportMeasurementStation(); | 62 this.current = new ImportMeasurementStation(); |
65 handleDataLine(lineNum, line); | 63 handleDataLine(lineNum, line); |
66 measurementStations.add(current); | 64 this.measurementStations.add(this.current); |
67 } | 65 } |
68 catch (MeasurementStationParserException e) { | 66 catch (final MeasurementStationParserException e) { |
69 log.warn("Problem in line " + lineNum + ": " + e.getMessage()); | 67 log.warn("Problem in line " + lineNum + ": " + e.getMessage()); |
70 } | 68 } |
71 } | 69 } |
72 | 70 |
73 public List<ImportMeasurementStation> getMeasurementStations() { | 71 public List<ImportMeasurementStation> getMeasurementStations() { |
74 return measurementStations; | 72 return this.measurementStations; |
75 } | 73 } |
76 | 74 |
77 protected void handleDataLine(int lineNum, String line) | 75 protected void handleDataLine(final int lineNum, final String line) |
78 throws MeasurementStationParserException { | 76 throws MeasurementStationParserException { |
79 String[] cols = line.split(SEPERATOR_CHAR); | 77 final String[] cols = line.split(SEPERATOR_CHAR); |
80 | 78 |
81 if (cols == null || cols.length < MIN_COLUMNS) { | 79 if (cols == null || cols.length < MIN_COLUMNS) { |
82 int num = cols != null ? cols.length : 0; | 80 final int num = cols != null ? cols.length : 0; |
83 throw new MeasurementStationParserException("Not enough columns: " | 81 throw new MeasurementStationParserException("Not enough columns: " |
84 + num); | 82 + num); |
85 } | 83 } |
86 | 84 |
87 current.name = getName(cols, lineNum); | 85 this.current.name = getName(cols, lineNum); |
88 current.range = getRange(cols, lineNum); | 86 this.current.range = getRange(cols, lineNum); |
89 current.measurementType = getMeasurementType(cols, lineNum); | 87 this.current.measurementType = getMeasurementType(cols, lineNum); |
90 current.riverside = getRiverside(cols, lineNum); | 88 this.current.riverside = getRiverside(cols, lineNum); |
91 current.gauge = getGauge(cols, lineNum); | 89 this.current.gauge = getGauge(cols, lineNum); |
92 current.observationTimerange = getObservationTimerange(cols, lineNum); | 90 this.current.observationTimerange = getObservationTimerange(cols, lineNum); |
93 current.operator = getOperator(cols, lineNum); | 91 this.current.operator = getOperator(cols, lineNum); |
94 current.comment = getComment(cols, lineNum); | 92 this.current.comment = getComment(cols, lineNum); |
95 } | 93 } |
96 | 94 |
97 protected String getName(String[] cols, int lineNum) | 95 protected String getName(final String[] cols, final int lineNum) |
98 throws MeasurementStationParserException { | 96 throws MeasurementStationParserException { |
99 if (cols[0] == null || cols[0].length() == 0) { | 97 if (cols[0] == null || cols[0].length() == 0) { |
100 throw new MeasurementStationParserException("invalid name in line " | 98 throw new MeasurementStationParserException("invalid name in line " |
101 + lineNum); | 99 + lineNum); |
102 } | 100 } |
103 | 101 |
104 return cols[0]; | 102 return cols[0]; |
105 } | 103 } |
106 | 104 |
107 protected ImportRange getRange(String[] cols, int lineNum) { | 105 protected ImportRange getRange(final String[] cols, final int lineNum) { |
108 String from = cols[1]; | 106 final String from = cols[1]; |
109 String to = cols[4]; | 107 final String to = cols[4]; |
110 if (from == null || from.length() == 0) { | 108 if (from == null || from.length() == 0) { |
111 log.error("No station found in line" + lineNum); | 109 log.error("No station found in line" + lineNum); |
112 return null; | 110 return null; |
113 } | 111 } |
114 | 112 |
115 try { | 113 try { |
116 double lower = getDouble(from); | |
117 | |
118 if (to == null || to.length() == 0) { | 114 if (to == null || to.length() == 0) { |
119 log.warn("No end km found in line " + lineNum); | 115 log.warn("No end km found in line " + lineNum); |
120 return new ImportRange(new BigDecimal(lower)); | 116 return new ImportRange(AbstractParser.parseDecimal(from)); |
121 } | 117 } |
122 | 118 |
123 try { | 119 try { |
124 double upper = getDouble(to); | 120 return new ImportRange(AbstractParser.parseDecimal(from), AbstractParser.parseDecimal(to)); |
125 | 121 } |
126 return new ImportRange(new BigDecimal(lower), | 122 catch (final NumberFormatException e) { |
127 new BigDecimal(upper)); | |
128 } | |
129 catch (ParseException e) { | |
130 log.warn("Unparseable end km in line " + lineNum + | 123 log.warn("Unparseable end km in line " + lineNum + |
131 ". Error: " + e.getMessage()); | 124 ". Error: " + e.getMessage()); |
132 return new ImportRange(new BigDecimal(lower)); | 125 return new ImportRange(AbstractParser.parseDecimal(from)); |
133 } | 126 } |
134 | 127 |
135 } | 128 } |
136 catch (ParseException e) { | 129 catch (final NumberFormatException e) { |
137 log.error("Unparseable station in line " + lineNum + | 130 log.error("Unparseable station in line " + lineNum + |
138 ". Error: " + e.getMessage()); | 131 ". Error: " + e.getMessage()); |
139 return null; | 132 return null; |
140 } | 133 } |
141 } | 134 } |
142 | 135 |
143 protected String getMeasurementType(String[] cols, int lineNum) | 136 protected String getMeasurementType(final String[] cols, final int lineNum) |
144 throws MeasurementStationParserException { | 137 throws MeasurementStationParserException { |
145 String mtype = cols[2].trim(); | 138 final String mtype = cols[2].trim(); |
146 if (!(MeasurementStation.MEASUREMENT_TYPE_BEDLOAD.equals(mtype) || | 139 if (!(MeasurementStation.MEASUREMENT_TYPE_BEDLOAD.equals(mtype) || |
147 MeasurementStation.MEASUREMENT_TYPE_SUSP.equals(mtype))) { | 140 MeasurementStation.MEASUREMENT_TYPE_SUSP.equals(mtype))) { |
148 throw new MeasurementStationParserException( | 141 throw new MeasurementStationParserException( |
149 "invalid measurement type in line " + lineNum); | 142 "invalid measurement type in line " + lineNum); |
150 } | 143 } |
151 | 144 |
152 return mtype; | 145 return mtype; |
153 } | 146 } |
154 | 147 |
155 protected String getRiverside(String[] cols, int lineNum) { | 148 protected String getRiverside(final String[] cols, final int lineNum) { |
156 String col = cols[3]; | 149 final String col = cols[3]; |
157 if (col == null || col.length() == 0) { | 150 if (col == null || col.length() == 0) { |
158 log.warn("No river side given in line " + lineNum); | 151 log.warn("No river side given in line " + lineNum); |
159 } | 152 } |
160 return col; | 153 return col; |
161 } | 154 } |
162 | 155 |
163 protected String getGauge(String[] cols, int lineNum) { | 156 protected String getGauge(final String[] cols, final int lineNum) { |
164 String col = cols[5]; | 157 final String col = cols[5]; |
165 if (col == null || col.length() == 0) { | 158 if (col == null || col.length() == 0) { |
166 log.warn("Invalid gauge found in line " + lineNum); | 159 log.warn("Invalid gauge found in line " + lineNum); |
167 } | 160 } |
168 return col; | 161 return col; |
169 } | 162 } |
170 | 163 |
171 protected ImportTimeInterval getObservationTimerange( | 164 protected ImportTimeInterval getObservationTimerange( |
172 String[] cols, | 165 final String[] cols, |
173 int lineNum | 166 final int lineNum |
174 ) { | 167 ) { |
175 String col = cols[7]; | 168 final String col = cols[7]; |
176 if (col == null || col.length() == 0) { | 169 if (col == null || col.length() == 0) { |
177 log.warn("Observation time invalid in line " + lineNum); | 170 log.warn("Observation time invalid in line " + lineNum); |
178 return null; | 171 return null; |
179 } | 172 } |
180 | 173 |
181 try { | 174 try { |
182 Date date = getDate(col); | 175 final Date date = getDate(col); |
183 | 176 |
184 if (date != null) { | 177 if (date != null) { |
185 return new ImportTimeInterval(date); | 178 return new ImportTimeInterval(date); |
186 } | 179 } |
187 log.warn("Observation time invalid in line " + lineNum); | 180 log.warn("Observation time invalid in line " + lineNum); |
188 } | 181 } |
189 catch (ParseException pe) { | 182 catch (final ParseException pe) { |
190 log.warn("Unparseable observation time '" + col + | 183 log.warn("Unparseable observation time '" + col + |
191 "' in line " + lineNum); | 184 "' in line " + lineNum); |
192 } | 185 } |
193 return null; | 186 return null; |
194 } | 187 } |
195 | 188 |
196 protected String getOperator(String[] cols, int lineNum) { | 189 protected String getOperator(final String[] cols, final int lineNum) { |
197 String col = cols[8]; | 190 final String col = cols[8]; |
198 if (col == null || col.length() == 0) { | 191 if (col == null || col.length() == 0) { |
199 log.warn("No operator given in line " + lineNum); | 192 log.warn("No operator given in line " + lineNum); |
200 } | 193 } |
201 return col; | 194 return col; |
202 } | 195 } |
203 | 196 |
204 protected String getComment(String[] cols, int lineNum) { | 197 protected String getComment(final String[] cols, final int lineNum) { |
205 if (cols.length > MIN_COLUMNS) { | 198 if (cols.length > MIN_COLUMNS) { |
206 String col = cols[9]; | 199 final String col = cols[9]; |
207 if (col.length() > MAX_COMMENT_LENGTH) { | 200 if (col.length() > MAX_COMMENT_LENGTH) { |
208 log.warn("Comment in line " + lineNum + | 201 log.warn("Comment in line " + lineNum + |
209 " longer than allowed " + MAX_COMMENT_LENGTH + | 202 " longer than allowed " + MAX_COMMENT_LENGTH + |
210 " characters. Truncated."); | 203 " characters. Truncated."); |
211 return col.substring(0, MAX_COMMENT_LENGTH); | 204 return col.substring(0, MAX_COMMENT_LENGTH); |
212 } | 205 } |
213 return col; | 206 return col; |
214 } | 207 } |
215 return null; | 208 return null; |