1 4 package com.tc.stats; 5 6 import java.util.LinkedList ; 7 8 11 public class LossyStack { 12 13 private final LinkedList data = new LinkedList (); 14 private final int maxDepth; 15 16 public LossyStack(int depth) { 17 if (depth < 1) { throw new IllegalArgumentException ("stack depth must be greater than or equal to 1"); } 18 this.maxDepth = depth; 19 } 20 21 public synchronized void push(Object obj) { 22 data.addFirst(obj); 25 if (data.size() > maxDepth) { 26 data.removeLast(); 27 } 28 } 29 30 public synchronized Object pop() { 31 if (data.isEmpty()) { throw new IllegalStateException ("stack empty"); } 32 return data.removeFirst(); 33 } 34 35 public synchronized Object [] toArray(Object [] type) { 36 return data.toArray(type); 37 } 38 39 public synchronized Object peek() { 40 if (data.isEmpty()) { return null; } 41 return data.getFirst(); 42 } 43 44 public synchronized boolean isEmtpy() { 45 return data.isEmpty(); 46 } 47 48 public synchronized int depth() { 49 return data.size(); 50 } 51 52 } 53 | Popular Tags |