package org.kne.cloud.network.klalb;

import java.util.AbstractQueue;
import java.util.Collection;
import java.util.Iterator;
import java.util.Queue;
import java.util.Spliterator;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.atomic.AtomicLong;
import java.util.function.Consumer;
import java.util.function.IntFunction;
import java.util.function.Predicate;
import java.util.stream.Stream;

public class SumQueue<T extends Sumable> extends AbstractQueue<T> implements Queue<T> {
	private AbstractQueue<T> abq;
	public SumQueue(AbstractQueue<T> abq) {
		this.abq=abq;
	}
	private AtomicLong sumed=new AtomicLong();
	public long getSumValue() {
		return sumed.get();
	}

	private void resum() {
		AtomicLong l=new AtomicLong();
		forEach((t)->{
			l.addAndGet(t.getValue());
		});
		sumed.set(l.get());
	}

	

	@Override
	public boolean removeIf(Predicate<? super T> filter) {
		boolean removeIf = abq.removeIf(filter);
		resum();
		return removeIf;
	}


	@Override
	public boolean offer(T e) {
		boolean offer = abq.offer(e);
		if(offer)
			sumed.addAndGet(e.getValue());
		return offer;
	}

	@Override
	public T poll() {
		T poll = abq.poll();
		if(poll!=null) {
			sumed.addAndGet(-poll.getValue());
		}
		return poll;
	}

	

	@Override
	public boolean add(T e) {
		boolean add = abq.add(e);
		if(add) {
			sumed.addAndGet(e.getValue());
		}
		return add;
	}

	@Override
	public T remove() {
		T remove = abq.remove();
		if(remove!=null) {
			sumed.addAndGet(-remove.getValue());
		}
		return remove;
	}


	@Override
	public void clear() {
		abq.clear();
		sumed.set(0);
	}

	@Override
	public boolean addAll(Collection<? extends T> c) {
		boolean addAll = abq.addAll(c);
		resum();
		return addAll;
	}

	@Override
	public Iterator<T> iterator() {
		return null;
	}

	

	

	

	

	@Override
	public boolean remove(Object o) {
		boolean b = abq.remove(o);
		if(b)
			sumed.addAndGet(((Sumable)o).getValue());
		return b;
	}

	
	@Override
	public boolean removeAll(Collection<?> c) {
		boolean removeAll = abq.removeAll(c);
		resum();
		return removeAll;
	}

	@Override
	public boolean retainAll(Collection<?> c) {
		boolean retainAll = abq.retainAll(c);
		resum();
		return retainAll;
	}
	
	
	
	
	
	
	
	
	
	
	
	
	

	@Override
	public T element() {
		return (T) abq.element();
	}
	
	@Override
	public T peek() {
		return (T) abq.peek();
	}
	@Override
	public int size() {
		return abq.size();
	}
	
	@Override
	public boolean isEmpty() {
		return abq.isEmpty();
	}

	@Override
	public boolean contains(Object o) {
		return abq.contains(o);
	}
	
	@Override
	public Object[] toArray() {
		return abq.toArray();
	}

	@Override
	public Spliterator<T> spliterator() {
		return abq.spliterator();
	}

	@Override
	public Stream<T> stream() {
		// TODO 自动生成的方法存根
		return abq.stream();
	}

	@Override
	public Stream<T> parallelStream() {
		// TODO 自动生成的方法存根
		return abq.parallelStream();
	}

	@Override
	public void forEach(Consumer<? super T> action) {
		// TODO 自动生成的方法存根
		abq.forEach(action);
	}
	
	@Override
	public boolean containsAll(Collection<?> c) {
		// TODO 自动生成的方法存根
		return abq.containsAll(c);
	}

	@Override
	public <T> T[] toArray(IntFunction<T[]> generator) {
		return (T[]) abq.toArray(generator);
	}
@Override
	public <T> T[] toArray(T[] a) {
		// TODO 自动生成的方法存根
		return (T[]) abq.toArray(a);
	}
	@Override
	public String toString() {
		// TODO 自动生成的方法存根
		return abq.toString();
	}
	
}