Mercurial > dive4elements > river
diff backend/src/main/java/org/dive4elements/river/importer/parsers/WaterlevelParser.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 | 5e38e2924c07 |
children | c43d8c1a4455 |
line wrap: on
line diff
--- a/backend/src/main/java/org/dive4elements/river/importer/parsers/WaterlevelParser.java Sun Apr 08 18:09:32 2018 +0200 +++ b/backend/src/main/java/org/dive4elements/river/importer/parsers/WaterlevelParser.java Mon Apr 09 09:07:00 2018 +0200 @@ -12,23 +12,21 @@ import java.io.IOException; import java.math.BigDecimal; import java.text.NumberFormat; -import java.text.ParseException; import java.util.ArrayList; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; import org.apache.log4j.Logger; - +import org.dive4elements.river.backend.utils.DateUtil; +import org.dive4elements.river.importer.ImportRange; import org.dive4elements.river.importer.ImportTimeInterval; import org.dive4elements.river.importer.ImportUnit; - -import org.dive4elements.river.importer.ImportRange; import org.dive4elements.river.importer.ImportWst; import org.dive4elements.river.importer.ImportWstColumn; import org.dive4elements.river.importer.ImportWstColumnValue; import org.dive4elements.river.importer.ImportWstQRange; -import org.dive4elements.river.backend.utils.DateUtil; +import org.dive4elements.river.importer.common.AbstractParser; /** @@ -43,17 +41,17 @@ private static final Logger log = Logger.getLogger(WaterlevelParser.class); private static final NumberFormat nf = - NumberFormat.getInstance(DEFAULT_LOCALE); + NumberFormat.getInstance(DEFAULT_LOCALE); private static final Pattern META_Q_RANGE = - Pattern.compile("Abfluss\\s\\[(.*)\\];(.*)"); + Pattern.compile("Abfluss\\s\\[(.*)\\];(.*)"); public static final Pattern META_UNIT = - Pattern.compile("^Einheit: \\[(.*)\\].*"); + Pattern.compile("^Einheit: \\[(.*)\\].*"); - public static final double INTERVAL_GAP = 0.00001d; + public static final BigDecimal INTERVAL_GAP = new BigDecimal("0.00001"); - private List<ImportWst> waterlevels; + private final List<ImportWst> waterlevels; private ImportWst current; @@ -70,18 +68,18 @@ public WaterlevelParser() { - waterlevels = new ArrayList<ImportWst>(); + this.waterlevels = new ArrayList<>(); } public List<ImportWst> getWaterlevels() { - return waterlevels; + return this.waterlevels; } @Override - public void parse(File file) throws IOException { - currentDescription = file.getName(); + public void parse(final File file) throws IOException { + this.currentDescription = file.getName(); super.parse(file); } @@ -89,64 +87,63 @@ @Override protected void reset() { - currentQRange = null; - current = new ImportWst(currentDescription); - current.setNumberColumns(1); - column = current.getColumn(0); - column.setName(currentDescription); - column.setDescription(currentDescription); + this.currentQRange = null; + this.current = new ImportWst(this.currentDescription); + this.current.setNumberColumns(1); + this.column = this.current.getColumn(0); + this.column.setName(this.currentDescription); + this.column.setDescription(this.currentDescription); // Try to extract and set the TimeInterval. - Matcher m = WaterlevelDifferencesParser.YEARS_IN_COLUMN.matcher( - currentDescription); + final Matcher m = WaterlevelDifferencesParser.YEARS_IN_COLUMN.matcher( + this.currentDescription); if (m.matches()) { - int startYear = Integer.parseInt(m.group(1)); - int endYear = Integer.parseInt(m.group(2)); - ImportTimeInterval time = new ImportTimeInterval( - DateUtil.getStartDateFromYear(startYear), - DateUtil.getEndDateFromYear(endYear) - ); - column.setTimeInterval(time); + final int startYear = Integer.parseInt(m.group(1)); + final int endYear = Integer.parseInt(m.group(2)); + final ImportTimeInterval time = new ImportTimeInterval( + DateUtil.getStartDateFromYear(startYear), + DateUtil.getEndDateFromYear(endYear) + ); + this.column.setTimeInterval(time); } else { log.debug("No time interval in column header found: " - + currentDescription); + + this.currentDescription); } - current.setKind(7); + this.current.setKind(7); } @Override protected void finish() { - if (current != null) { - if (currentQRange != null) { - List<ImportWstColumnValue> cValues = column.getColumnValues(); + if (this.current != null) { + if (this.currentQRange != null) { + final List<ImportWstColumnValue> cValues = this.column.getColumnValues(); // Set end of range to last station // or expand range to minimal length in case it would be 0 // TODO: should otherwise be extended to // (first station of next range - INTERVAL_GAP), // assuming always ascending stations - BigDecimal lastStation = cValues.get(cValues.size() -1) - .getPosition(); - if (lastStation.compareTo(currentRange.getA()) == 0) { - currentRange.setB(new BigDecimal(lastStation.doubleValue() - + INTERVAL_GAP)); + final BigDecimal lastStation = cValues.get(cValues.size() -1) + .getPosition(); + if (lastStation.compareTo(this.currentRange.getA()) == 0) { + this.currentRange.setB(lastStation.add(INTERVAL_GAP)); } else { - currentRange.setB(lastStation); + this.currentRange.setB(lastStation); } - currentQRange.setRange(currentRange); - column.addColumnQRange(currentQRange); + this.currentQRange.setRange(this.currentRange); + this.column.addColumnQRange(this.currentQRange); } - waterlevels.add(current); + this.waterlevels.add(this.current); } } @Override - protected void handleLine(int lineNum, String line) { + protected void handleLine(final int lineNum, final String line) { if (line.startsWith(START_META_CHAR)) { handleMetaLine(stripMetaLine(line)); return; @@ -161,44 +158,44 @@ } - private void handleMetaLine(String meta) { - Matcher m = META_UNIT.matcher(meta); + private void handleMetaLine(final String meta) { + final Matcher m = META_UNIT.matcher(meta); if (m.matches()) { - String unit = m.group(1); + final String unit = m.group(1); log.debug("Found unit: '" + unit + "'"); - current.setUnit(new ImportUnit(unit)); + this.current.setUnit(new ImportUnit(unit)); } } - private boolean handleQRange(String line) { - Matcher m = META_Q_RANGE.matcher(line); + private boolean handleQRange(final String line) { + final Matcher m = META_Q_RANGE.matcher(line); if (m.matches()) { - String unitStr = m.group(1); - String valueStr = m.group(2); + final String unitStr = m.group(1); + final String valueStr = m.group(2); try { - if (currentQRange != null) { + if (this.currentQRange != null) { // Finish off the last one. - List<ImportWstColumnValue> cValues = column - .getColumnValues(); + final List<ImportWstColumnValue> cValues = this.column + .getColumnValues(); // Set end of range to last station. - currentRange.setB(cValues.get(cValues.size() -1) - .getPosition()); - currentQRange.setRange(currentRange); - column.addColumnQRange(currentQRange); + this.currentRange.setB(cValues.get(cValues.size() -1) + .getPosition()); + this.currentQRange.setRange(this.currentRange); + this.column.addColumnQRange(this.currentQRange); } - currentQRange = new ImportWstQRange(null, - new BigDecimal(nf.parse(valueStr).doubleValue())); - currentRange = new ImportRange(); + this.currentQRange = new ImportWstQRange(null, + AbstractParser.parseDecimal(valueStr)); + this.currentRange = new ImportRange(); log.debug("Found new Q range: Q=" + valueStr); return true; } - catch (ParseException pe) { + catch (final NumberFormatException pe) { log.warn("Unparseable Q range: '" + line + "'"); } } @@ -207,8 +204,8 @@ } - private void handleDataLine(String line) { - String[] cols = line.split(SEPERATOR_CHAR); + private void handleDataLine(final String line) { + final String[] cols = line.split(SEPERATOR_CHAR); if (cols == null || cols.length < 2) { log.warn("skip invalid waterlevel line: '" + line + "'"); @@ -217,18 +214,16 @@ try { // Store the value and remember the position for QRange, if needed. - Double station = nf.parse(cols[0]).doubleValue(); - Double value = nf.parse(cols[1]).doubleValue(); + final BigDecimal station = AbstractParser.parseDecimal(cols[0]); + final BigDecimal value = AbstractParser.parseDecimal(cols[1]); - BigDecimal stationBD = new BigDecimal(station); + this.column.addColumnValue(station, value); - column.addColumnValue(stationBD, new BigDecimal(value)); - - if (currentRange.getA() == null) { - currentRange.setA(stationBD); + if (this.currentRange.getA() == null) { + this.currentRange.setA(station); } } - catch (ParseException pe) { + catch (final NumberFormatException pe) { log.warn("Unparseable number in data row: " + line); } }