view flys-backend/src/main/java/de/intevation/flys/model/Range.java @ 4631:d35d316049e8

A little optimization and handle the case that there are duplicates in the values.
author Sascha L. Teichmann <teichmann@intevation.de>
date Mon, 03 Dec 2012 21:16:15 +0100
parents 697d1faa8217
children 7b1c5fe4ebf3
line wrap: on
line source
package de.intevation.flys.model;

import java.io.Serializable;

import java.math.BigDecimal;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.GeneratedValue;
import javax.persistence.Column;
import javax.persistence.SequenceGenerator;
import javax.persistence.GenerationType;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;

@Entity
@Table(name = "ranges")
public class Range
implements   Serializable
{
    private Integer    id;
    private BigDecimal a;
    private BigDecimal b;

    private River      river;

    public Range() {
    }

    public Range(double a, double b, River river) {
        this(new BigDecimal(a), new BigDecimal(b), river);
    }

    public Range(BigDecimal a, BigDecimal b, River river) {
        this.a     = a;
        this.b     = b;
        this.river = river;
    }

    @Id
    @SequenceGenerator(
        name           = "SEQUENCE_RANGES_ID_SEQ",
        sequenceName   = "RANGES_ID_SEQ",
        allocationSize = 1)
    @GeneratedValue(
        strategy  = GenerationType.SEQUENCE,
        generator = "SEQUENCE_RANGES_ID_SEQ")
    @Column(name = "id")
    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    @Column(name = "a") // FIXME: type mapping needed?
    public BigDecimal getA() {
        return a;
    }

    public void setA(BigDecimal a) {
        this.a = a;
    }

    @Column(name = "b") // FIXME: type mapping needed?
    public BigDecimal getB() {
        return b;
    }

    public void setB(BigDecimal b) {
        this.b = b;
    }

    @OneToOne
    @JoinColumn(name = "river_id")
    public River getRiver() {
        return river;
    }

    public void setRiver(River river) {
        this.river = river;
    }

    public int code() {
        int code = 0;
        if (a != null) code  = 1;
        if (b != null) code |= 2;
        return code;
    }

    public boolean intersects(BigDecimal c) {
        return !(a.compareTo(c) > 0 || b.compareTo(c) < 0);
    }

    public boolean intersects(Range other) {

        int code  = code();
        int ocode = other.code();

        if (code == 0 || ocode == 0) {
            return false;
        }

        switch (code) {
            case 1: // has a
                switch (ocode) {
                    case 1: // has a
                        return a.compareTo(other.a) == 0;
                    case 2: // has b
                        return a.compareTo(other.b) == 0;
                    case 3: // has range
                        return other.intersects(a);
                }
                break;
            case 2: // has b
                switch (ocode) {
                    case 1: // has a
                        return b.compareTo(other.a) == 0;
                    case 2: // has b
                        return b.compareTo(other.b) == 0;
                    case 3: // has range
                        return other.intersects(b);
                }
                break;
            case 3: // has range
                switch (ocode) {
                    case 1: // has a
                        return intersects(other.a);
                    case 2: // has b
                        return intersects(other.b);
                    case 3: // has range
                        return !(other.b.compareTo(a) < 0
                               ||other.a.compareTo(b) > 0);
                }
                break;

        }

        return false;
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org