view flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/Segment.java @ 3318:dbe2f85bf160

merged flys-artifacts/2.8
author Thomas Arendsen Hein <thomas@intevation.de>
date Fri, 28 Sep 2012 12:14:35 +0200
parents 8124ca1ecaaa
children da7cf0e3ccaa
line wrap: on
line source
package de.intevation.flys.artifacts.model;

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

import java.io.Serializable;

import org.apache.log4j.Logger;

import gnu.trove.TDoubleArrayList;

import de.intevation.flys.utils.DoubleUtil;

public class Segment
implements   Serializable
{
    private static Logger logger = Logger.getLogger(Segment.class);

    protected double    from;
    protected double    to;
    protected double [] values;
    protected double [] backup;
    protected double    referencePoint;

    public Segment() {
    }

    public Segment(double referencePoint) {
        this.referencePoint = referencePoint;
    }

    public Segment(double from, double to, double [] values) {
        this.from   = from;
        this.to     = to;
        this.values = values;
    }

    public boolean isUp() {
        return from < to;
    }

    public String toString() {
        StringBuilder sb = new StringBuilder("Segment: [");
        sb.append("from: ").append(from).append("; to: ")
          .append(to)
          .append("; ref: ").append(referencePoint)
          .append("; values: (");
        for (int i = 0; i < values.length; ++i) {
            if (i > 0) sb.append(", ");
            sb.append(values[i]);
        }
        sb.append(")]");
        return sb.toString();
    }

    public void setFrom(double from) {
        this.from = from;
    }

    public void backup() {
        backup = (double [])values.clone();
    }

    public double getFrom() {
        return from;
    }

    public void setTo(double to) {
        this.to = to;
    }

    public double getTo() {
        return to;
    }

    public void setValues(double [] values) {
        this.values = values;
    }

    public double [] getValues() {
        return values;
    }

    public void setReferencePoint(double referencePoint) {
        this.referencePoint = referencePoint;
    }

    public double getReferencePoint() {
        return referencePoint;
    }

    public static List<Segment> parseSegments(String input) {

        ArrayList<Segment> segments = new ArrayList<Segment>();

        TDoubleArrayList vs = new TDoubleArrayList();

        for (String segmentStr: input.split(":")) {
            String [] parts = segmentStr.split(";");
            if (parts.length < 3) {
                logger.warn("invalid segment: '" + segmentStr + "'");
                continue;
            }
            try {
                double from = Double.parseDouble(parts[0].trim());
                double to   = Double.parseDouble(parts[1].trim());

                vs.clear();

                for (String valueStr: parts[2].split(",")) {
                    vs.add(DoubleUtil.round(
                        Double.parseDouble(valueStr.trim())));
                }

                double [] values = vs.toNativeArray();
                segments.add(new Segment(from, to, values));
            }
            catch (NumberFormatException nfe) {
                logger.warn("invalid segment: '" + segmentStr + "'");
            }
        }

        return segments;
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org