MATSIM
CoordArrayConverter.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 
22 import org.apache.logging.log4j.LogManager;
23 import org.matsim.api.core.v01.Coord;
25 
26 public class CoordArrayConverter implements AttributeConverter<Coord[]> {
27 
28  // [(X1;Y1),(X2;Y2),...,(Xn;Yn)]
29 
30  private static final String DELIMITER_COORD = ";";
31  private static final String BRACER_COORD = "()";
32  private static final String DELIMITER_ARRAY = ",";
33  private static final String BRACER_ARRAY = "[]";
34 
35  private static final String BRACER_COORD_BEGIN = BRACER_COORD.substring(0, 1);
36  private static final String BRACER_COORD_END = BRACER_COORD.substring(1, 2);
37  private static final String BRACER_ARRAY_BEGIN = BRACER_ARRAY.substring(0, 1);
38  private static final String BRACER_ARRAY_END = BRACER_ARRAY.substring(1, 2);
39 
40  @Override
41  public Coord[] convert(String value) {
42  value = value.replace(BRACER_ARRAY_BEGIN, "");
43  value = value.replace(BRACER_ARRAY_END, "");
44  String[] values = value.split(DELIMITER_ARRAY);
45  Coord[] result = new Coord[values.length];
46  for (int i = 0; i < values.length; i++) {
47  String s = values[i].replace(BRACER_COORD_BEGIN, "");
48  s = s.replace(BRACER_COORD_END, "");
49  String[] sa = s.split(DELIMITER_COORD);
50  result[i] = new Coord(Double.parseDouble(sa[0]), Double.parseDouble(sa[1]));
51  }
52  return result;
53  }
54 
55  @Override
56  public String convertToString(Object o) {
57  if (!(o instanceof Coord[])) {
58  LogManager.getLogger(getClass()).error("Object is not of type Coord[] " + o.getClass().toString());
59  return null;
60  }
61  Coord[] c = (Coord[]) o;
62  StringBuilder result = new StringBuilder();
63  result.append(BRACER_ARRAY_BEGIN);
64  for (int i = 0; i < c.length; i++) {
65  if (i > 0) {
66  result.append(DELIMITER_ARRAY);
67  }
68  result.append(String.format(
69  BRACER_COORD_BEGIN+"%s"+DELIMITER_COORD+"%s"+BRACER_COORD_END,
70  Double.toString(c[i].getX()),
71  Double.toString(c[i].getY())
72  ));
73  }
74  result.append(BRACER_ARRAY_END);
75  return result.toString();
76  }
77 }