//# 0 errors, 54 messages
//#
/*
    //#LazyExpiringCacheEntry.java:1:1: class: org.apache.roller.weblogger.util.cache.LazyExpiringCacheEntry
    //#LazyExpiringCacheEntry.java:1:1: method: org.apache.roller.weblogger.util.cache.LazyExpiringCacheEntry.org.apache.roller.weblogger.util.cache.LazyExpiringCacheEntry__static_init
 * Licensed to the Apache Software Foundation (ASF) under one or more
 *  contributor license agreements.  The ASF licenses this file to You
 * under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.  For additional information regarding
 * copyright in this work, please see the NOTICE file in the top level
 * directory of this distribution.
 */

package org.apache.roller.weblogger.util.cache;

import java.io.Serializable;


/**
 * A cache entry that is meant to expire in a lazy fashion.
 *
 * The way to use this class is to wrap the object you want to cache in an
 * instance of this class and store that in your cache.  Then when you want
 * to retrieve this entry you must input a last-expired time which can be
 * compared against the time this entry was cached to determine if the cached
 * entry is "fresh".  If the object is not fresh then we don't return it.
 *
 * This essentially allows us to track when an object is cached and then before
 * we can retrieve that cached object we must compare it with it's last known
 * invalidation time to make sure it hasn't expired.  This is useful because
 * instead of actively purging lots of cached objects from the cache at 
 * invalidation time, we can now be lazy and just invalidate them when we
 * actually try to retrieve the cached object.
 *
 * This is useful for Roller because we will no longer have to iterate through
 * the list of cached objects and inspect the keys to figure out what items to
 * invalidate.  Instead we can just sit back and let the items be invalidated as
 * we try to use them.
 */
public class LazyExpiringCacheEntry implements Serializable {
    
    private Object value = null;
    private long timeCached = -1;
    
    
    public LazyExpiringCacheEntry(Object item) {
    //#LazyExpiringCacheEntry.java:51: method: void org.apache.roller.weblogger.util.cache.LazyExpiringCacheEntry.org.apache.roller.weblogger.util.cache.LazyExpiringCacheEntry(Object)
    //#input(void org.apache.roller.weblogger.util.cache.LazyExpiringCacheEntry(Object)): item
    //#input(void org.apache.roller.weblogger.util.cache.LazyExpiringCacheEntry(Object)): this
    //#output(void org.apache.roller.weblogger.util.cache.LazyExpiringCacheEntry(Object)): this.timeCached
    //#output(void org.apache.roller.weblogger.util.cache.LazyExpiringCacheEntry(Object)): this.value
    //#post(void org.apache.roller.weblogger.util.cache.LazyExpiringCacheEntry(Object)): init'ed(this.timeCached)
    //#post(void org.apache.roller.weblogger.util.cache.LazyExpiringCacheEntry(Object)): this.value == item
    //#post(void org.apache.roller.weblogger.util.cache.LazyExpiringCacheEntry(Object)): init'ed(this.value)
        this.value = item;
        this.timeCached = System.currentTimeMillis();
    }
    //#LazyExpiringCacheEntry.java:54: end of method: void org.apache.roller.weblogger.util.cache.LazyExpiringCacheEntry.org.apache.roller.weblogger.util.cache.LazyExpiringCacheEntry(Object)
    
    
    /**
     * Retrieve the value of this cache entry if it is still "fresh".
     *
     * If the value has expired then we return null.
     */
    public Object getValue(long lastInvalidated) {
        if(this.isInvalid(lastInvalidated)) {
    //#LazyExpiringCacheEntry.java:63: method: Object org.apache.roller.weblogger.util.cache.LazyExpiringCacheEntry.getValue(long)
    //#input(Object getValue(long)): __Descendant_Table[org/apache/roller/weblogger/util/cache/LazyExpiringCacheEntry]
    //#input(Object getValue(long)): __Descendant_Table[others]
    //#input(Object getValue(long)): __Dispatch_Table.isInvalid(J)Z
    //#input(Object getValue(long)): lastInvalidated
    //#input(Object getValue(long)): this
    //#input(Object getValue(long)): this.__Tag
    //#input(Object getValue(long)): this.timeCached
    //#input(Object getValue(long)): this.value
    //#output(Object getValue(long)): return_value
    //#pre[3] (Object getValue(long)): this.__Tag == org/apache/roller/weblogger/util/cache/LazyExpiringCacheEntry
    //#pre[4] (Object getValue(long)): init'ed(this.timeCached)
    //#pre[6] (Object getValue(long)): (soft) init'ed(this.value)
    //#post(Object getValue(long)): return_value == One-of{null, this.value}
    //#post(Object getValue(long)): (soft) init'ed(return_value)
    //#test_vector(Object getValue(long)): this.timeCached - lastInvalidated: {0..27_670_116_110_564_327_423}, {-27_670_116_110_564_327_423..-1}
            return null;
        } else {
            return this.value;
    //#LazyExpiringCacheEntry.java:66: end of method: Object org.apache.roller.weblogger.util.cache.LazyExpiringCacheEntry.getValue(long)
        }
    }
    
    
    /**
     * Determine if this cache entry has expired.
     */
    public boolean isInvalid(long lastInvalidated) {
        
        return (this.timeCached < lastInvalidated);
    //#LazyExpiringCacheEntry.java:76: method: bool org.apache.roller.weblogger.util.cache.LazyExpiringCacheEntry.isInvalid(long)
    //#input(bool isInvalid(long)): lastInvalidated
    //#input(bool isInvalid(long)): this
    //#input(bool isInvalid(long)): this.timeCached
    //#output(bool isInvalid(long)): return_value
    //#pre[3] (bool isInvalid(long)): init'ed(this.timeCached)
    //#post(bool isInvalid(long)): init'ed(return_value)
    //#LazyExpiringCacheEntry.java:76: end of method: bool org.apache.roller.weblogger.util.cache.LazyExpiringCacheEntry.isInvalid(long)
    }

    
    public long getTimeCached() {
        return timeCached;
    //#LazyExpiringCacheEntry.java:81: method: long org.apache.roller.weblogger.util.cache.LazyExpiringCacheEntry.getTimeCached()
    //#input(long getTimeCached()): this
    //#input(long getTimeCached()): this.timeCached
    //#output(long getTimeCached()): return_value
    //#pre[2] (long getTimeCached()): init'ed(this.timeCached)
    //#post(long getTimeCached()): return_value == this.timeCached
    //#post(long getTimeCached()): init'ed(return_value)
    //#LazyExpiringCacheEntry.java:81: end of method: long org.apache.roller.weblogger.util.cache.LazyExpiringCacheEntry.getTimeCached()
    }
    
}
    //#output(org.apache.roller.weblogger.util.cache.LazyExpiringCacheEntry__static_init): __Descendant_Table[org/apache/roller/weblogger/util/cache/LazyExpiringCacheEntry]
    //#output(org.apache.roller.weblogger.util.cache.LazyExpiringCacheEntry__static_init): __Dispatch_Table.getTimeCached()J
    //#output(org.apache.roller.weblogger.util.cache.LazyExpiringCacheEntry__static_init): __Dispatch_Table.getValue(J)Ljava/lang/Object;
    //#output(org.apache.roller.weblogger.util.cache.LazyExpiringCacheEntry__static_init): __Dispatch_Table.isInvalid(J)Z
    //#post(org.apache.roller.weblogger.util.cache.LazyExpiringCacheEntry__static_init): __Descendant_Table[org/apache/roller/weblogger/util/cache/LazyExpiringCacheEntry] == &__Dispatch_Table
    //#post(org.apache.roller.weblogger.util.cache.LazyExpiringCacheEntry__static_init): __Dispatch_Table.getTimeCached()J == &getTimeCached
    //#post(org.apache.roller.weblogger.util.cache.LazyExpiringCacheEntry__static_init): __Dispatch_Table.getValue(J)Ljava/lang/Object; == &getValue
    //#post(org.apache.roller.weblogger.util.cache.LazyExpiringCacheEntry__static_init): __Dispatch_Table.isInvalid(J)Z == &isInvalid
    //#LazyExpiringCacheEntry.java:: end of method: org.apache.roller.weblogger.util.cache.LazyExpiringCacheEntry.org.apache.roller.weblogger.util.cache.LazyExpiringCacheEntry__static_init
    //#LazyExpiringCacheEntry.java:: end of class: org.apache.roller.weblogger.util.cache.LazyExpiringCacheEntry
