sticky documentation popup [take 1]
[fedora-idea.git] / platform / platform-impl / src / com / intellij / util / PatchedWeakReference.java
blob1401cbda83ee60d5c7b80bc093a6db6eab13b926
1 /*
2 * Copyright 2000-2009 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com.intellij.util;
18 import com.intellij.concurrency.JobScheduler;
19 import com.intellij.openapi.diagnostic.Logger;
20 import org.jetbrains.annotations.TestOnly;
22 import java.lang.ref.ReferenceQueue;
23 import java.lang.ref.WeakReference;
24 import java.util.ArrayList;
25 import java.util.List;
26 import java.util.concurrent.TimeUnit;
28 public class PatchedWeakReference<T> extends WeakReference<T>{
29 private static final Logger LOG = Logger.getInstance("#com.intellij.util.PatchedWeakReference");
31 private static List<PatchedWeakReference<?>> ourRefsList = new ArrayList<PatchedWeakReference<?>>();
32 private static final ReferenceQueue ourQueue = new ReferenceQueue();
34 static {
35 JobScheduler.getScheduler().scheduleAtFixedRate(new Runnable() {
36 public void run() {
37 processQueue();
39 }, (long)500, (long)500, TimeUnit.MILLISECONDS);
42 public PatchedWeakReference(T referent) {
43 super(referent, ourQueue);
44 if(ourRefsList.size() % 100 == 0) {
45 int i = 0;
47 synchronized(ourQueue) {
48 ourRefsList.add(this);
52 /**
53 * public for being accessible from the degenerator as timer stuff does not work.
55 public static void processQueue() {
56 boolean haveClearedRefs = false;
57 while(true){
58 PatchedWeakReference ref = (PatchedWeakReference)ourQueue.poll();
59 if (ref != null){
60 haveClearedRefs = true;
62 else{
63 break;
66 if (!haveClearedRefs) return;
68 synchronized(ourQueue) {
69 ArrayList<PatchedWeakReference<?>> newList = new ArrayList<PatchedWeakReference<?>>(ourRefsList.size()/2+1);
70 for(int i = 0; i < ourRefsList.size(); i++){
71 PatchedWeakReference<?> ref = ourRefsList.get(i);
72 if (ref.get() != null){
73 newList.add(ref);
77 if (LOG.isDebugEnabled()){
78 LOG.info("old size:" + ourRefsList.size());
79 LOG.info("new size:" + newList.size());
81 //System.out.println("old size:" + ourRefsList.size());
82 //System.out.println("new size:" + newList.size());
83 ourRefsList = newList;
87 @TestOnly
88 public static void clearAll() {
89 synchronized (ourQueue) {
90 while (ourQueue.poll() != null);
91 ourRefsList = new ArrayList<PatchedWeakReference<?>>();