All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.opentripplanner.api.model.ElevationAdapter Maven / Gradle / Ivy

package org.opentripplanner.api.model;

import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.annotation.adapters.XmlAdapter;

import org.opentripplanner.common.model.P2;

public class ElevationAdapter extends XmlAdapter>> {
    @Override
    public String marshal(List> pairs) throws Exception {
        if (pairs == null)
            return null;
        StringBuilder str = new StringBuilder();
        for (P2 pair : pairs) {
            str.append(Math.round(pair.first));
            str.append(",");
            str.append(Math.round(pair.second * 10.0) / 10.0);
            str.append(",");
        }
        if (str.length() > 0) 
            str.setLength(str.length() - 1);
        return str.toString();
    }

    @Override
    public List> unmarshal(String data) throws Exception {
        if (data == null)
            return null;
        String[] values = data.split(",");
        ArrayList> out = new ArrayList>();
        for (int i = 0; i < values.length; i += 2) {
            P2 value = new P2(Double.parseDouble(values[i]),
                    Double.parseDouble(values[i + 1]));
            out.add(value);
        }
        return out;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy