view flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/Segment.java @ 3809:cc83b8e42dbe

Picked rev3347-rev3351 from trunk. flys-artifacts/tags/pre2.6-2011-12-05@3352 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Mon, 05 Dec 2011 09:48:44 +0000
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