2021-01-10 23:37:09 +01:00
|
|
|
/* Copyright (C) 2015-2021 Andreas Shimokawa, Daniel Dakhno, Daniele Gobbetti,
|
2019-12-06 22:49:44 +01:00
|
|
|
Julien Pivotto
|
2017-03-10 14:53:19 +01:00
|
|
|
|
|
|
|
This file is part of Gadgetbridge.
|
|
|
|
|
|
|
|
Gadgetbridge is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU Affero General Public License as published
|
|
|
|
by the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
Gadgetbridge is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU Affero General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Affero General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
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;
|
|
|
|
}
|
|
|
|
|
2017-02-26 00:40:50 +01:00
|
|
|
synchronized 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));
|
|
|
|
}
|
|
|
|
|
2017-02-26 00:40:50 +01:00
|
|
|
synchronized public void remove(int id) {
|
2016-01-09 17:54:17 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-02-26 00:40:50 +01:00
|
|
|
synchronized 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;
|
|
|
|
}
|
2019-10-20 01:42:31 +02:00
|
|
|
|
|
|
|
synchronized public Object lookupByValue(Object value){
|
|
|
|
for (Pair entry : list) {
|
|
|
|
if (value.equals(entry.second)) {
|
|
|
|
return entry.first;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2015-12-14 23:31:31 +01:00
|
|
|
}
|