package com.wizzardo.tools.cache;

/**
 * @author: wizzardo
 * Date: 2/12/14
 */
public class Holder<K, V> {

    protected V v;
    protected final K k;
    protected volatile boolean done = false;
    protected volatile long validUntil;
    private Cache.TimingsHolder<K, V> timingsHolder;

    public Holder(K k, Cache.TimingsHolder<K, V> timingsHolder) {
        this.k = k;
        this.timingsHolder = timingsHolder;
    }

    public Holder(K k, V v, Cache.TimingsHolder<K, V> timingsHolder) {
        this.v = v;
        this.k = k;
        this.timingsHolder = timingsHolder;
        done = true;
    }

    public V get() {
        if (!done) {
            synchronized (this) {
                while (!done) {
                    try {
                        this.wait();
                    } catch (InterruptedException ignored) {
                    }
                }
            }
        }
        return v;
    }

    void compute(Computable<? super K, ? extends V> c, K k) throws Exception {
        v = c.compute(k);
    }

    void done() {
        synchronized (this) {
            done = true;
            this.notifyAll();
        }
    }

    public void setValidUntil(long validUntil) {
        this.validUntil = validUntil;
    }

    public K getKey() {
        return k;
    }

    Cache.TimingsHolder<K, V> getTimingsHolder() {
        return timingsHolder;
    }

    boolean isRemoved() {
        return timingsHolder == null;
    }

    void setRemoved() {
        timingsHolder = null;
        done = true; // just for hb
    }
}
@@ -1163,23 +1163,16 @@
  %7D%0A%0A    
-public 
 void set
@@ -1238,32 +1238,126 @@
 idUntil;%0A    %7D%0A%0A
+    public boolean isValid() %7B%0A        return System.currentTimeMillis() %3C validUntil;%0A    %7D%0A%0A
     public K get

add isValid