1 24 package org.ofbiz.base.util.collections; 25 26 import java.io.Serializable ; 27 import java.util.AbstractSet ; 28 import java.util.Iterator ; 29 import java.util.LinkedList ; 30 import java.util.EmptyStackException ; 31 32 40 public class LifoSet extends AbstractSet implements Serializable { 41 42 private LinkedList backedList = new LinkedList (); 44 private int maxCapacity = 10; 45 46 51 public LifoSet() {} 52 53 60 public LifoSet(int capacity) { 61 maxCapacity = capacity; 62 } 63 64 68 public void setCapactity(int capacity) { 69 this.maxCapacity = capacity; 70 } 71 72 75 public int size() { 76 return backedList.size(); 77 } 78 79 82 public boolean add(Object obj) { 83 int index = backedList.indexOf(obj); 84 85 if (index == -1) { 86 backedList.addFirst(obj); 87 while (size() > maxCapacity) 88 backedList.removeLast(); 89 } else { 90 backedList.remove(index); 91 backedList.addFirst(obj); 92 } 93 return true; 94 } 95 96 99 public Iterator iterator() { 100 return backedList.iterator(); 101 } 102 103 105 110 public boolean empty() { 111 if (this.size() == 0) { 112 return true; 113 } 114 return false; 115 } 116 117 122 public void push(Object item) { 123 this.add(item); 124 } 125 126 132 public Object pop() throws EmptyStackException { 133 if (this.size() > 0) { 134 return backedList.removeFirst(); 135 } 136 throw new EmptyStackException (); 137 } 138 139 145 public Object peek() throws EmptyStackException { 146 if (this.size() > 0) { 147 return backedList.getFirst(); 148 } 149 throw new EmptyStackException (); 150 } 151 152 159 public int search(Object item) { 160 int index = backedList.indexOf(item); 161 if (index > -1) { 162 return index + 1; } 164 return -1; 165 } 166 } 167 168 | Popular Tags |