All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.wings.util.WeakArrayList Maven / Gradle / Ivy

The newest version!
/*
 * $Id: SOURCEHEADER 1313 2007-09-11 13:25:48Z hengels $
 * (c) Copyright 2007 osbl development team.
 *
 * This file is part of the OSBL (http://concern.sf.net).
 *
 * The OSBL is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2.1
 * of the License, or (at your option) any later version.
 *
 * Please see COPYING for the complete licence.
 */
package org.wings.util;

import java.util.*;
import java.lang.ref.ReferenceQueue;
import java.lang.ref.Reference;
import java.lang.ref.WeakReference;

@SuppressWarnings("ForLoopReplaceableByForEach")
public class WeakArrayList extends AbstractList {
    private final ReferenceQueue garbageCollected = new ReferenceQueue<>();
    private final List> list = new ArrayList<>();

    public WeakArrayList() {
    }

    @Override
    public boolean contains(Object o) {
        try {
            for (Reference aList : list) {
                T t = aList.get();
                if (t == null)
                    continue;
                if (t.equals(o))
                    return true;
            }
        } catch(IndexOutOfBoundsException e) {
            return false;
        }
        return false;
    }

    @Override
    public boolean add(final T obj) {
        updateList();

        return list.add(new WeakReference<>(obj, garbageCollected));
    }

    @Override
    public void add(int index, final T obj) {
        updateList();

        list.add(index, new WeakReference<>(obj, garbageCollected));
    }

    @Override
    public T remove(int index) {
        updateList();

        return list.remove(index).get();
    }

    @Override
    public int size() {
        updateList();

        return list.size();
    }

    @Override
    public T get(int index) {
        updateList();

        try {
            T obj = list.get(index).get();
            while (obj == null) {
                updateList();

                obj = list.get(index).get();
            }

            return obj;
        } catch (IndexOutOfBoundsException e) {
            return null;
        }
    }

    @Override
    public T set(int index, T element) {
        updateList();

        T old = list.get(index).get();
        list.set(index, new WeakReference<>(element, garbageCollected));

        return old;
    }

    @Override
    public  T[] toArray(T[] a) {
        updateList();

        List returnList = new LinkedList<>();
        for(int i=0; i returnList = new LinkedList<>();
        for(int i=0; i iterator() {
        return new Iterator() {
            Iterator> iterator = list.iterator();
            @Override
            public boolean hasNext() {
                return iterator.hasNext();
            }

            @Override
            public T next() {
                return iterator.next().get();
            }

            @Override
            public void remove() {
                iterator.remove();
            }
        };
    }

    @Override
    public ListIterator listIterator() {
        throw new RuntimeException("does not work - weaklists can't be iterated");
    }

    @Override
    public ListIterator listIterator(int index) {
        throw new RuntimeException("does not work - weaklists can't be iterated");
    }

    private final void updateList() {
        while (true) {
            Reference ref = garbageCollected.poll();
            if (ref == null)
                break;

            this.list.remove(ref);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy