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

com.calendarfx.util.WeakList Maven / Gradle / Ivy

There is a newer version: 8.5.0
Show newest version
package com.calendarfx.util;

import java.lang.ref.WeakReference;
import java.util.AbstractList;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

/**
 * A simple list wich holds only weak references to the original objects.
 */
public class WeakList extends AbstractList {

    private final ArrayList> items;

    /**
     * Creates new WeakList
     */
    public WeakList() {
        items = new ArrayList<>();
    }

    public WeakList(Collection c) {
        items = new ArrayList();
        addAll(0, c);
    }

    public void add(int index, Object element) {
        items.add(index, new WeakReference(element));
    }

    public Iterator iterator() {
        return new WeakListIterator();
    }

    public int size() {
        removeReleased();
        return items.size();
    }

    public T get(int index) {
        return items.get(index).get();
    }

    private void removeReleased() {
        List> temp = new ArrayList<>(items);
        temp.forEach(ref -> {
            if (ref.get() == null) {
                items.remove(ref);
            }
        });
    }

    private class WeakListIterator implements Iterator {

        private final int n;
        private int i;

        public WeakListIterator() {
            n = size();
            i = 0;
        }

        public boolean hasNext() {
            return i < n;
        }

        public T next() {
            return get(i++);
        }

        public void remove() {
            throw new UnsupportedOperationException();
        }

    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy