VectorUtil { public static DenseVector parseDense(String str) { if (org.apache.flink.util.StringUtils.isNullOrWhitespaceOnly(str)) { return new DenseVector(); } int len = str.length(); int inDataBuffPos = 0; boolean isInBuff = false; for (int i = 0; i < len; ++i) { char c = str.charAt(i); if (c == ELEMENT_DELIMITER || c == ',') { if (isInBuff) { inDataBuffPos++; } isInBuff = false; } else { isInBuff = true; } } if (isInBuff) { inDataBuffPos++; } double[] data = new double[inDataBuffPos]; int lastestInCharBuffPos = 0; inDataBuffPos = 0; isInBuff = false; for (int i = 0; i < len; ++i) { char c = str.charAt(i); if (c == ELEMENT_DELIMITER || c == ',') { if (isInBuff) { data[inDataBuffPos++] = Double.parseDouble( StringUtils.substring(str, lastestInCharBuffPos, i).trim() ); lastestInCharBuffPos = i + 1; } isInBuff = false; } else { isInBuff = true; } } if (isInBuff) { data[inDataBuffPos] = Double.valueOf( StringUtils.substring(str, lastestInCharBuffPos).trim() ); } return new DenseVector(data); }  static DenseVector parseDense(String str); static SparseVector parseSparse(String str); static String toString(Vector vector); static String toString(SparseVector sparseVector); static String toString(DenseVector denseVector); static Vector getVector(Object obj); static DenseVector getDenseVector(Object obj); static SparseVector getSparseVector(Object obj);  }
@Test public void testParseDenseWithSpace() { DenseVector vec1 = VectorUtil.parseDense("1 2 -3"); DenseVector vec2 = VectorUtil.parseDense(" 1 2 -3 "); DenseVector vec = new DenseVector(new double[]{1, 2, -3}); Assert.assertArrayEquals(vec1.getData(), vec.getData(), 0); Assert.assertArrayEquals(vec2.getData(), vec.getData(), 0); }