Android Open Source - makler Cache






From Project

Back to project page makler.

License

The source code is released under:

GNU General Public License

If you think the Android project makler listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package pl.net.newton.Makler.history;
//ww  w .j  a  v a 2 s .  co  m
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

import android.content.Context;
import android.util.Log;

public class Cache {
  private static final String TAG = "Makler";

  private Map<String, CacheEntry> map;

  private int capacity;

  private int validTime;

  private Context ctx;

  private String filePrefix;

  public Cache(String filePrefix, int capacity, int validTime, Context ctx) {
    this.map = new HashMap<String, CacheEntry>(capacity);
    this.capacity = capacity;
    this.validTime = validTime;
    this.ctx = ctx;
    this.filePrefix = filePrefix;
  }

  public synchronized void addEntry(String key, EntryList entry) {
    if (map.size() == capacity) {
      removeOldest();
    }
    CacheEntry cEntry = new CacheEntry(entry);
    map.put(key, cEntry);
    saveToDisk(key);
  }

  public synchronized boolean hasKey(String key) {
    if (!map.containsKey(key)) {
      getFromDisk(key);
    }
    if (!map.containsKey(key)) {
      return false;
    }

    CacheEntry cEntry = map.get(key);
    if ((Calendar.getInstance().getTimeInMillis() - cEntry.calendar.getTimeInMillis()) > (validTime * 1000)) {
      map.remove(key);
      return false;
    }
    return true;
  }

  public synchronized EntryList getEntry(String key) {
    if (!hasKey(key)) {
      return null;
    }
    return map.get(key).entry;
  }

  private void removeOldest() {
    Calendar oldest = null;
    String oldestKey = null;
    for (String key : map.keySet()) {
      CacheEntry entry = map.get(key);
      if (oldest == null || oldest.after(entry.calendar)) {
        oldest = entry.calendar;
        oldestKey = key;
      }
    }
    if (oldestKey != null) {
      map.remove(oldestKey);
    }
  }

  private void saveToDisk(final String entry) {
    if (!map.containsKey(entry)) {
      return;
    }

    new Thread(new Runnable() {
      public void run() {
        try {
          synchronized (map.get(entry)) {
            Log.d(TAG, "saving " + entry + " to disk");
            FileOutputStream fos = ctx.openFileOutput(filePrefix + entry, Context.MODE_PRIVATE);
            ObjectOutputStream oos = new ObjectOutputStream(fos);
            oos.writeObject(map.get(entry));
            oos.close();
            Log.d(TAG, "saved " + entry + " to disk");
          }
        } catch (Exception e) {
          Log.e(TAG, "error in saving history", e);
        }
      }
    }).start();

  }

  private synchronized void getFromDisk(String entry) {
    try {
      Log.d(TAG, "loading " + entry + " from disk");
      File f = ctx.getFileStreamPath(filePrefix + entry);
      if (!f.exists()) {
        Log.d(TAG, entry + " not found");
        return;
      }
      Calendar cal = Calendar.getInstance();
      cal.setTimeInMillis(f.lastModified());
      if ((Calendar.getInstance().getTimeInMillis() - cal.getTimeInMillis()) > (validTime * 1000)) {
        Log.d(TAG, entry + " out of date");
        if (!f.delete()) {
          Log.e(TAG, "Can't delete file " + f.getPath());
        }
        return;
      }

      loadCacheEntry(entry);
      Log.d(TAG, "loaded " + entry);
    } catch (IOException e) {
      Log.e(TAG, "error in loading history", e);
    }
  }

  private void loadCacheEntry(String entry) throws IOException {
    FileInputStream fis = ctx.openFileInput(filePrefix + entry);
    try {
      ObjectInputStream ois = new ObjectInputStream(fis);
      CacheEntry e = (CacheEntry) ois.readObject();
      map.put(entry, e);
    } catch (ClassNotFoundException e) {
      Log.e(TAG, "Can't find class", e);
    } finally {
      fis.close();
    }
  }

  public static class CacheEntry implements Serializable {
    private static final long serialVersionUID = -3188392713226765929L;

    private EntryList entry;

    private Calendar calendar;

    public CacheEntry(EntryList entry) {
      this.entry = entry;
      this.calendar = Calendar.getInstance();
    }
  }
}




Java Source Code List

pl.net.newton.Makler.common.Configuration.java
pl.net.newton.Makler.common.DateFormatUtils.java
pl.net.newton.Makler.common.GpwUtils.java
pl.net.newton.Makler.common.LocaleUtils.java
pl.net.newton.Makler.common.NumberFormatUtils.java
pl.net.newton.Makler.db.Constants.java
pl.net.newton.Makler.db.DbHelper.java
pl.net.newton.Makler.db.SqlConnection.java
pl.net.newton.Makler.db.alert.AlertBuilder.java
pl.net.newton.Makler.db.alert.AlertChecker.java
pl.net.newton.Makler.db.alert.AlertValue.java
pl.net.newton.Makler.db.alert.Alert.java
pl.net.newton.Makler.db.alert.AlertsDao.java
pl.net.newton.Makler.db.alert.Event.java
pl.net.newton.Makler.db.alert.Subject.java
pl.net.newton.Makler.db.quote.QuoteField.java
pl.net.newton.Makler.db.quote.Quote.java
pl.net.newton.Makler.db.quote.QuotesDao.java
pl.net.newton.Makler.db.service.SqlProvider.java
pl.net.newton.Makler.db.symbol.SymbolBuilder.java
pl.net.newton.Makler.db.symbol.Symbol.java
pl.net.newton.Makler.db.symbol.SymbolsDb.java
pl.net.newton.Makler.db.wallet.WalletDb.java
pl.net.newton.Makler.db.wallet.WalletItemBuilder.java
pl.net.newton.Makler.db.wallet.WalletItem.java
pl.net.newton.Makler.gpw.DefaultQuotesReceiver.java
pl.net.newton.Makler.gpw.QuotesReceiver.java
pl.net.newton.Makler.gpw.ex.GpwException.java
pl.net.newton.Makler.gpw.service.QuotesListener.java
pl.net.newton.Makler.gpw.service.QuotesService.java
pl.net.newton.Makler.gpw.service.UpdatingThread.java
pl.net.newton.Makler.history.BossaProvider.java
pl.net.newton.Makler.history.ByteArrayUtils.java
pl.net.newton.Makler.history.Cache.java
pl.net.newton.Makler.history.ChannelTools.java
pl.net.newton.Makler.history.EntryListWithIndexes.java
pl.net.newton.Makler.history.EntryList.java
pl.net.newton.Makler.history.HistoryFilter.java
pl.net.newton.Makler.history.HistoryProvider.java
pl.net.newton.Makler.history.service.HistoryListener.java
pl.net.newton.Makler.history.service.HistoryService.java
pl.net.newton.Makler.httpClient.Connector.java
pl.net.newton.Makler.receivers.QuotesAlarmReceiver.java
pl.net.newton.Makler.receivers.StartupReceiver.java
pl.net.newton.Makler.service.ServiceManager.java
pl.net.newton.Makler.ui.About.java
pl.net.newton.Makler.ui.AbstractActivity.java
pl.net.newton.Makler.ui.Alerts.java
pl.net.newton.Makler.ui.FullScreenGraph.java
pl.net.newton.Makler.ui.Preferences.java
pl.net.newton.Makler.ui.QuoteDetails.java
pl.net.newton.Makler.ui.Quotes.java
pl.net.newton.Makler.ui.Symbols.java
pl.net.newton.Makler.ui.WalletForm.java
pl.net.newton.Makler.ui.WalletItemCalculator.java
pl.net.newton.Makler.ui.Wallet.java
pl.net.newton.Makler.ui.adapter.AlertsAdapter.java
pl.net.newton.Makler.ui.adapter.QuotesAdapter.java
pl.net.newton.Makler.ui.adapter.SymbolsAdapter.java
pl.net.newton.Makler.ui.adapter.WalletAdapter.java
pl.net.newton.Makler.ui.graph.GraphView.java
pl.net.newton.Makler.ui.graph.MaklerGraphicalView.java