1 20 package org.apache.mina.util; 21 22 import java.io.Serializable ; 23 import java.util.Arrays ; 24 25 31 public class Stack implements Serializable { 32 private static final long serialVersionUID = 3546919169401434168L; 33 34 private static final int DEFAULT_CAPACITY = 4; 35 36 private Object [] items; 37 38 private int size = 0; 39 40 43 public Stack() { 44 items = new Object [DEFAULT_CAPACITY]; 45 } 46 47 50 public void clear() { 51 Arrays.fill(items, null); 52 size = 0; 53 } 54 55 61 public Object pop() { 62 if (size == 0) { 63 return null; 64 } 65 66 int pos = size - 1; 67 Object ret = items[pos]; 68 items[pos] = null; 69 size--; 70 71 return ret; 72 } 73 74 77 public void push(Object obj) { 78 if (size == items.length) { 79 final int oldLen = items.length; 81 Object [] tmp = new Object [oldLen * 2]; 82 System.arraycopy(items, 0, tmp, 0, size); 83 items = tmp; 84 } 85 86 items[size] = obj; 87 size++; 88 } 89 90 public void remove(Object o) { 91 for (int i = size - 1; i >= 0; i--) { 92 if (items[i] == o) { 93 System.arraycopy(items, i + 1, items, i, size - i - 1); 94 items[size - 1] = null; 95 size--; 96 break; 97 } 98 } 99 } 100 101 107 public Object first() { 108 if (size == 0) { 109 return null; 110 } 111 112 return items[size - 1]; 113 } 114 115 public Object last() { 116 if (size == 0) { 117 return null; 118 } 119 120 return items[0]; 121 } 122 123 126 public boolean isEmpty() { 127 return (size == 0); 128 } 129 130 133 public int size() { 134 return size; 135 } 136 } | Popular Tags |