Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Grupo 2.3
- import java.lang.reflect.Array;
- import java.util.Iterator;
- public class Queue<ELEMENT> {
- private final static Integer defaulDimension = 10;
- private int dimension;
- protected Class<?> elementClass;
- protected SimpleLinkedList<ELEMENT> list;
- private int count;
- public Queue() {
- list = new SimpleLinkedList<ELEMENT>();
- this.count = 0;
- dimension = defaulDimension;
- }
- @SuppressWarnings("unchecked")
- public Queue(int dimension, ELEMENT... dummy) {
- if (dummy.length > 0) {
- throw new IllegalArgumentException("No se debe facilitar valores para dummy");
- }
- elementClass = dummy.getClass().getComponentType();
- list = new SimpleLinkedList<ELEMENT>();
- this.count = 0;
- this.dimension = dimension;
- }
- public boolean add(ELEMENT element) {
- if (list.size() >= dimension) {
- throw new IllegalStateException("Cola llena ...");
- }
- list.addLast(element);
- ++count;
- return true;
- }
- public ELEMENT element() {
- if (list.size() <= 0) {
- throw new IllegalStateException("Cola vacía ...");
- }
- return list.getFirst();
- }
- public boolean offer(ELEMENT element) {
- if (list.size() >= dimension) {
- return false;
- }
- list.addLast(element);
- ++count;
- return true;
- }
- public ELEMENT peek() {
- if (this.size() <= 0) {
- return null;
- }
- return list.getFirst();
- }
- public ELEMENT pool() {
- if (this.size() <= 0) {
- return null;
- }
- ELEMENT result = list.removeFirst();
- --count;
- return result;
- }
- public ELEMENT remove() {
- if (this.size() <= 0) {
- throw new IllegalStateException("Cola vacía");
- }
- ELEMENT result = list.removeFirst();
- --count;
- return result;
- }
- public boolean isEmpty() {
- return list.size() <= 0;
- }
- public int size() {
- return list.size();
- }
- public Object[] toArray() {
- ELEMENT [] result = (ELEMENT []) Array.newInstance(this.elementClass, this.size());
- Iterator<ELEMENT> iteratorList = list.iterator();
- int i = 0;
- while(iteratorList.hasNext()) {
- result[i] = iteratorList.next();
- ++i;
- }
- return result;
- }
- public int getDimension() {
- return dimension;
- }
- public void setDimension(int dimension) {
- this.dimension = dimension;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement