]> gitweb.fperrin.net Git - Dictionary.git/blob - src/com/hughes/android/util/PersistentObjectCache.java
About dialog, added pictures, multi word search.
[Dictionary.git] / src / com / hughes / android / util / PersistentObjectCache.java
1 // Copyright 2011 Google Inc. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 package com.hughes.android.util;
16
17 import java.io.File;
18 import java.io.FileInputStream;
19 import java.io.FileOutputStream;
20 import java.io.ObjectInputStream;
21 import java.io.ObjectOutputStream;
22 import java.util.LinkedHashMap;
23 import java.util.Map;
24
25 import com.hughes.android.dictionary.DictionaryApplication;
26
27 import android.app.Application;
28 import android.content.Context;
29 import android.os.Environment;
30 import android.util.Log;
31
32 public class PersistentObjectCache {
33
34   private final File dir;
35   private final Map<String, Object> objects = new LinkedHashMap<String, Object>();
36   
37   public synchronized <T> T read(final String filename, final Class<T> resultClass) {
38     try {
39       Object object = (objects.get(filename));
40       if (object != null) {
41         return resultClass.cast(object);
42       }
43       Log.d(getClass().getSimpleName(), "Cache miss.");
44       final File src = new File(dir, filename);
45       if (!src.canRead()) {
46         Log.d(getClass().getSimpleName(), "File empty: " + src);
47         return null;
48       }
49       try {
50         final ObjectInputStream in = new ObjectInputStream(new FileInputStream(src));
51         object = in.readObject();
52         in.close();
53       } catch (Exception e) {
54         Log.e(getClass().getSimpleName(), "Deserialization failed: " + src, e);
55         return null;
56       }
57       objects.put(filename, object);
58       return resultClass.cast(object);
59     } catch (ClassCastException e) {
60       return null;
61     }
62   }
63   
64   public synchronized void write(final String filename, final Object object) {
65     objects.put(filename, object);
66     final File dest = new File(dir, filename);
67     try {
68       final ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(dest));
69       out.writeObject(object);
70       out.close();
71     } catch (Exception e) {
72       Log.e(getClass().getSimpleName(), "Serialization failed: " + dest, e);
73     }
74   }
75
76   private PersistentObjectCache(final Context context) {
77     final File filesDir = context.getFilesDir();
78     dir = filesDir != null ? filesDir : Environment.getExternalStorageDirectory();
79     if (dir == null) {
80       throw new RuntimeException("context.getFilesDir() == " + context.getFilesDir() + ", Environment.getExternalStorageDirectory()=" + Environment.getExternalStorageDirectory());
81     }
82   }
83   
84   public static synchronized PersistentObjectCache getInstance() {
85     if (instance == null) {
86       throw new RuntimeException("getInstance called before init.");
87     }
88     return instance;
89   }
90
91   public static synchronized PersistentObjectCache init(final Context context) {
92       if (instance == null) {
93         instance = new PersistentObjectCache(context);
94       } else {
95         if (!instance.dir.equals(context.getFilesDir())) {
96           throw new RuntimeException("File dir changed.  old=" + instance.dir + ", new=" + context.getFilesDir());
97         }
98       }
99       return instance;
100   }
101   
102   private static PersistentObjectCache instance = null;
103
104 }