Package: CloneTrancientMemoryCache

CloneTrancientMemoryCache

nameinstructionbranchcomplexitylinemethod
CloneTrancientMemoryCache()
M: 0 C: 8
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
load(Integer)
M: 45 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 7 C: 0
0%
M: 1 C: 0
0%
store(Object)
M: 48 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 7 C: 0
0%
M: 1 C: 0
0%

Coverage

1: package org.overture.interpreter.runtime;
2:
3: import java.util.HashMap;
4: import java.util.Map;
5:
6: /**
7: * Caching class used for custom serialization of context serialization of transient values
8: *
9: * @author kel
10: */
11: class CloneTrancientMemoryCache<T>
12: {
13:         static class EntryPair<K, V> implements Map.Entry<K, V>
14:         {
15:                 public K key;
16:                 public V value;
17:
18:                 public EntryPair(K key, V value)
19:                 {
20:                         this.key = key;
21:                         this.value = value;
22:                 }
23:
24:                 @Override
25:                 public K getKey()
26:                 {
27:                         return key;
28:                 }
29:
30:                 @Override
31:                 public V getValue()
32:                 {
33:                         return value;
34:                 }
35:
36:                 @Override
37:                 public V setValue(V value)
38:                 {
39:                         return this.value = value;
40:                 }
41:
42:                 @Override
43:                 public String toString()
44:                 {
45:                         return key + " -> " + value;
46:                 }
47:         }
48:
49:         Map<Integer, EntryPair<Integer, T>> map = new HashMap<Integer, EntryPair<Integer, T>>();
50:
51:         public synchronized int store(T o)
52:         {
53:                 Integer key = System.identityHashCode(o);
54:                 EntryPair<Integer, T> val = map.get(key);
55:•                if (val == null)
56:                 {
57:                         val = new EntryPair<Integer, T>(1, o);
58:                         map.put(key, val);
59:                 } else
60:                 {
61:                         val.key++;
62:                 }
63:                 return key;
64:         }
65:
66:         public synchronized T load(Integer key)
67:         {
68:                 EntryPair<Integer, T> val = map.get(key);
69:•                if (val == null)
70:                 {
71:                         throw new RuntimeException("Not possible to decode cashed key");
72:                 } else
73:                 {
74:                         val.key--;
75:                 }
76:
77:•                if (val.key == 0)
78:                 {
79:                         map.remove(key);
80:                 }
81:                 return val.value;
82:         }
83: }