ingo@361: package de.intevation.flys.artifacts.model; ingo@361: ingo@361: import java.io.Serializable; ingo@365: ingo@365: import gnu.trove.TDoubleArrayList; ingo@361: ingo@402: import org.apache.log4j.Logger; ingo@402: ingo@361: ingo@361: /** ingo@361: * This class represents a pool of data triples that consists of 'W', 'Q' and ingo@361: * 'KM' data. ingo@361: * ingo@361: * @author Ingo Weinzierl ingo@361: */ ingo@361: public class WQKms implements Serializable { ingo@361: ingo@402: private static Logger logger = Logger.getLogger(WQKms.class); ingo@402: ingo@402: ingo@361: /** The array that contains the 'W' values.*/ ingo@365: protected TDoubleArrayList w; ingo@361: ingo@361: /** The array that contains the 'Q' values.*/ ingo@365: protected TDoubleArrayList q; ingo@361: ingo@361: /** The array that contains the 'KMs' values.*/ ingo@365: protected TDoubleArrayList kms; ingo@361: ingo@361: ingo@361: public WQKms() { ingo@365: this.w = new TDoubleArrayList(); ingo@365: this.q = new TDoubleArrayList(); ingo@365: this.kms = new TDoubleArrayList(); ingo@361: } ingo@361: sascha@380: public WQKms(int capacity) { sascha@380: this.w = new TDoubleArrayList(capacity); sascha@380: this.q = new TDoubleArrayList(capacity); sascha@380: this.kms = new TDoubleArrayList(capacity); sascha@380: } sascha@380: ingo@361: ingo@402: public WQKms(double[] kms, double[] qs, double[] ws) { ingo@402: this.w = new TDoubleArrayList(ws); ingo@402: this.q = new TDoubleArrayList(qs); ingo@402: this.kms = new TDoubleArrayList(kms); ingo@402: } ingo@402: ingo@402: ingo@361: /** ingo@361: * Adds a new row to this data pool. ingo@361: * ingo@361: * @param w a W. ingo@361: * @param q a Q. ingo@361: * @param kms a Kms. ingo@361: */ ingo@361: public void add(double w, double q, double kms) { ingo@365: this.w.add(w); ingo@365: this.q.add(q); ingo@365: this.kms.add(kms); ingo@361: } ingo@361: ingo@361: ingo@402: public void add(double[] w, double[] q, double[] kms) { ingo@402: this.w.add(w); ingo@402: this.q.add(q); ingo@402: this.kms.add(kms); ingo@402: } ingo@402: ingo@402: ingo@361: /** ingo@367: * Returns the number of triples stored in this data pool. ingo@361: * ingo@367: * @return the number of triples stored in this data pool. ingo@361: */ ingo@367: public int size() { ingo@367: return kms.size(); sascha@366: } sascha@366: sascha@366: /** sascha@366: * This method returns a triple of W, Q and Kms in a single 3dim array. sascha@366: * sascha@366: * @param idx The position of the triple. sascha@366: * @param dst destination array sascha@366: * sascha@366: * @return a triple of [W, Q, Kms] in dst. sascha@366: */ sascha@366: public double[] get(int idx, double [] dst) { sascha@366: dst[0] = w .get(idx); sascha@366: dst[1] = q .get(idx); sascha@366: dst[2] = kms.get(idx); sascha@366: return dst; ingo@361: } ingo@367: ingo@367: ingo@367: public double getKms(int idx) { ingo@367: return kms.get(idx); ingo@367: } ingo@402: ingo@402: ingo@402: public double[] getKms() { ingo@402: return kms.toNativeArray(); ingo@402: } ingo@402: ingo@402: ingo@402: public double[] getWs() { ingo@402: return w.toNativeArray(); ingo@402: } ingo@402: ingo@402: ingo@402: public double[] getQs() { ingo@402: return q.toNativeArray(); ingo@402: } ingo@402: ingo@402: ingo@402: /** ingo@402: * Returns a string that consist of the first and last kilometer. ingo@402: * ingo@402: * @return a string that consist of the first and last kilometer. ingo@402: */ ingo@402: public String toString() { ingo@402: double from = getKms(0); ingo@402: double to = getKms(size()-1); ingo@402: return Double.toString(from) + " - " + Double.toString(to); ingo@402: } ingo@402: ingo@402: ingo@402: /** ingo@402: * Merges the WQKms objects of an incoming 2dim array (table) where the ingo@402: * objects of a column belong together. ingo@402: * ingo@402: * @param toMerge The objects that need to be merged. ingo@402: * ingo@402: * @return an array of merged WQKms objects. ingo@402: */ ingo@402: public static WQKms[] merge(WQKms[][] toMerge) { ingo@402: int num = toMerge[0].length; ingo@402: ingo@402: // TODO IS THE LENGTH CORRECT? ingo@402: WQKms[] merged = new WQKms[num]; ingo@402: for (int i = 0; i < num; i++) { ingo@402: merged[i] = new WQKms(); ingo@402: } ingo@402: ingo@402: for (int i = 0; i < toMerge.length; i++) { ingo@402: WQKms[] tmp = toMerge[i]; ingo@402: ingo@402: for (int j = 0; j < num; j++) { ingo@402: WQKms toAdd = tmp[j]; ingo@402: merged[j].add(toAdd.getWs(), toAdd.getQs(), toAdd.getKms()); ingo@402: } ingo@402: } ingo@402: ingo@402: return merged; ingo@402: } ingo@361: } ingo@361: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :