// Decompiled by DJ v2.6.6.51 Copyright 2000 Atanas Neshkov  Date: 6/15/2001 17:43:16
// Home Page : http://members.fortunecity.com/neshkov/dj.html  - Check often for new version!
// Decompiler options: packimports(3) 
// Source File Name:   com/macromedia/generator/app/GenCache

package com.macromedia.generator.app;

import com.macromedia.generator.platform.Mac;
import com.macromedia.generator.utils.Comparer;
import com.macromedia.generator.utils.GenUtils;
import java.text.DateFormat;
import java.util.*;

// Referenced classes of package com.macromedia.generator.app:
//            FlashEnvironment, GenCacheEntry, GenCacheExpireComparator, GenCacheExpireComparator2, 
//            GenLog, CacheLog

public class GenCache
{

    public GenCache(long l, boolean flag, long l1, CacheLog cachelog)
    {
        mIndex = null;
        mCurrentSize = 0L;
        mLog = null;
        F0 = false;
        mMaxSize = 0L;
        mRecycle = true;
        mDefaultExpire = 0L;
        mDynamicMaintanence = true;
        mLog = cachelog;
        logCacheMessage(getResString("CreatingCache"), 1);
        setMaxSize(l);
        setRecycle(flag);
        setDefaultExpire(l1);
        mIndex = new Hashtable();
    }

    protected void dispose()
    {
        purge();
    }

    protected boolean existsCacheEntry(GenCacheEntry gencacheentry)
    {
        if(gencacheentry == null)
            return false;
        return getCacheValue(gencacheentry.key) != null;
    }

    protected void expireCacheEntries()
    {
        if(mIndex == null)
            return;
        for(Enumeration enumeration = mIndex.elements(); enumeration.hasMoreElements();)
        {
            GenCacheEntry gencacheentry = (GenCacheEntry)enumeration.nextElement();
            if(isCacheEntryExpired(gencacheentry))
                removeCacheEntry(gencacheentry);
        }

    }

    protected void finalize()
        throws Throwable
    {
        logCacheMessage(getResString("DeletingCache"), 1);
        dispose();
        mIndex = null;
    }

    protected synchronized long freeCacheItems(int i)
    {
        if(mIndex == null)
            return 0L;
        int j = mIndex.size();
        expireCacheEntries();
        long l = j - mIndex.size();
        if(l > (long)i)
            return l;
        Vector vector = null;
        if(GenUtils.isJava2())
        {
            vector = new Vector(mIndex.values());
            GenCacheExpireComparator2 gencacheexpirecomparator2 = new GenCacheExpireComparator2();
            Collections.sort(vector, gencacheexpirecomparator2);
        } else
        {
            vector = GenUtils.hashtableToVector(mIndex);
            GenCacheExpireComparator gencacheexpirecomparator = new GenCacheExpireComparator();
            gencacheexpirecomparator.Sort(vector);
        }
        Enumeration enumeration = vector.elements();
        for(int k = 0; enumeration.hasMoreElements() && k < i;)
        {
            GenCacheEntry gencacheentry = (GenCacheEntry)enumeration.nextElement();
            removeCacheEntry(gencacheentry);
            k++;
            l++;
        }

        logCacheMessage(getResString("FreedItems") + Long.toString(l), 1);
        return l;
    }

    protected synchronized long freeCacheSpace(long l)
    {
        if(mIndex == null)
            return 0L;
        logCacheMessage(getResString("RequestFree") + Long.toString(l / 1000L) + "K", 3);
        long l1 = mCurrentSize;
        expireCacheEntries();
        long l2 = l1 - mCurrentSize;
        if(l2 > l)
            return l2;
        Vector vector = null;
        if(GenUtils.isJava2())
        {
            vector = new Vector(mIndex.values());
            GenCacheExpireComparator2 gencacheexpirecomparator2 = new GenCacheExpireComparator2();
            Collections.sort(vector, gencacheexpirecomparator2);
        } else
        {
            vector = GenUtils.hashtableToVector(mIndex);
            GenCacheExpireComparator gencacheexpirecomparator = new GenCacheExpireComparator();
            gencacheexpirecomparator.Sort(vector);
        }
        l1 = mCurrentSize;
        for(Enumeration enumeration = vector.elements(); enumeration.hasMoreElements();)
        {
            GenCacheEntry gencacheentry = (GenCacheEntry)enumeration.nextElement();
            removeCacheEntry(gencacheentry);
            l2 = l1 - mCurrentSize;
            if(l2 > l)
            {
                logCacheMessage(getResString("Freed") + Long.toString(l2 / 1000L) + "K", 1);
                return l2;
            }
        }

        if(l2 > 0L)
            logCacheMessage(getResString("Freed") + Long.toString(l2 / 1000L) + "K", 1);
        else
            logCacheMessage(getResString("Freed") + getResString("None"), 1);
        return 0L;
    }

    protected synchronized Object getCacheValue(Object obj)
    {
        if(mIndex == null)
            return null;
        GenCacheEntry gencacheentry = (GenCacheEntry)mIndex.get(obj);
        if(gencacheentry != null)
            if(verifyCacheValue(gencacheentry))
            {
                if(isCacheEntryExpired(gencacheentry))
                {
                    removeCacheEntry(gencacheentry);
                } else
                {
                    logCacheMessage(getResString("FoundValue") + gencacheentry.key, 4);
                    gencacheentry.hits++;
                    logCacheMessage(getResString("HitCount") + gencacheentry.hits, 5);
                    return gencacheentry.value;
                }
            } else
            {
                removeCacheEntry(gencacheentry);
            }
        return null;
    }

    public long getDefaultExpire()
    {
        return mDefaultExpire / 1000L;
    }

    public synchronized boolean getLock()
    {
        return F0;
    }

    public long getMaxSize()
    {
        return mMaxSize / 1000L;
    }

    public boolean getRecycle()
    {
        return mRecycle;
    }

    protected String getResString(String s)
    {
        ResourceBundle resourcebundle = ResourceBundle.getBundle("com.macromedia.generator.app.CacheData", Locale.getDefault());
        return resourcebundle.getString(s);
    }

    public boolean isCacheEnabled()
    {
        Mac mac = Mac.getMac();
        if(mac != null)
            return false;
        if(FlashEnvironment.isLimited())
            return false;
        return mMaxSize != 0L;
    }

    protected boolean isCacheEntryExpired(GenCacheEntry gencacheentry)
    {
        long l = System.currentTimeMillis();
        if(gencacheentry.expires == 0L)
            return false;
        if(gencacheentry.time + gencacheentry.expires < l)
        {
            logCacheMessage(getResString("Expired") + gencacheentry.key, 5);
            return true;
        } else
        {
            return false;
        }
    }

    protected void logCacheMessage(String s, int i)
    {
        logCacheMessage(s, true, i);
    }

    private void logCacheMessage(String s, boolean flag, int i)
    {
        if(mLog != null)
        {
            if(i == -1)
                mLog.setForce(true);
            if(i <= mLog.getLevel())
                mLog.logMessage(s, flag);
            if(i == -1)
                mLog.setForce(false);
        }
    }

    public void logHitCounts()
    {
        if(mIndex == null)
            return;
        Enumeration enumeration = mIndex.elements();
        if(enumeration == null)
            return;
        logCacheMessage(getResString("LogHitCounts"), -1);
        boolean flag;
        for(flag = false; enumeration.hasMoreElements(); flag = true)
        {
            GenCacheEntry gencacheentry = (GenCacheEntry)enumeration.nextElement();
            logCacheMessage((String)gencacheentry.key, -1);
            logCacheMessage(getResString("HitCount") + gencacheentry.hits, -1);
        }

        if(!flag)
            logCacheMessage(getResString("None"), -1);
    }

    public void purge()
    {
        if(mIndex == null)
            return;
        Enumeration enumeration = mIndex.elements();
        if(enumeration == null)
            return;
        GenCacheEntry gencacheentry;
        for(; enumeration.hasMoreElements(); removeCacheEntry(gencacheentry))
            gencacheentry = (GenCacheEntry)enumeration.nextElement();

        logCacheMessage(getResString("Purged"), 2);
    }

    protected synchronized void removeCacheEntry(GenCacheEntry gencacheentry)
    {
        if(mIndex == null)
            return;
        if(F0)
        {
            logCacheMessage(getResString("CacheLockedGet") + gencacheentry.key, 3);
            return;
        }
        if(gencacheentry != null)
        {
            if(mLog != null && mLog.getLevel() >= 3)
            {
                logCacheMessage(getResString("RemovingCacheEntry") + gencacheentry.key, 3);
                logCacheMessage(getResString("Size") + Long.toString(gencacheentry.size / 1000L) + " K," + getResString("Time") + DateFormat.getDateTimeInstance().format(new Date(gencacheentry.time)) + "," + getResString("Expires") + Long.toString(gencacheentry.expires / 1000L) + getResString("Seconds"), 5);
            }
            mCurrentSize -= gencacheentry.size;
            mIndex.remove(gencacheentry.key);
        }
    }

    protected synchronized void removeCacheValue(Object obj)
    {
        if(mIndex == null)
            return;
        GenCacheEntry gencacheentry = (GenCacheEntry)mIndex.get(obj);
        if(gencacheentry != null)
            removeCacheEntry(gencacheentry);
    }

    protected synchronized boolean setCacheEntry(GenCacheEntry gencacheentry)
    {
        if(mIndex == null)
            return false;
        if(F0)
        {
            logCacheMessage(getResString("CacheLockedSet") + gencacheentry.key, 3);
            return false;
        }
        if(existsCacheEntry(gencacheentry))
            return false;
        if(F0(gencacheentry))
        {
            if(mLog != null && mLog.getLevel() >= 3)
            {
                logCacheMessage(getResString("AddingCacheEntry") + gencacheentry.key, 3);
                logCacheMessage(getResString("Size") + Long.toString(gencacheentry.size / 1000L) + " K," + getResString("Time") + DateFormat.getDateTimeInstance().format(new Date(gencacheentry.time)) + "," + getResString("Expires") + Long.toString(gencacheentry.expires / 1000L) + getResString("Seconds"), 5);
            }
            mIndex.put(gencacheentry.key, gencacheentry);
            mCurrentSize += gencacheentry.size;
            return true;
        } else
        {
            return false;
        }
    }

    private boolean F0(GenCacheEntry gencacheentry)
    {
        if(gencacheentry == null || gencacheentry.value == null)
            return false;
        if(gencacheentry.expires == 0L)
            gencacheentry.expires = mDefaultExpire;
        if(mCurrentSize + gencacheentry.size > mMaxSize)
            if(mRecycle && mDynamicMaintanence)
            {
                long l = freeCacheSpace(gencacheentry.size);
                if(l < gencacheentry.size)
                    return false;
            } else
            {
                return false;
            }
        return true;
    }

    protected synchronized boolean setCacheValue(Object obj, Object obj1, long l, long l1)
    {
        GenCacheEntry gencacheentry = new GenCacheEntry();
        gencacheentry.key = obj;
        gencacheentry.value = obj1;
        gencacheentry.size = l;
        gencacheentry.time = System.currentTimeMillis();
        gencacheentry.expires = l1 * 1000L;
        gencacheentry.hits = 0L;
        return setCacheEntry(gencacheentry);
    }

    public void setDefaultExpire(long l)
    {
        if(l < 0L)
            l = 0L;
        mDefaultExpire = l * 1000L;
        logCacheMessage(getResString("SetDefaultExpire") + Long.toString(l) + getResString("Seconds"), 2);
    }

    public synchronized void setLock(boolean flag)
    {
        F0 = flag;
        logCacheMessage(getResString("SetLock") + (new Boolean(flag)).toString(), 2);
    }

    public void setMaxSize(long l)
    {
        if(l < 0L)
            l = 0L;
        l *= 1000L;
        if(mDynamicMaintanence && l < mMaxSize)
            freeCacheSpace(mMaxSize - l);
        mMaxSize = l;
        logCacheMessage(getResString("SetMaxSize") + Long.toString(l / 1000L) + "K", 2);
    }

    public void setRecycle(boolean flag)
    {
        mRecycle = flag;
        logCacheMessage(getResString("SetRecycle") + (new Boolean(mRecycle)).toString(), 2);
    }

    protected boolean verifyCacheValue(GenCacheEntry gencacheentry)
    {
        return gencacheentry.value != null;
    }

    protected Hashtable mIndex;
    protected long mCurrentSize;
    protected CacheLog mLog;
    protected final String kLineBreak = "\r\n";
    private boolean F0;
    protected long mMaxSize;
    protected boolean mRecycle;
    protected long mDefaultExpire;
    protected boolean mDynamicMaintanence;
    private final int F1 = -1;
}