4 * Copyright 2010 Codist Monk.
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 package net
.sourceforge
.aprog
.tools
;
27 import static net
.sourceforge
.aprog
.tools
.Tools
.*;
29 import java
.lang
.ref
.WeakReference
;
30 import java
.util
.Iterator
;
31 import java
.util
.LinkedList
;
32 import java
.util
.List
;
33 import java
.util
.logging
.Level
;
36 * Instances of this class contain weak references on their elements.
37 * <br>Hard references are created only during iteration by {@link Iterator#hasNext()}
38 * (to ensure that the returned element is never null),
39 * and then removed by {@link Iterator#next()}.
40 * <br>When elements need to be compared, the identity operator {@code ==} is used.
41 * <br>Iteration goes through the elements in the same order they were inserted.
43 * @param <T> The element type
44 * @author codistmonk (creation 2010-10-24)
46 public final class WeakBag
<T
> implements Iterable
<T
> {
48 private final List
<WeakReference
<T
>> references
;
51 this.references
= new LinkedList
<WeakReference
<T
>>();
60 public final void append(final T element
) {
61 this.getReferences().add(new WeakReference
<T
>(element
));
69 public final void remove(final T element
) {
70 for (final Iterator
<T
> i
= this.iterator(); i
.hasNext();) {
71 if (i
.next() == element
) {
80 * <br>Range: {@code [0 .. Integer.MAX_VALUE]}
82 public final int getElementCount() {
85 for (final T element
: this) {
86 if (element
!= null) {
97 * <br>Range: any boolean
99 public final boolean isEmpty() {
100 return 0 == this.getElementCount();
104 public final Iterator
<T
> iterator() {
105 return this.new WeakIterator();
114 final List
<WeakReference
<T
>> getReferences() {
115 return this.references
;
119 * @author codistmonk (creation 2010-10-24)
121 private final class WeakIterator
implements Iterator
<T
> {
123 private final Iterator
<WeakReference
<T
>> iterator
;
127 public WeakIterator() {
128 this.iterator
= WeakBag
.this.getReferences().iterator();
132 public final boolean hasNext() {
133 while (this.next
== null && this.iterator
.hasNext()) {
134 this.next
= this.iterator
.next().get();
136 if (this.next
== null) {
137 this.iterator
.remove();
141 return this.next
!= null;
145 public final T
next() {
146 final T result
= this.next
;
153 public final void remove() {
154 this.iterator
.remove();
160 * {@value} milliseconds.
162 public static final long GARBAGE_COLLECTOR_TIME
= 100L;
165 * Calls {@link System#gc()} and then waits for {@link #GARBAGE_COLLECTOR_TIME} milliseconds.
166 * <br>This method should only be used for testing.
167 * <br>Due to the unpredictable nature of the garbage collector, it may or may not have
168 * finished its work at the end of the wait.
170 public static final void runGarbageCollector() {
174 // Give some time to the garbage collector to do its work
175 Thread
.sleep(GARBAGE_COLLECTOR_TIME
);
176 } catch (final InterruptedException exception
) {
177 getLoggerForThisMethod().log(Level
.WARNING
, null, exception
);