[Aprog]
[aprog.git] / Aprog / src / net / sourceforge / aprog / tools / WeakBag.java
blob49bb92db36d35184dec2384b407464cc424250f0
1 /*
2 * The MIT License
3 *
4 * Copyright 2010 Codist Monk.
5 *
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
22 * THE SOFTWARE.
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;
35 /**
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;
50 public WeakBag() {
51 this.references = new LinkedList<WeakReference<T>>();
54 /**
56 * @param element
57 * <br>Not null
58 * <br>Shared
60 public final void append(final T element) {
61 this.getReferences().add(new WeakReference<T>(element));
64 /**
66 * @param element
67 * <br>Maybe null
69 public final void remove(final T element) {
70 for (final Iterator<T> i = this.iterator(); i.hasNext();) {
71 if (i.next() == element) {
72 i.remove();
77 /**
79 * @return
80 * <br>Range: {@code [0 .. Integer.MAX_VALUE]}
82 public final int getElementCount() {
83 int result = 0;
85 for (final T element : this) {
86 if (element != null) {
87 ++result;
91 return result;
94 /**
96 * @return
97 * <br>Range: any boolean
99 public final boolean isEmpty() {
100 return 0 == this.getElementCount();
103 @Override
104 public final Iterator<T> iterator() {
105 return this.new WeakIterator();
110 * @return
111 * <br>Not null
112 * <br>Shared
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;
125 private T next;
127 public WeakIterator() {
128 this.iterator = WeakBag.this.getReferences().iterator();
131 @Override
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;
144 @Override
145 public final T next() {
146 final T result = this.next;
147 this.next = null;
149 return result;
152 @Override
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() {
171 System.gc();
173 try {
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);