001: /*
002: * Copyright 2006-2007 Pentaho Corporation. All rights reserved.
003: * This software was developed by Pentaho Corporation and is provided under the terms
004: * of the Mozilla Public License, Version 1.1, or any later version. You may not use
005: * this file except in compliance with the license. If you need a copy of the license,
006: * please go to http://www.mozilla.org/MPL/MPL-1.1.txt.
007: *
008: * Software distributed under the Mozilla Public License is distributed on an "AS IS"
009: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
010: * the license for the specific language governing your rights and limitations.
011: *
012: * Additional Contributor(s): Martin Schmid gridvision engineering GmbH
013: */
014: package org.pentaho.reportdesigner.lib.client.commands;
015:
016: import org.jetbrains.annotations.NonNls;
017: import org.jetbrains.annotations.NotNull;
018:
019: import java.lang.ref.WeakReference;
020: import java.util.ArrayList;
021: import java.util.Iterator;
022: import java.util.logging.Level;
023: import java.util.logging.Logger;
024:
025: /**
026: * User: Martin
027: * Date: 18.11.2004
028: * Time: 18:03:33
029: */
030: public class WeakList<T> implements Iterable<T> {
031: @NotNull
032: @NonNls
033: private static final Logger LOG = Logger.getLogger(WeakList.class
034: .getName());
035:
036: private static final boolean DEBUG = false;
037:
038: @NotNull
039: private ArrayList<Ref<T>> arrayList;
040:
041: public WeakList() {
042: arrayList = new ArrayList<Ref<T>>();
043: }
044:
045: public boolean add(@NotNull
046: T elem) {
047: Ref<T> ref = new Ref<T>(elem);
048:
049: if (!arrayList.contains(ref)) {
050: return arrayList.add(ref);
051: } else {
052: return false;
053: }
054: }
055:
056: public boolean remove(@NotNull
057: T elem) {
058: return arrayList.remove(new Ref<T>(elem));
059: }
060:
061: public boolean contains(@NotNull
062: T elem) {
063: Ref<T> ref = new Ref<T>(elem);
064: return arrayList.contains(ref);
065: }
066:
067: @NotNull
068: public Iterator<T> iterator() {
069: ArrayList<T> tempList = new ArrayList<T>();
070: for (Iterator<Ref<T>> iterator = arrayList.iterator(); iterator
071: .hasNext();) {
072: Ref<T> ref = iterator.next();
073: T t = ref.get();
074: if (t == null) {
075: //noinspection ConstantConditions
076: if (DEBUG && LOG.isLoggable(Level.FINE))
077: LOG.log(Level.FINE,
078: "WeakList.getIterator ref.getInfoText() = "
079: + ref.getInfoText());
080: iterator.remove();
081: } else {
082: tempList.add(t);
083: }
084: }
085:
086: return tempList.iterator();
087: }
088:
089: @SuppressWarnings({"EqualsAndHashcode"})
090: private static class Ref<T> extends WeakReference<T> {
091: @NotNull
092: private String infoText;
093:
094: private Ref(@NotNull
095: T o) {
096: super (o);
097:
098: if (DEBUG) {
099: infoText = o.toString();
100: }
101: }
102:
103: @NotNull
104: public String getInfoText() {
105: return infoText;
106: }
107:
108: public boolean equals(@NotNull
109: Object obj) {
110: if (!(obj instanceof Ref)) {
111: return false;
112: }
113:
114: Ref<?> r = (Ref<?>) obj;
115: if (get() != null) {
116: return get().equals(r.get());
117: } else {
118: return r.get() == null;
119: }
120: }
121:
122: }
123: }
|