view artifacts/src/main/java/org/dive4elements/river/utils/KMIndex.java @ 9415:9744ce3c3853

Rework of fixanalysis computation and dWt and WQ facets. Got rid of strange remapping and bitshifting code by explicitely saving the column information and using it in the facets. The facets also put the valid station range into their xml-metadata
author gernotbelger
date Thu, 16 Aug 2018 16:27:53 +0200
parents ddcd52d239cd
children
line wrap: on
line source
/* Copyright (C) 2011, 2012, 2013 by Bundesanstalt für Gewässerkunde
 * Software engineering by Intevation GmbH
 *
 * 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.utils;

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

/** km to value, searchable. Tolerance is at 10cm. */
public class KMIndex<A> implements Serializable, Iterable<KMIndex.Entry<A>> {

    private static final long serialVersionUID = 1L;

    private static final double EPSILON = 1e-4;

    public static class Entry<A> implements Serializable, Comparable<Entry<A>> {
        private static final long serialVersionUID = 1L;

        private final double km;

        private A value;

        public Entry(final double km) {
            this.km = km;
        }

        public Entry(final double km, final A value) {
            this.km = km;
            this.value = value;
        }

        public double getKm() {
            return this.km;
        }

        public A getValue() {
            return this.value;
        }

        @Override
        public int compareTo(final Entry<A> other) {
            final double diff = this.km - other.km;
            if (diff < -EPSILON)
                return -1;
            if (diff > +EPSILON)
                return +1;
            return 0;
        }

        public boolean epsilonEquals(final double otherKm) {
            return Math.abs(this.km - otherKm) < EPSILON;
        }
    }

    private List<Entry<A>> entries;

    public KMIndex() {
        this(10);
    }

    public KMIndex(final int capacity) {
        this.entries = new ArrayList<>(capacity);
    }

    public void add(final double km, final A value) {
        this.entries.add(new Entry<>(km, value));
    }

    public void sort() {
        Collections.sort(this.entries);
    }

    public int size() {
        return this.entries.size();
    }

    public Entry<A> get(final int idx) {
        return this.entries.get(idx);
    }

    /** Return the first entry at km. */
    public Entry<A> search(final double km) {
        for (final Entry<A> entry : this.entries) {
            if (entry.epsilonEquals(km)) {
                return entry;
            }
        }
        return null;
    }

    public Entry<A> binarySearch(final double km) {
        final int index = Collections.binarySearch(this.entries, new Entry<A>(km));
        return index >= 0 ? this.entries.get(index) : null;
    }

    @Override
    public Iterator<Entry<A>> iterator() {
        return this.entries.iterator();
    }
}

http://dive4elements.wald.intevation.org