46 lines
1.2 KiB
Java
46 lines
1.2 KiB
Java
package org.kne.cloud.network.klalb;
|
|
|
|
import java.util.Objects;
|
|
import java.util.concurrent.atomic.AtomicInteger;
|
|
import java.util.concurrent.atomic.AtomicLong;
|
|
import java.util.concurrent.atomic.AtomicReferenceArray;
|
|
|
|
public class ReceiveSlidingWindow<E> {
|
|
private AtomicReferenceArray<E> array;
|
|
private AtomicInteger windowSize =new AtomicInteger();
|
|
private AtomicLong windowPosition=new AtomicLong();
|
|
|
|
public int getWindowSize() {
|
|
return windowSize.get();
|
|
}
|
|
|
|
public void setWindowSize(int windowSize) {
|
|
this.windowSize.set(windowSize);
|
|
}
|
|
|
|
public long getWindowPosition() {
|
|
return windowPosition.get();
|
|
}
|
|
|
|
public void setWindowPosition(long windowPosition) {
|
|
this.windowPosition.set(windowPosition);
|
|
}
|
|
|
|
public ReceiveSlidingWindow(int capacity){
|
|
array=new AtomicReferenceArray<E>(capacity);
|
|
}
|
|
|
|
public boolean push(E object) {
|
|
Objects.requireNonNull(object);
|
|
if(array.get((int) ((windowPosition.get()-windowSize.get())%array.length()))==null ) {
|
|
array.set((int) (windowPosition.getAndIncrement()%array.length()), object);
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public E pull(long number) {
|
|
return array.getAndSet((int) (number%array.length()), null);
|
|
}
|
|
}
|