Mercurial > dive4elements > river
comparison backend/src/main/java/org/dive4elements/river/importer/parsers/SedimentDensityParser.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 |
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 org.dive4elements.river.importer.ImportDepth; | |
12 import org.dive4elements.river.importer.ImportSedimentDensity; | |
13 import org.dive4elements.river.importer.ImportSedimentDensityValue; | |
14 | |
15 import java.io.File; | 11 import java.io.File; |
16 import java.io.IOException; | 12 import java.io.IOException; |
17 | |
18 import java.math.BigDecimal; | 13 import java.math.BigDecimal; |
19 | |
20 import java.text.NumberFormat; | 14 import java.text.NumberFormat; |
21 import java.text.ParseException; | |
22 | |
23 import java.util.ArrayList; | 15 import java.util.ArrayList; |
24 import java.util.List; | 16 import java.util.List; |
25 | |
26 import java.util.regex.Matcher; | 17 import java.util.regex.Matcher; |
27 import java.util.regex.Pattern; | 18 import java.util.regex.Pattern; |
28 | 19 |
29 import org.apache.log4j.Logger; | 20 import org.apache.log4j.Logger; |
21 import org.dive4elements.river.importer.ImportDepth; | |
22 import org.dive4elements.river.importer.ImportSedimentDensity; | |
23 import org.dive4elements.river.importer.ImportSedimentDensityValue; | |
24 import org.dive4elements.river.importer.common.AbstractParser; | |
30 | 25 |
31 public class SedimentDensityParser extends LineParser { | 26 public class SedimentDensityParser extends LineParser { |
32 | 27 |
33 private static final Logger log = | 28 private static final Logger log = |
34 Logger.getLogger(SedimentDensityParser.class); | 29 Logger.getLogger(SedimentDensityParser.class); |
35 | 30 |
36 public static final NumberFormat nf = | 31 public static final NumberFormat nf = |
37 NumberFormat.getInstance(DEFAULT_LOCALE); | 32 NumberFormat.getInstance(DEFAULT_LOCALE); |
38 | 33 |
39 public static final Pattern META_DEPTH = | 34 public static final Pattern META_DEPTH = |
40 Pattern.compile("^Tiefe: (\\d++)-(\\d++).*"); | 35 Pattern.compile("^Tiefe: (\\d++)-(\\d++).*"); |
41 | 36 |
42 public static final Pattern META_YEAR = | 37 public static final Pattern META_YEAR = |
43 Pattern.compile("^Jahr: (\\d{4}).*"); | 38 Pattern.compile("^Jahr: (\\d{4}).*"); |
44 | 39 |
45 protected List<ImportSedimentDensity> sedimentDensities; | 40 protected List<ImportSedimentDensity> sedimentDensities; |
46 | 41 |
47 protected ImportSedimentDensity current; | 42 protected ImportSedimentDensity current; |
48 | 43 |
49 protected String currentDescription; | 44 protected String currentDescription; |
50 | 45 |
51 protected String yearString; | 46 protected String yearString; |
52 | 47 |
53 public SedimentDensityParser() { | 48 public SedimentDensityParser() { |
54 sedimentDensities = new ArrayList<ImportSedimentDensity>(); | 49 this.sedimentDensities = new ArrayList<>(); |
55 } | 50 } |
56 | 51 |
57 | 52 |
58 @Override | 53 @Override |
59 public void parse(File file) throws IOException { | 54 public void parse(final File file) throws IOException { |
60 currentDescription = file.getName(); | 55 this.currentDescription = file.getName(); |
61 | 56 |
62 super.parse(file); | 57 super.parse(file); |
63 } | 58 } |
64 | 59 |
65 | 60 |
66 @Override | 61 @Override |
67 protected void reset() { | 62 protected void reset() { |
68 current = new ImportSedimentDensity(currentDescription); | 63 this.current = new ImportSedimentDensity(this.currentDescription); |
69 } | 64 } |
70 | 65 |
71 | 66 |
72 @Override | 67 @Override |
73 protected void finish() { | 68 protected void finish() { |
74 if (current != null) { | 69 if (this.current != null) { |
75 sedimentDensities.add(current); | 70 this.sedimentDensities.add(this.current); |
76 } | 71 } |
77 } | 72 } |
78 | 73 |
79 | 74 |
80 @Override | 75 @Override |
81 protected void handleLine(int lineNum, String line) { | 76 protected void handleLine(final int lineNum, final String line) { |
82 if (line.startsWith(START_META_CHAR)) { | 77 if (line.startsWith(START_META_CHAR)) { |
83 handleMetaLine(stripMetaLine(line)); | 78 handleMetaLine(stripMetaLine(line)); |
84 } | 79 } |
85 else { | 80 else { |
86 handleDataLine(line); | 81 handleDataLine(line); |
87 } | 82 } |
88 } | 83 } |
89 | 84 |
90 | 85 |
91 protected void handleMetaLine(String line) { | 86 protected void handleMetaLine(final String line) { |
92 if (handleMetaDepth(line)) { | 87 if (handleMetaDepth(line)) { |
93 return; | 88 return; |
94 } | 89 } |
95 if (handleMetaYear(line)) { | 90 if (handleMetaYear(line)) { |
96 return; | 91 return; |
97 } | 92 } |
98 log.warn("Unknown meta line: '" + line + "'"); | 93 log.warn("Unknown meta line: '" + line + "'"); |
99 } | 94 } |
100 | 95 |
101 | 96 |
102 protected boolean handleMetaDepth(String line) { | 97 protected boolean handleMetaDepth(final String line) { |
103 Matcher m = META_DEPTH.matcher(line); | 98 final Matcher m = META_DEPTH.matcher(line); |
104 | 99 |
105 if (m.matches()) { | 100 if (m.matches()) { |
106 String lo = m.group(1); | 101 final String lo = m.group(1); |
107 String up = m.group(2); | 102 final String up = m.group(2); |
108 | 103 |
109 log.info("Found sediment density depth: " | 104 log.info("Found sediment density depth: " |
110 + lo + " - " + up + " cm"); | 105 + lo + " - " + up + " cm"); |
111 | 106 |
112 try { | 107 try { |
113 ImportDepth depth = new ImportDepth( | 108 final ImportDepth depth = new ImportDepth( |
114 new BigDecimal(nf.parse(lo).doubleValue()), | 109 AbstractParser.parseDecimal(lo), |
115 new BigDecimal(nf.parse(up).doubleValue()) | 110 AbstractParser.parseDecimal(up)); |
116 ); | |
117 | 111 |
118 current.setDepth(depth); | 112 this.current.setDepth(depth); |
119 | 113 |
120 return true; | 114 return true; |
121 } | 115 } |
122 catch (ParseException pe) { | 116 catch (final NumberFormatException pe) { |
123 log.warn("Unparseable numbers in: '" + line + "'"); | 117 log.warn("Unparseable numbers in: '" + line + "'"); |
124 } | 118 } |
125 } | 119 } |
126 else { | 120 else { |
127 log.debug("Meta line doesn't contain depth information: " + line); | 121 log.debug("Meta line doesn't contain depth information: " + line); |
128 } | 122 } |
129 | 123 |
130 return false; | 124 return false; |
131 } | 125 } |
132 | 126 |
133 protected boolean handleMetaYear(String line) { | 127 protected boolean handleMetaYear(final String line) { |
134 Matcher m = META_YEAR.matcher(line); | 128 final Matcher m = META_YEAR.matcher(line); |
135 | 129 |
136 if (m.matches()) { | 130 if (m.matches()) { |
137 yearString = m.group(1); | 131 this.yearString = m.group(1); |
138 | 132 |
139 log.info("Found sediment density year: " + yearString); | 133 log.info("Found sediment density year: " + this.yearString); |
140 | 134 |
141 return true; | 135 return true; |
142 } | 136 } |
143 | 137 |
144 log.debug("Meta line doesn't contain year: " + line); | 138 log.debug("Meta line doesn't contain year: " + line); |
145 | 139 |
146 return false; | 140 return false; |
147 } | 141 } |
148 | 142 |
149 | 143 |
150 protected void handleDataLine(String line) { | 144 protected void handleDataLine(final String line) { |
151 String[] vals = line.split(SEPERATOR_CHAR); | 145 final String[] vals = line.split(SEPERATOR_CHAR); |
152 | 146 |
153 if (vals == null || vals.length < 3) { | 147 if (vals == null || vals.length < 3) { |
154 log.warn("skip invalid data line: '" + line + "'"); | 148 log.warn("skip invalid data line: '" + line + "'"); |
155 return; | 149 return; |
156 } | 150 } |
157 | 151 |
158 BigDecimal km = null; | 152 BigDecimal km = null; |
159 BigDecimal shoreOffset = null; | 153 BigDecimal shoreOffset = null; |
160 BigDecimal density = null; | 154 BigDecimal density = null; |
161 try { | 155 try { |
162 km = new BigDecimal(nf.parse(vals[0]).doubleValue()); | 156 km = AbstractParser.parseDecimal(vals[0]); |
163 density = new BigDecimal(nf.parse(vals[2]).doubleValue()); | 157 density = AbstractParser.parseDecimal(vals[2]); |
164 if (!vals[1].isEmpty()) { | 158 if (!vals[1].isEmpty()) { |
165 shoreOffset = new BigDecimal(nf.parse(vals[1]).doubleValue()); | 159 shoreOffset = AbstractParser.parseDecimal(vals[1]); |
166 } | 160 } |
167 } | 161 } |
168 catch (ParseException pe) { | 162 catch (final NumberFormatException pe) { |
169 log.warn("Unparseable numbers in '" + line + "'"); | 163 log.warn("Unparseable numbers in '" + line + "'"); |
170 } | 164 } |
171 | 165 |
172 if (km == null || density == null) { | 166 if (km == null || density == null) { |
173 log.warn("SDP: No km nor density given. Skip line"); | 167 log.warn("SDP: No km nor density given. Skip line"); |
174 return; | 168 return; |
175 } | 169 } |
176 | 170 |
177 BigDecimal year = null; | 171 BigDecimal year = null; |
178 if (yearString != null) { | 172 if (this.yearString != null) { |
179 try { | 173 try { |
180 year = new BigDecimal(nf.parse(yearString).doubleValue()); | 174 year = AbstractParser.parseDecimal(this.yearString); |
181 } | 175 } |
182 catch (ParseException pe) { | 176 catch (final NumberFormatException pe) { |
183 log.warn("Unparseable year string"); | 177 log.warn("Unparseable year string"); |
184 } | 178 } |
185 } | 179 } |
186 | 180 |
187 current.addValue(new ImportSedimentDensityValue( | 181 this.current.addValue(new ImportSedimentDensityValue( |
188 km, | 182 km, |
189 shoreOffset, | 183 shoreOffset, |
190 density, | 184 density, |
191 year, | 185 year, |
192 currentDescription)); | 186 this.currentDescription)); |
193 } | 187 } |
194 | 188 |
195 | 189 |
196 public List<ImportSedimentDensity> getSedimentDensities() { | 190 public List<ImportSedimentDensity> getSedimentDensities() { |
197 return sedimentDensities; | 191 return this.sedimentDensities; |
198 } | 192 } |
199 } | 193 } |
200 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : | 194 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |