MATSIM
DoubleArrayConverter.java
Go to the documentation of this file.
1 /* *********************************************************************** *
2  * project: org.matsim.*
3  * *
4  * *********************************************************************** *
5  * *
6  * copyright : (C) 2016 by the members listed in the COPYING, *
7  * LICENSE and WARRANTY file. *
8  * email : info at matsim dot org *
9  * *
10  * *********************************************************************** *
11  * *
12  * This program is free software; you can redistribute it and/or modify *
13  * it under the terms of the GNU General Public License as published by *
14  * the Free Software Foundation; either version 2 of the License, or *
15  * (at your option) any later version. *
16  * See also COPYING, LICENSE and WARRANTY file *
17  * *
18  * *********************************************************************** */
19 
20 package org.matsim.utils.objectattributes.attributeconverters;/*
21  * created by jbischoff, 22.08.2018
22  */
23 
24 import org.apache.logging.log4j.LogManager;
26 
27 public class DoubleArrayConverter implements AttributeConverter<double[]> {
28 
29  private static final String DELIMITER = ",";
30 
31  @Override
32  public double[] convert(String value) {
33  String[] values = value.split(DELIMITER);
34  double[] result = new double[values.length];
35  for (int i = 0; i < values.length; i++) {
36  result[i] = Double.parseDouble(values[i]);
37  }
38  return result;
39  }
40 
41  @Override
42  public String convertToString(Object o) {
43  if (!(o instanceof double[])) {
44  LogManager.getLogger(getClass()).error("Object is not of type double[] " + o.getClass().toString());
45  return null;
46  }
47  double[] s = (double[]) o;
48  StringBuilder result = new StringBuilder();
49  for (int i = 0; i < s.length; i++) {
50  if (i > 0) {
51  result.append(DELIMITER);
52  }
53  result.append(s[i]);
54  }
55  return result.toString();
56  }
57 }