sascha@650: package de.intevation.flys.artifacts.model; sascha@650: sascha@650: import java.util.List; sascha@650: import java.util.ArrayList; sascha@650: sascha@650: import java.io.Serializable; sascha@650: sascha@650: import org.apache.log4j.Logger; sascha@650: sascha@650: import gnu.trove.TDoubleArrayList; sascha@650: sascha@655: import de.intevation.flys.utils.DoubleUtil; sascha@655: sascha@650: public class Segment sascha@650: implements Serializable sascha@650: { sascha@650: private static Logger logger = Logger.getLogger(Segment.class); sascha@650: sascha@650: protected double from; sascha@650: protected double to; sascha@650: protected double [] values; sascha@655: protected double [] backup; sascha@655: protected double referencePoint; sascha@650: sascha@650: public Segment() { sascha@650: } sascha@650: sascha@655: public Segment(double referencePoint) { sascha@655: this.referencePoint = referencePoint; sascha@655: } sascha@655: sascha@650: public Segment(double from, double to, double [] values) { sascha@650: this.from = from; sascha@650: this.to = to; sascha@650: this.values = values; sascha@650: } sascha@650: sascha@650: public boolean isUp() { sascha@650: return from < to; sascha@650: } sascha@650: sascha@650: public String toString() { sascha@650: StringBuilder sb = new StringBuilder("Segment: ["); sascha@650: sb.append("from: ").append(from).append("; to: ") sascha@670: .append(to) sascha@670: .append("; ref: ").append(referencePoint) sascha@670: .append("; values: ("); sascha@650: for (int i = 0; i < values.length; ++i) { sascha@650: if (i > 0) sb.append(", "); sascha@650: sb.append(values[i]); sascha@650: } sascha@650: sb.append(")]"); sascha@650: return sb.toString(); sascha@650: } sascha@650: sascha@655: public void setFrom(double from) { sascha@655: this.from = from; sascha@655: } sascha@655: sascha@655: public void backup() { sascha@655: backup = (double [])values.clone(); sascha@655: } sascha@655: sascha@655: public double getFrom() { sascha@655: return from; sascha@655: } sascha@655: sascha@655: public void setTo(double to) { sascha@655: this.to = to; sascha@655: } sascha@655: sascha@655: public double getTo() { sascha@655: return to; sascha@655: } sascha@655: sascha@655: public void setValues(double [] values) { sascha@655: this.values = values; sascha@655: } sascha@655: sascha@655: public double [] getValues() { sascha@655: return values; sascha@655: } sascha@655: sascha@655: public void setReferencePoint(double referencePoint) { sascha@655: this.referencePoint = referencePoint; sascha@655: } sascha@655: sascha@655: public double getReferencePoint() { sascha@655: return referencePoint; sascha@655: } sascha@655: sascha@650: public static List parseSegments(String input) { sascha@650: sascha@650: ArrayList segments = new ArrayList(); sascha@650: sascha@650: TDoubleArrayList vs = new TDoubleArrayList(); sascha@650: sascha@650: for (String segmentStr: input.split(":")) { sascha@650: String [] parts = segmentStr.split(";"); sascha@650: if (parts.length < 3) { sascha@650: logger.warn("invalid segment: '" + segmentStr + "'"); sascha@650: continue; sascha@650: } sascha@650: try { sascha@650: double from = Double.parseDouble(parts[0].trim()); sascha@650: double to = Double.parseDouble(parts[1].trim()); sascha@650: sascha@650: vs.clear(); sascha@650: sascha@650: for (String valueStr: parts[2].split(",")) { sascha@655: vs.add(DoubleUtil.round( sascha@655: Double.parseDouble(valueStr.trim()))); sascha@650: } sascha@650: sascha@650: double [] values = vs.toNativeArray(); sascha@650: segments.add(new Segment(from, to, values)); sascha@650: } sascha@650: catch (NumberFormatException nfe) { sascha@650: logger.warn("invalid segment: '" + segmentStr + "'"); sascha@650: } sascha@650: } sascha@650: sascha@650: return segments; sascha@650: } sascha@650: } sascha@650: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :