2015-12-14 23:31:31 +01:00
|
|
|
package nodomain.freeyourgadget.gadgetbridge.util;
|
|
|
|
|
|
|
|
import android.util.Pair;
|
|
|
|
|
2016-01-09 17:54:17 +01:00
|
|
|
import java.util.Iterator;
|
2015-12-14 23:31:31 +01:00
|
|
|
import java.util.LinkedList;
|
|
|
|
|
2016-01-09 16:07:22 +01:00
|
|
|
public class LimitedQueue {
|
|
|
|
private final int limit;
|
2015-12-14 23:31:31 +01:00
|
|
|
private LinkedList<Pair> list = new LinkedList<>();
|
|
|
|
|
2016-01-09 16:07:22 +01:00
|
|
|
public LimitedQueue(int limit) {
|
|
|
|
this.limit = limit;
|
|
|
|
}
|
|
|
|
|
2016-01-09 17:54:17 +01:00
|
|
|
public void add(int id, Object obj) {
|
2016-01-09 16:07:22 +01:00
|
|
|
if (list.size() > limit - 1) {
|
2015-12-14 23:31:31 +01:00
|
|
|
list.removeFirst();
|
|
|
|
}
|
2016-01-09 17:54:17 +01:00
|
|
|
list.add(new Pair<>(id, obj));
|
|
|
|
}
|
|
|
|
|
|
|
|
public void remove(int id) {
|
|
|
|
for (Iterator<Pair> iter = list.iterator(); iter.hasNext(); ) {
|
|
|
|
Pair pair = iter.next();
|
2016-02-08 06:32:36 +01:00
|
|
|
if ((Integer) pair.first == id) {
|
2016-01-09 17:54:17 +01:00
|
|
|
iter.remove();
|
|
|
|
}
|
|
|
|
}
|
2015-12-14 23:31:31 +01:00
|
|
|
}
|
|
|
|
|
2016-01-09 16:07:22 +01:00
|
|
|
public Object lookup(int id) {
|
2015-12-14 23:31:31 +01:00
|
|
|
for (Pair entry : list) {
|
|
|
|
if (id == (Integer) entry.first) {
|
2016-01-09 16:07:22 +01:00
|
|
|
return entry.second;
|
2015-12-14 23:31:31 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|