chao.java.tools.servicepool.event.WeakList Maven / Gradle / Ivy
package chao.java.tools.servicepool.event;
import java.lang.ref.WeakReference;
import java.util.AbstractCollection;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
/**
* @author luqin
* @since 2019-08-27
*/
public class WeakList extends AbstractCollection implements List {
private List> weakPreferences;
public WeakList() {
weakPreferences = new ArrayList<>();
}
@Override
public Iterator iterator() {
return new Iterator() {
private int index = 0;
private T t;
@Override
public boolean hasNext() {
int start = index;
for (int i = start; i < weakPreferences.size(); i++) {
t = weakPreferences.get(i).get();
if (t == null) {
index ++;
} else {
break;
}
}
return t != null;
}
@Override
public T next() {
index++;
T r = t;
t = null;
return r;
}
};
}
/**
* 整理WeakReference列表
*/
public void tidy() {
List> tidyList = new ArrayList<>();
for (WeakReference weak: weakPreferences) {
if (weak.get() != null) {
tidyList.add(weak);
}
}
weakPreferences = tidyList;
}
@Override
public boolean addAll(int i, Collection extends T> collection) {
throw new UnsupportedOperationException();
}
@Override
public T get(int i) {
return weakPreferences.get(i).get();
}
@Override
public T set(int i, T t) {
return weakPreferences.set(i, new WeakReference<>(t)).get();
}
@Override
public boolean add(T t) {
add(size(), t);
return true;
}
@Override
public void add(int i, T t) {
weakPreferences.add(i, new WeakReference<>(t));
}
public void addIfAbsent(T t) {
if (!contains(t)){
add(t);
}
}
@Override
public T remove(int i) {
return weakPreferences.remove(i).get();
}
@Override
public int indexOf(Object o) {
if (o == null) {
for (int i=0; i listIterator() {
throw new UnsupportedOperationException();
}
@Override
public ListIterator listIterator(int i) {
throw new UnsupportedOperationException();
}
@Override
public List subList(int i, int i1) {
throw new UnsupportedOperationException();
}
@Override
public int size() {
return weakPreferences.size();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy