LaserMeasurement { @NonNull public static List<LaserMeasurement> parse(@NonNull String pointString, boolean metric, boolean strict) throws ParseException { final List<LaserMeasurement> points = new ArrayList<>(); final String[] lines = pointString.split("\n"); final double units = metric ? 1 : Convert.FT; for (int i = 0; i < lines.length; i++) { final String line = lines[i]; final String[] row = line.trim().split("[, \t/]+", -1); if (row.length == 2) { try { final double x = Double.parseDouble(row[0]) * units; final double y = Double.parseDouble(row[1]) * units; if (Numbers.isReal(x) && Numbers.isReal(y)) { points.add(new LaserMeasurement(x, y)); } else { Log.w(TAG, "Laser measurements must be real " + x + " " + y); if (strict) { throw new ParseException("Invalid measurement", i + 1); } } } catch (NumberFormatException e) { Log.w(TAG, "Error parsing laser profile " + e); if (strict) { throw new ParseException("Invalid measurement", i + 1); } } } else { if (strict) { throw new ParseException("Invalid measurement", i + 1); } } } return points; } LaserMeasurement(double x, double y); @NonNull @Override String toString(); @NonNull static List<LaserMeasurement> parse(@NonNull String pointString, boolean metric, boolean strict); @NonNull static List<LaserMeasurement> parseSafe(@NonNull String pointString, boolean metric); @NonNull static CharSequence render(@NonNull List<LaserMeasurement> points, boolean metric); @NonNull static List<LaserMeasurement> reorder(@NonNull List<LaserMeasurement> points); final double x; final double y; }
@Test public void parseErrors() throws ParseException { assertEquals(0, LaserMeasurement.parse("100,NaN", true, false).size()); assertEquals(0, LaserMeasurement.parse("100,Infinity", true, false).size()); assertEquals(0, LaserMeasurement.parse("100,-Infinity", true, false).size()); assertEquals(0, LaserMeasurement.parse("100,-100,", true, false).size()); assertEquals(0, LaserMeasurement.parse("100,-100,333", true, false).size()); assertEquals(1, LaserMeasurement.parse("100,-100\nXXXX", true, false).size()); assertEquals(0, LaserMeasurement.parse("100,ZZZ", true, false).size()); assertEquals(0, LaserMeasurement.parse("100p 200q", true, false).size()); assertEquals(0, LaserMeasurement.parse("100 200ft", true, false).size()); assertEquals(0, LaserMeasurement.parse("100 200m", false, false).size()); assertEquals(0, LaserMeasurement.parse("100 200mm", true, false).size()); assertEquals(0, LaserMeasurement.parse("100 200mm", false, false).size()); }
@Test public void parseMetric() throws ParseException { assertEquals(0, LaserMeasurement.parse("", true, false).size()); assertEquals(0, LaserMeasurement.parse(" ", true, false).size()); assertEquals(0, LaserMeasurement.parse("\n", true, false).size()); assertEquals(0, LaserMeasurement.parse("\n\n", true, false).size()); assertEquals(1, LaserMeasurement.parse("100 -100", true, true).size()); assertEquals(1, LaserMeasurement.parse("100,-100", true, true).size()); assertEquals(1, LaserMeasurement.parse("100, -100", true, true).size()); assertEquals(1, LaserMeasurement.parse("100\t-100", true, true).size()); assertEquals(1, LaserMeasurement.parse("100/-100", true, true).size()); assertEquals(1, LaserMeasurement.parse(" 100, -100 ", true, true).size()); assertEquals(2, LaserMeasurement.parse("100,-100\n20.0, -50.0", true, true).size()); assertEquals(2, LaserMeasurement.parse("100,-100\n20.0, -50.0\n", true, true).size()); assertEquals(2, LaserMeasurement.parse("100 -100\n20.0 -50.0\n", true, true).size()); assertEquals(2, LaserMeasurement.parse("100 -100\n 20.0 -50.0 ", true, true).size()); }
@Test public void parseFreedom() throws ParseException { assertEquals(1, LaserMeasurement.parse("100 200", false, true).size()); assertEquals(2, LaserMeasurement.parse("100 200\n20.0 -50.0", false, true).size()); }