root / branches / v2_0_0_prep / libraries / libTools / src / org / gvsig / tools / dispose / impl / WeakReferencingDisposableManager.java @ 31544
History | View | Annotate | Download (4.55 KB)
1 |
/* gvSIG. Geographic Information System of the Valencian Government
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2007-2008 Infrastructures and Transports Department
|
4 |
* of the Valencian Government (CIT)
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 2
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program; if not, write to the Free Software
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
* MA 02110-1301, USA.
|
20 |
*
|
21 |
*/
|
22 |
|
23 |
/*
|
24 |
* AUTHORS (In addition to CIT):
|
25 |
* 2009 {} {{Task}}
|
26 |
*/
|
27 |
package org.gvsig.tools.dispose.impl; |
28 |
|
29 |
import java.lang.ref.Reference; |
30 |
import java.lang.ref.ReferenceQueue; |
31 |
import java.lang.ref.WeakReference; |
32 |
import java.util.Iterator; |
33 |
import java.util.LinkedHashSet; |
34 |
import java.util.Set; |
35 |
|
36 |
import org.gvsig.tools.dispose.Disposable; |
37 |
import org.gvsig.tools.dispose.DisposableManager; |
38 |
import org.gvsig.tools.exception.BaseException; |
39 |
|
40 |
/**
|
41 |
* Default implementation for the {@link DisposableManager}.
|
42 |
*
|
43 |
* @author 2009- <a href="cordinyana@gvsig.org">C?sar Ordi?ana</a> - gvSIG team
|
44 |
*/
|
45 |
public class WeakReferencingDisposableManager implements DisposableManager { |
46 |
|
47 |
private static final int MAX_QUEUE_ITERATIONS = 10; |
48 |
|
49 |
private Set boundDisposables; |
50 |
|
51 |
private ReferenceQueue referenceQueue; |
52 |
|
53 |
private final int maxQueueIterations; |
54 |
|
55 |
public WeakReferencingDisposableManager() {
|
56 |
this(MAX_QUEUE_ITERATIONS);
|
57 |
} |
58 |
|
59 |
public WeakReferencingDisposableManager(int maxQueueIterations) { |
60 |
this.maxQueueIterations = maxQueueIterations;
|
61 |
boundDisposables = new LinkedHashSet(); |
62 |
referenceQueue = new ReferenceQueue(); |
63 |
} |
64 |
|
65 |
public boolean bind(Disposable disposable) { |
66 |
Iterator iterator = boundDisposables.iterator();
|
67 |
while (iterator.hasNext()) {
|
68 |
Reference ref = (Reference) iterator.next(); |
69 |
if (!ref.isEnqueued()) {
|
70 |
Disposable boundDisposable = (Disposable) ref.get(); |
71 |
// Check for object identity
|
72 |
if (boundDisposable == disposable) {
|
73 |
return false; |
74 |
} |
75 |
} else {
|
76 |
iterator.remove(); |
77 |
} |
78 |
} |
79 |
boolean result =
|
80 |
boundDisposables.add(new WeakReference(disposable, |
81 |
referenceQueue)); |
82 |
disposeQueuedReferences(); |
83 |
return result;
|
84 |
} |
85 |
|
86 |
public boolean release(Disposable disposable) { |
87 |
boolean result = false; |
88 |
Iterator iterator = boundDisposables.iterator();
|
89 |
while (iterator.hasNext()) {
|
90 |
Reference ref = (Reference) iterator.next(); |
91 |
if (!ref.isEnqueued()) {
|
92 |
Disposable boundDisposable = (Disposable) ref.get(); |
93 |
// Check for object identity
|
94 |
if (boundDisposable == disposable) {
|
95 |
iterator.remove(); |
96 |
boundDisposable.dispose(); |
97 |
ref.clear(); |
98 |
result = true;
|
99 |
break;
|
100 |
} |
101 |
} else {
|
102 |
iterator.remove(); |
103 |
} |
104 |
} |
105 |
|
106 |
releaseQueuedReferences(); |
107 |
disposeQueuedReferences(); |
108 |
return result;
|
109 |
} |
110 |
|
111 |
public void releaseAll() throws BaseException { |
112 |
Iterator iterator = boundDisposables.iterator();
|
113 |
boundDisposables = new LinkedHashSet(); |
114 |
while (iterator.hasNext()) {
|
115 |
Reference ref = (Reference) iterator.next(); |
116 |
if (!ref.isEnqueued()) {
|
117 |
Disposable disposable = (Disposable) ref.get(); |
118 |
disposable.dispose(); |
119 |
ref.clear(); |
120 |
} |
121 |
iterator.remove(); |
122 |
} |
123 |
disposeQueuedReferences(); |
124 |
} |
125 |
|
126 |
public int getBoundDisposableCount() { |
127 |
releaseQueuedReferences(); |
128 |
disposeQueuedReferences(); |
129 |
return boundDisposables.size();
|
130 |
} |
131 |
|
132 |
public Set getBoundDisposables() { |
133 |
Set boundDispoToReturn = new LinkedHashSet(boundDisposables.size()); |
134 |
Iterator iterator = boundDisposables.iterator();
|
135 |
while (iterator.hasNext()) {
|
136 |
Reference ref = (Reference) iterator.next(); |
137 |
if (!ref.isEnqueued()) {
|
138 |
boundDispoToReturn.add(ref.get()); |
139 |
} else {
|
140 |
iterator.remove(); |
141 |
} |
142 |
} |
143 |
disposeQueuedReferences(); |
144 |
return boundDispoToReturn;
|
145 |
} |
146 |
|
147 |
private void releaseQueuedReferences() { |
148 |
Iterator iterator = boundDisposables.iterator();
|
149 |
while (iterator.hasNext()) {
|
150 |
Reference ref = (Reference) iterator.next(); |
151 |
if (ref.isEnqueued()) {
|
152 |
iterator.remove(); |
153 |
} |
154 |
} |
155 |
} |
156 |
|
157 |
private void disposeQueuedReferences() { |
158 |
for (int i = 0; i < maxQueueIterations; i++) { |
159 |
Reference ref = referenceQueue.poll();
|
160 |
if (ref == null) { |
161 |
return;
|
162 |
} |
163 |
Disposable disposable = (Disposable) ref.get(); |
164 |
if (disposable != null) { |
165 |
disposable.dispose(); |
166 |
} |
167 |
} |
168 |
} |
169 |
} |