]> gitweb.fperrin.net Git - Dictionary.git/blob - src/com/hughes/android/util/PersistentObjectCache.java
Added Apache license.
[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 android.content.Context;
26 import android.util.Log;
27
28 public class PersistentObjectCache {
29
30   private final File dir;
31   private final Map<String, Object> objects = new LinkedHashMap<String, Object>();
32   
33   public synchronized <T> T read(final String filename, final Class<T> resultClass) {
34     try {
35       Object object = (objects.get(filename));
36       if (object != null) {
37         return resultClass.cast(object);
38       }
39       Log.d(getClass().getSimpleName(), "Cache miss.");
40       final File src = new File(dir, filename);
41       if (!src.canRead()) {
42         Log.d(getClass().getSimpleName(), "File empty: " + src);
43         return null;
44       }
45       try {
46         final ObjectInputStream in = new ObjectInputStream(new FileInputStream(src));
47         object = in.readObject();
48         in.close();
49       } catch (Exception e) {
50         Log.e(getClass().getSimpleName(), "Deserialization failed: " + src, e);
51         return null;
52       }
53       objects.put(filename, object);
54       return resultClass.cast(object);
55     } catch (ClassCastException e) {
56       return null;
57     }
58   }
59   
60   public synchronized void write(final String filename, final Object object) {
61     objects.put(filename, object);
62     final File dest = new File(dir, filename);
63     try {
64       final ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(dest));
65       out.writeObject(object);
66       out.close();
67     } catch (Exception e) {
68       Log.e(getClass().getSimpleName(), "Serialization failed: " + dest, e);
69     }
70   }
71
72   private PersistentObjectCache(final Context context) {
73     dir = context.getFilesDir();
74     if (dir == null) {
75       throw new RuntimeException("context.getFilesDir() == null");
76     }
77   }
78   
79   public static synchronized PersistentObjectCache getInstance() {
80     if (instance == null) {
81       throw new RuntimeException("getInstance called before init.");
82     }
83     return instance;
84   }
85
86   public static synchronized PersistentObjectCache init(final Context context) {
87       if (instance == null) {
88         instance = new PersistentObjectCache(context);
89       } else {
90         if (!instance.dir.equals(context.getFilesDir())) {
91           throw new RuntimeException("File dir changed.  old=" + instance.dir + ", new=" + context.getFilesDir());
92         }
93       }
94       return instance;
95   }
96   
97   private static PersistentObjectCache instance = null;
98
99 }