/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package tetris2;

import java.util.Collection;
import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.SortedSet;

/**
 *
 * @author Fmunch
 */
public class MaxSizedPriorityQueue<E> extends PriorityQueue<E>
        implements java.io.Serializable {

  int maxsize;

  MaxSizedPriorityQueue(int maxsize_) {
    super();
    maxsize = maxsize_;
  }

  MaxSizedPriorityQueue(Collection<? extends E> c, int maxsize_) {
    super(c);
    maxsize = maxsize_;
  }

  MaxSizedPriorityQueue(int initialCapacity, int maxsize_) {
    super(initialCapacity);
    maxsize = maxsize_;
  }

  MaxSizedPriorityQueue(int initialCapacity, Comparator<? super E> comparator, int maxsize_) {
    super(initialCapacity, comparator);
    maxsize = maxsize_;
  }

  MaxSizedPriorityQueue(PriorityQueue<? extends E> c, int maxsize_) {
    super(c);
    maxsize = maxsize_;
  }

  MaxSizedPriorityQueue(SortedSet<? extends E> c, int maxsize_) {
    super(c);
    maxsize = maxsize_;
  }

  @Override
  public boolean add(E e) {
    boolean res = super.offer(e);
    if (this.size() > maxsize) {
      this.poll();
    }
    return res;
  }

  @Override
  public boolean offer(E e) {
    boolean res = super.offer(e);
    if (this.size() > maxsize) {
      this.poll();
    }
    return res;
  }

  public int getMaxHeight() {
    return maxsize;
  }

  public static void main(String[] args) {
    System.out.println("Testing MaxSizedPriorityQueue :");
    MaxSizedPriorityQueue<Integer> mspq = new MaxSizedPriorityQueue<Integer>(6);
    mspq.add(new Integer(1));
    mspq.add(new Integer(2));
    mspq.add(new Integer(7));
    mspq.add(new Integer(5));
    mspq.add(new Integer(3));
    mspq.add(new Integer(6));
    mspq.add(new Integer(4));
    mspq.add(new Integer(1));
    Integer[] array = new Integer[0];
    for (Integer i : mspq.toArray(array)) {
      System.out.println(i.intValue());
    }

    while (!mspq.isEmpty()) {
      System.out.println(mspq.poll().intValue());
    }
  }
}


