View Javadoc
1   package com.puppycrawl.tools.checkstyle.indentation;
2   
3   import java.util.AbstractMap;
4   import java.util.Set;
5   import java.util.concurrent.ConcurrentMap;
6   
7   import org.antlr.v4.runtime.misc.Nullable;
8   
9   import com.google.common.base.Equivalence;
10  
11  class LocalCache<K, V> extends AbstractMap<K, V> implements ConcurrentMap<K, V> {
12  
13    enum Strength {
14      /*
15       * TODO(kevinb): If we strongly reference the value and aren't loading, we needn't wrap the
16       * value. This could save ~8 bytes per entry.
17       */
18  
19      STRONG {
20        <K, V> Object referenceValue(
21            Segment<K, V> segment, ReferenceEntry<K, V> entry, int value, int weight) {
22          return (weight == 1)
23              ? new StrongValueReference<K, V>(value)
24              : new WeightedStrongValueReference<K, V>(value, weight);
25        }
26  
27        @Override
28        Equivalence<Object> defaultEquivalence() {
29          return Equivalence.equals();
30        }
31  
32        @Override
33       <K, V> ValueReference<K, V> referenceValue(Segment<K, V> segment, 
34      		    ReferenceEntry<K, V> entry, V value, int weight) {
35  
36          return null;
37        }
38      },
39  
40      SOFT {
41        <K, V> Object referenceValue1(
42            Segment<K, V> segment, ReferenceEntry<Integer, Integer> entry, int value, int weight) {
43          return (weight == 1)
44              ? new SoftValueReference<K, V>(segment.valueReferenceQueue, value, entry)
45              : new WeightedSoftValueReference<K, V>();
46        }
47  
48        @Override
49        Equivalence<Object> defaultEquivalence() {
50          return Equivalence.identity();
51        }
52  
53        @Override <K, V> Object referenceValue(Segment<K, V> segment, ReferenceEntry<K, V> entry,
54      		    V value, int weight)
55        {
56          return null;
57        }
58      },
59  
60      WEAK {
61        @Override
62        <K, V> Object referenceValue(
63            Segment<K, V> segment, ReferenceEntry<K, V> entry, V value, int weight) {
64          return (weight == 1)
65              ? new WeakValueReference<K, V>()
66              : new WeightedWeakValueReference<K, V>();
67        }
68  
69        @Override
70        Equivalence<Object> defaultEquivalence() {
71          return Equivalence.identity();
72        }
73      };
74  
75      /**
76       * Creates a reference for the given value according to this value strength.
77       */
78      abstract <K, V> Object referenceValue(
79          Segment<K, V> segment, ReferenceEntry<K, V> entry, V value, int weight);
80  
81      /**
82       * Returns the default equivalence strategy used to compare and hash keys or values referenced
83       * at this strength. This strategy will be used unless the user explicitly specifies an
84       * alternate strategy.
85       */
86      abstract Equivalence<Object> defaultEquivalence();
87    }
88  
89    /**
90     * Creates new entries.
91     */
92    enum EntryFactory {
93      STRONG {
94        <K, V> StrongEntry<K, V> newEntry(
95            Segment<K, V> segment, K key, int hash, @Nullable ReferenceEntry<K, V> next) {
96          return new StrongEntry<K, V>();
97        }
98      },
99      STRONG_ACCESS {
100       <K, V> StrongAccessEntry<K, V> newEntry(
101           Segment<K, V> segment, K key, int hash, @Nullable ReferenceEntry<K, V> next) {
102         return new StrongAccessEntry<K, V>(key, hash, next);
103       }
104 
105       <K, V> ReferenceEntry<K, V> copyEntry(
106           Segment<K, V> segment, ReferenceEntry<K, V> original, ReferenceEntry<K, V> newNext) {
107         return newNext;
108       }
109       {;
110       }
111      },
112     STRONG_WRITE {
113       <K, V> StrongEntry<K, V> newEntry(
114           Segment<K, V> segment, K key, int hash, @Nullable ReferenceEntry<K, V> next) {
115         return new StrongEntry<K, V>();
116       }
117 
118       <K, V> ReferenceEntry<K, V> copyEntry(
119           Segment<K, V> segment, ReferenceEntry<K, V> original, ReferenceEntry<K, V> newNext) {
120         return newNext;
121       }
122     },
123     STRONG_ACCESS_WRITE {
124       <K, V> StrongEntry<K, V> newEntry(
125           Segment<K, V> segment, K key, int hash, @Nullable ReferenceEntry<K, V> next) {
126         return new StrongEntry<K, V>();
127       }
128 
129       <K, V> ReferenceEntry<K, V> copyEntry(
130           Segment<K, V> segment, ReferenceEntry<K, V> original, ReferenceEntry<K, V> newNext) {
131         return newNext;
132       }
133     },
134 
135     WEAK {
136       <K, V> StrongEntry<K, V> newEntry(
137           Segment<K, V> segment, K key, int hash, @Nullable ReferenceEntry<K, V> next) {
138         return new StrongEntry<K, V>();
139       }
140     },
141     WEAK_ACCESS {
142       <K, V> StrongEntry<K, V> newEntry(
143           Segment<K, V> segment, K key, int hash, @Nullable ReferenceEntry<K, V> next) {
144         return new StrongEntry<K, V>();
145       }
146 
147       <K, V> ReferenceEntry<K, V> copyEntry(
148           Segment<K, V> segment, ReferenceEntry<K, V> original, ReferenceEntry<K, V> newNext) {
149         return newNext;
150       }
151     },
152     WEAK_WRITE {
153       <K, V> StrongEntry<K, V> newEntry(
154           Segment<K, V> segment, K key, int hash, @Nullable ReferenceEntry<K, V> next) {
155         return new StrongEntry<K, V>();
156       }
157 
158       <K, V> ReferenceEntry<K, V> copyEntry(
159           Segment<K, V> segment, ReferenceEntry<K, V> original, ReferenceEntry<K, V> newNext) {
160         return newNext;
161       }
162     },
163     WEAK_ACCESS_WRITE {
164       <K, V> StrongEntry<K, V> newEntry(
165           Segment<K, V> segment, K key, int hash, @Nullable ReferenceEntry<K, V> next) {
166         return new StrongEntry<K, V>();
167       }
168 
169       <K, V> ReferenceEntry<K, V> copyEntry(
170           Segment<K, V> segment, ReferenceEntry<K, V> original, ReferenceEntry<K, V> newNext) {
171         return newNext;
172       }
173     };
174   }
175 
176   @Override
177   public Set<java.util.Map.Entry<K, V>> entrySet()
178   {
179     return null;
180   }
181 
182   @Override
183   public V putIfAbsent(K key, V value)
184   {
185     return null;
186   }
187 
188   @Override
189   public boolean remove(Object key, Object value)
190   {
191     return false;
192   }
193 
194   @Override
195   public boolean replace(K key, V oldValue, V newValue)
196   {
197     return false;
198   }
199 
200   @Override
201   public V replace(K key, V value)
202   { 
203     return null;
204   }
205   
206   private static class ValueReference<T1, T2> {
207 
208   }
209 
210   private static class ReferenceEntry<T1, T2> {
211 
212   }
213 
214   private static class Segment<T1, T2> {
215 
216     protected Object valueReferenceQueue;
217 
218   }
219 
220   private static class StrongAccessEntry<T1, T2> {
221 
222     public StrongAccessEntry(T1 key, int hash, ReferenceEntry<T1, T2> next)
223     {
224 
225     }
226 
227   }
228   
229   private static class StrongValueReference<T1, T2> {
230 
231     public StrongValueReference(int value)
232     {
233 
234     }
235 
236   }
237   
238   private static class WeightedStrongValueReference<T1, T2> {
239 
240     public WeightedStrongValueReference(int value, int weight)
241     {
242 
243     }
244 
245   }
246 
247   private static class SoftValueReference<T1, T2> {
248 
249     public SoftValueReference(Object valueReferenceQueue, int value,
250               ReferenceEntry<Integer, Integer> entry)
251     {
252 
253     }
254 
255   }
256 
257   private static class WeightedSoftValueReference<T1, T2> {
258 
259   }
260 
261   private static class WeakValueReference<T1, T2> {
262 
263   }
264 
265   private static class WeightedWeakValueReference<T1, T2> {
266 
267   }
268 
269   private static class StrongEntry<T1, T2> {
270 
271   }
272 }