view artifacts/src/main/java/org/dive4elements/river/artifacts/common/AbstractCalculationResult.java @ 9425:3f49835a00c3

Extended CrossSectionFacet so it may fetch different data from within the artifact result. Also allows to have acces to the potentially already computed artifact result via its normal computation cache.
author gernotbelger
date Fri, 17 Aug 2018 15:31:02 +0200
parents e5367900dd6d
children aa6ee96071b7
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 List<TYPE> values = new ArrayList<>();

        for (final ResultRow row : this.rows) {
            @SuppressWarnings("unchecked")
            final TYPE value = (TYPE) row.getValue(type);
            values.add(value);
        }

        return values;
    }
}

http://dive4elements.wald.intevation.org