view artifacts/src/main/java/org/dive4elements/river/artifacts/common/AbstractCalculationResult.java @ 9585:aa6ee96071b7

Punkt 6.2.1 Sprünge auf vollen HM
author gernotbelger
date Wed, 09 Jan 2019 18:07:51 +0100
parents 3f49835a00c3
children
line wrap: on
line source
/** Copyright (C) 2017 by Bundesanstalt für Gewässerkunde
 * Software engineering by
 *  Björnsen Beratende Ingenieure GmbH
 *  Dr. Schumacher Ingenieurbüro für Wasser und Umwelt
 *
 * This file is Free Software under the GNU AGPL (>=v3)
 * and comes with ABSOLUTELY NO WARRANTY! Check out the
 * documentation coming with Dive4Elements River for details.
 */
package org.dive4elements.river.artifacts.common;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

import org.apache.commons.collections.Predicate;
import org.apache.commons.collections.functors.TruePredicate;

import gnu.trove.TDoubleArrayList;

/**
 * @author Gernot Belger
 */
public abstract class AbstractCalculationResult implements Serializable {

    private static final long serialVersionUID = 1L;

    private final Collection<ResultRow> rows;

    private final String label;

    public AbstractCalculationResult(final String label, final Collection<ResultRow> rows) {
        this.label = label;
        this.rows = new ArrayList<>(rows);
    }

    public final String getLabel() {
        return this.label;
    }

    public boolean isEmpty() {
        return this.rows.isEmpty();
    }

    public Collection<ResultRow> getRows() {
        return Collections.unmodifiableCollection(this.rows);
    }

    public final double[][] getStationPoints(final IResultType yType) {
        return getPoints(GeneralResultType.station, yType, TruePredicate.INSTANCE);
    }

    /**
     * Return the row with the given station (within the given tolerance) by linear search.
     * Returns <code>null</code>, if no such station was found.
     */
    protected final ResultRow getRowForStation(final double searchStation, final double stationTolerance) {

        for (final ResultRow row : this.rows) {
            final double station = row.getDoubleValue(GeneralResultType.station);
            if (Math.abs(station - searchStation) < stationTolerance)
                return row;
        }

        return null;
    }

    public final double[][] getPoints(final IResultType typeX, final IResultType typeY, final Predicate rowFilter) {

        final TDoubleArrayList xPoints = new TDoubleArrayList(this.rows.size());
        final TDoubleArrayList yPoints = new TDoubleArrayList(this.rows.size());

        for (final ResultRow row : this.rows) {

            if (rowFilter.evaluate(row)) {
                final double station = row.getDoubleValue(typeX);
                final double value = row.getDoubleValue(typeY);

                xPoints.add(station);
                yPoints.add(value);
            }
        }

        return new double[][] { xPoints.toNativeArray(), yPoints.toNativeArray() };
    }

    protected final <TYPE> List<TYPE> getValues(final IResultType type, final Predicate rowFilter) {

        final List<TYPE> values = new ArrayList<>();

        for (final ResultRow row : this.rows) {

            if (rowFilter.evaluate(row)) {
                @SuppressWarnings("unchecked")
                final TYPE value = (TYPE) row.getValue(type);
                values.add(value);
            }
        }

        return values;
    }
}

http://dive4elements.wald.intevation.org