what-the-bank/sources/com/huawei/hms/framework/common/LimitQueue.java

108 lines
3.0 KiB
Java
Raw Normal View History

2024-07-27 18:17:47 +07:00
package com.huawei.hms.framework.common;
import java.util.Collection;
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.concurrent.ConcurrentLinkedQueue;
/* loaded from: classes2.dex */
public class LimitQueue<E> extends ConcurrentLinkedQueue<E> {
private static final String TAG = "LimitQueue";
private static final long serialVersionUID = -4636313759149307798L;
private boolean deduplication;
private int limit;
public LimitQueue(int i) {
this.deduplication = false;
this.limit = i;
}
public LimitQueue(int i, boolean z) {
this.limit = i;
this.deduplication = z;
}
public LimitQueue(Collection<? extends E> collection, boolean z) {
this(collection.size(), z);
addAll(collection);
}
@Override // java.util.concurrent.ConcurrentLinkedQueue, java.util.Queue
public boolean offer(E e) {
if (this.deduplication) {
super.remove(e);
}
if (super.size() >= this.limit) {
super.poll();
}
return super.offer(e);
}
@Override // java.util.concurrent.ConcurrentLinkedQueue, java.util.AbstractQueue, java.util.AbstractCollection, java.util.Collection, java.util.Queue
public boolean add(E e) {
if (this.deduplication) {
super.remove(e);
}
if (super.size() >= this.limit) {
super.poll();
}
return super.add(e);
}
@Override // java.util.concurrent.ConcurrentLinkedQueue, java.util.AbstractQueue, java.util.AbstractCollection, java.util.Collection
public void clear() {
super.clear();
}
@Override // java.util.concurrent.ConcurrentLinkedQueue, java.util.Queue
public E poll() {
return (E) super.poll();
}
@Override // java.util.AbstractQueue, java.util.Queue
public E remove() {
try {
return (E) super.remove();
} catch (NoSuchElementException unused) {
Logger.w(TAG, "remove failed, limitQueue is empty");
return null;
}
}
@Override // java.util.concurrent.ConcurrentLinkedQueue, java.util.AbstractQueue, java.util.AbstractCollection, java.util.Collection
public boolean addAll(Collection<? extends E> collection) {
if (collection.size() > this.limit) {
return false;
}
if (this.deduplication) {
super.removeAll(collection);
}
for (int size = (collection.size() + super.size()) - this.limit; size > 0; size--) {
super.poll();
}
return super.addAll(collection);
}
public E get(int i) {
Iterator<E> it = iterator();
E e = null;
for (int i2 = 0; i2 <= i && it.hasNext(); i2++) {
e = it.next();
}
return e;
}
public E peekLast() {
Iterator<E> it = iterator();
E e = null;
while (it.hasNext()) {
e = it.next();
}
return e;
}
public int getLimit() {
return this.limit;
}
}