File D:\maven-qcr-plugin-testsunitaires\qcr4java\h2-1.3.170-sources\target\site\qc\lecode\util\CacheSecondLevel.java.html

1 /*
2 * Copyright 2004-2011 H2 Group. Multiple-Licensed under the H2 License,
3 * Version 1.0, and under the Eclipse Public License, Version 1.0
4 * (http://h2database.com/html/license.html).
5 * Initial Developer: Jan Kotek
6 */
7 package org.h2.util;
8
9 import java.util.ArrayList;
10 import java.util.Map;
11
12 /**
13 * Cache which wraps another cache (proxy pattern) and adds caching using map.
14 * This is useful for WeakReference, SoftReference or hard reference cache.
15 */
16 class CacheSecondLevel implements Cache {
17
18 private final Cache baseCache;
19 private final Map<Integer, CacheObject> map;
20
21 CacheSecondLevel(Cache cache, Map<Integer, CacheObject> map) {
22 this.baseCache = cache;
23 this.map = map;
24 }
25
26 public void clear() {
27 map.clear();
28 baseCache.clear();
29 }
30
31 public CacheObject find(int pos) {
32 CacheObject ret = baseCache.find(pos);
33 if (ret == null) {
34 ret = map.get(pos);
35 }
36 return ret;
37 }
38
39 public CacheObject get(int pos) {
40 CacheObject ret = baseCache.get(pos);
41 if (ret == null) {
42 ret = map.get(pos);
43 }
44 return ret;
45 }
46
47 public ArrayList<CacheObject> getAllChanged() {
48 return baseCache.getAllChanged();
49 }
50
51 public int getMaxMemory() {
52 return baseCache.getMaxMemory();
53 }
54
55 public int getMemory() {
56 return baseCache.getMemory();
57 }
58
59 public void put(CacheObject r) {
60 baseCache.put(r);
61 map.put(r.getPos(), r);
62 }
63
64 public boolean remove(int pos) {
65 boolean result = baseCache.remove(pos);
66 result |= map.remove(pos) != null;
67 return result;
68 }
69
70 public void setMaxMemory(int size) {
71 baseCache.setMaxMemory(size);
72 }
73
74 public CacheObject update(int pos, CacheObject record) {
75 CacheObject oldRec = baseCache.update(pos, record);
76 map.put(pos, record);
77 return oldRec;
78 }
79
80 }
81