RangeMap.java [src/java/d/util] Revision: default  Date:
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package d.util;

/**
 *
 * @author sidereus
 */
public class RangeMap<K extends Comparable<K>, V> {

    protected boolean empty;
    protected K lower, upper;
    protected V value;
    protected RangeMap<K, V> next;
    
    public V get(K key) {
        if (empty) {
            throw new IndexOutOfBoundsException();
        }
        
        if (key.compareTo(upper) > 0) {
            return next.get(key);
        }
        
        return value;
    }
    
    public void put(K lower, K upper, V value) {
        if (empty) {
            this.lower = lower;
            this.upper = upper;
            this.value = value;
            empty = false;
            next = new RangeMap<>();
            return;
        }

        if (lower.compareTo(this.upper) == 0 ||
                lower.compareTo(this.upper) > 0) {
            next.put(lower, upper, value);
        } else {
            throw new UnsupportedOperationException();
        }
    }
    
    public RangeMap() {
        this.empty = true;
    }
}