forked from KNEMC/KLALB
97 lines
2.2 KiB
Java
97 lines
2.2 KiB
Java
package org.kne.cloud.network.congestion;
|
|
|
|
import java.util.Iterator;
|
|
import java.util.Objects;
|
|
import java.util.concurrent.atomic.AtomicInteger;
|
|
import java.util.concurrent.atomic.AtomicLong;
|
|
import java.util.concurrent.atomic.AtomicReferenceArray;
|
|
|
|
public class SendByteSlidingWindow<E> implements Iterable<E>{
|
|
private Object[] array;
|
|
private volatile int windowSize ;
|
|
private volatile long windowPosition;
|
|
|
|
public int getWindowSize() {
|
|
return windowSize;
|
|
}
|
|
|
|
public void setWindowSize(int windowSize) {
|
|
this.windowSize=windowSize;
|
|
}
|
|
|
|
public long getWindowPosition() {
|
|
return windowPosition;
|
|
}
|
|
|
|
public void setWindowPosition(long windowPosition) {
|
|
this.windowPosition=windowPosition;
|
|
}
|
|
|
|
public SendByteSlidingWindow(int capacity,int windowSize){
|
|
if(windowSize>capacity) {
|
|
throw new IllegalArgumentException("windowSize>capacity!");
|
|
}
|
|
array= new Object[capacity];
|
|
this.windowSize=windowSize;
|
|
}
|
|
|
|
public boolean checkPush() {
|
|
if(array[calcPosition(windowPosition-windowSize)]==null ) {
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public void push(E object) {
|
|
|
|
array[calcPosition(windowPosition)]= object;
|
|
}
|
|
|
|
public E remove(long number) {
|
|
if(number<windowPosition&&number>=windowPosition-windowSize) {
|
|
int indp=calcPosition(number);
|
|
E old=(E) array[indp];
|
|
array[indp]=null;
|
|
return old;
|
|
}else {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public boolean isEmpty() {
|
|
for (long i = windowPosition-windowSize; i < windowPosition; i++) {
|
|
if(array[calcPosition(i)]!=null) {
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
public E get(long number) {
|
|
if(number<windowPosition&&number>=windowPosition-windowSize) {
|
|
return (E) array[calcPosition(number)];
|
|
}else {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public Iterator<E> iterator() {
|
|
return new Iterator<E>() {
|
|
private long pointer=windowPosition-windowSize;
|
|
@Override
|
|
public boolean hasNext() {
|
|
return pointer<windowPosition;
|
|
}
|
|
|
|
@Override
|
|
public E next() {
|
|
return (E) array[calcPosition(pointer++)];
|
|
}
|
|
};
|
|
}
|
|
private int calcPosition(long number) {
|
|
return (int) ((number+array.length)%array.length);
|
|
}
|
|
}
|