view flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/Segment.java @ 3426:e7a67407dea1

Limited the input of time periods in SQ relation calculation to 1. flys-artifacts/trunk@5082 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Fri, 20 Jul 2012 09:34:47 +0000
parents da7cf0e3ccaa
children cb11919cccf9
line wrap: on
line source
package de.intevation.flys.artifacts.model;

import de.intevation.flys.utils.DoubleUtil;

import gnu.trove.TDoubleArrayList;

import java.io.Serializable;

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

import org.apache.log4j.Logger;

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;
    }

    @Override
    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 = values != null
            ? (double [])values.clone()
            : null;
    }

    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