Skip to content

Commit

Permalink
fix range parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
SteffenHeu committed Mar 9, 2022
1 parent 5289a84 commit d9d56db
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 20 deletions.
30 changes: 14 additions & 16 deletions src/main/java/io/github/mzmine/util/ParsingUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -157,27 +157,25 @@ public static String rangeToString(Range<Comparable<?>> range) {
}

public static Range<Double> stringToDoubleRange(String str) {
Pattern regex = Pattern.compile(
"\\[([+-]?([0-9]*[.])?[0-9]+)" + SEPARATOR + "([+-]?([0-9]*[.])?[0-9]+)\\]");
Matcher matcher = regex.matcher(str);
if (matcher.matches()) {
double lower = Double.parseDouble(matcher.group(1));
double upper = Double.parseDouble(matcher.group(3));
return Range.closed(lower, upper);
String[] vals = str.replaceAll("\\[", "").replaceAll("\\]", "").split(SEPARATOR);
assert vals.length == 2;
try {
return Range.closed(Double.parseDouble(vals[0]), Double.parseDouble(vals[1]));
} catch (NumberFormatException e) {
logger.log(Level.WARNING, "Error parsing double range from string " + str, e);
return null;
}
return null;
}

public static Range<Float> stringToFloatRange(String str) {
Pattern regex = Pattern.compile(
"\\[([+-]?([0-9]*[.])?[0-9]+)" + SEPARATOR + "([+-]?([0-9]*[.])?[0-9]+)\\]");
Matcher matcher = regex.matcher(str);
if (matcher.matches()) {
float lower = Float.parseFloat(matcher.group(1));
float upper = Float.parseFloat(matcher.group(3));
return Range.closed(lower, upper);
String[] vals = str.replaceAll("\\[", "").replaceAll("\\]", "").split(SEPARATOR);
assert vals.length == 2;
try {
return Range.closed(Float.parseFloat(vals[0]), Float.parseFloat(vals[1]));
} catch (NumberFormatException e) {
logger.log(Level.WARNING, "Error parsing float range from string " + str, e);
return null;
}
return null;
}

public static Range<Integer> parseIntegerRange(String str) {
Expand Down
39 changes: 39 additions & 0 deletions src/test/java/util/ParsingUtilTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package util;

import com.google.common.collect.Range;
import io.github.mzmine.util.ParsingUtils;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

public class ParsingUtilTest {

@Test
void testStringToDoubleRange() {
final Range<Double> range1 = Range.closed(5.345, 17.32E10);
final Range<Double> range2 = Range.closed(-0.3347E-10, 0.3348);

final String string1 = ParsingUtils.rangeToString((Range) range1);
final String string2 = ParsingUtils.rangeToString((Range) range2);

Assertions.assertEquals("[5.345;1.732E11]", string1);
Assertions.assertEquals("[-3.347E-11;0.3348]", string2);

Assertions.assertEquals(range1, ParsingUtils.stringToDoubleRange(string1));
Assertions.assertEquals(range2, ParsingUtils.stringToDoubleRange(string2));
}

@Test
void testStringToFloatRange() {
final Range<Float> range1 = Range.closed(5.345f, 17.32E10f);
final Range<Float> range2 = Range.closed(-0.3347E-10f, 0.3348f);

final String string1 = ParsingUtils.rangeToString((Range) range1);
final String string2 = ParsingUtils.rangeToString((Range) range2);

Assertions.assertEquals("[5.345;1.73199999E11]", string1);
Assertions.assertEquals("[-3.347E-11;0.3348]", string2);

Assertions.assertEquals(range1, ParsingUtils.stringToFloatRange(string1));
Assertions.assertEquals(range2, ParsingUtils.stringToFloatRange(string2));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* USA
*/

package lipidannotationtest;
package util.lipidannotationtest;

import io.github.mzmine.datamodel.IonizationType;
import io.github.mzmine.modules.dataprocessing.id_lipididentification.lipids.ILipidAnnotation;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* USA
*/

package lipidannotationtest;
package util.lipidannotationtest;

import io.github.mzmine.datamodel.IonizationType;
import io.github.mzmine.modules.dataprocessing.id_lipididentification.lipids.LipidClasses;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* USA
*/

package lipidannotationtest;
package util.lipidannotationtest;

import java.util.HashSet;
import java.util.Set;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* USA
*/

package lipidannotationtest;
package util.lipidannotationtest;

import static org.junit.jupiter.api.Assertions.assertEquals;
import java.text.DecimalFormat;
Expand Down

0 comments on commit d9d56db

Please sign in to comment.