1 6 7 11 12 package org.openlaszlo.sc; 13 import java.io.*; 14 import java.util.*; 15 import org.openlaszlo.sc.Instructions.*; 16 17 18 27 public class StackModel extends ArrayList { 28 private static class RemovableArrayList extends ArrayList { 30 public RemovableArrayList(Collection c) { 31 super(c); 32 } 33 34 public void removeRange(int fromIndex, int toIndex) { 35 super.removeRange(fromIndex, toIndex); 36 } 37 } 38 39 RemovableArrayList pushes; 40 41 public StackModel(Collection c) { 42 super(c); 44 this.pushes = new RemovableArrayList(Collections.nCopies(c.size() + 1, null)); 48 } 49 50 public StackModel() { 51 this(Collections.EMPTY_LIST); 52 } 53 54 public void add(int index, Object value) { 55 if (index < 0) index += this.size(); 56 this.pushes.add(index + 1, null); 59 super.add(index, value); 60 } 61 62 public boolean add(Object value) { 63 this.add(this.size(), value); 64 return true; 65 } 66 67 public boolean addAll(int index, Collection c) { 68 if (index < 0) index += this.size(); 69 int l = c.size(); 72 if (index != this.size()) l -= 1; 74 this.pushes.addAll(index + 1, Collections.nCopies(l, null)); 75 return super.addAll(index, c); 76 } 77 78 public boolean addAll(Collection c) { 79 return this.addAll(this.size(), c); 80 } 81 82 public void clear() { 83 this.pushes.clear(); 84 super.clear(); 85 } 86 87 public Object clone() { 88 return new StackModel(this); 89 } 90 91 93 public void ensureCapacity(int minCapacity) { 94 super.ensureCapacity(minCapacity); 95 this.pushes.ensureCapacity(minCapacity + 1); 96 } 97 98 public Object get(int index) { 99 if (index < 0) index += this.size(); 100 return super.get(index); 101 } 102 103 105 107 109 public Object remove(int index) { 110 if (index < 0) index += this.size(); 111 this.pushes.remove(index + 1); 114 return super.remove(index); 115 } 116 117 protected void removeRange(int fromIndex, int toIndex) { 118 int l = this.size(); 119 if (fromIndex < 0) fromIndex += l; 120 if (fromIndex > l) fromIndex = l; 121 if (toIndex < 0) toIndex += l; 122 if (toIndex > l) toIndex = l; 123 this.pushes.removeRange(fromIndex + 1, toIndex + 1); 126 super.removeRange(fromIndex, toIndex); 127 } 128 129 public Object set(int index, Object value) { 130 if (index < 0) index += this.size(); 131 this.pushes.set(index + 1, null); 134 return super.set(index, value); 135 } 136 137 139 public void trimToSize() { 140 this.pushes.trimToSize(); 141 super.trimToSize(); 142 } 143 144 public void notePush(PUSHInstruction instr, List data) { 145 this.pushes.removeRange(this.size(), this.pushes.size()); 148 this.pushes.addAll(this.size(), Collections.nCopies(data.size() + 1, instr)); 149 super.addAll(data); 150 } 151 152 public PUSHInstruction lastPush() { 153 if (this.pushes.size() > this.size()) { 154 return (PUSHInstruction)(this.pushes.get(this.size())); 155 } else { 156 return null; 157 } 158 } 159 160 public int pushDepth(PUSHInstruction instr) { 163 int l = this.size(); 166 List p = this.pushes; 167 for (int i = l - 1; i >= 0; i--) { 169 if (p.get(i) != instr) { 170 return l - (i + 1); 171 } 172 } 173 return l; 174 } 175 176 public String toString() { 177 StringBuffer b = new StringBuffer (); 178 int i = -1; 179 for (ListIterator li = super.listIterator(); li.hasNext(); ) { 180 i = li.nextIndex(); 181 Object v = li.next(); 182 b.append(v != null ? v.toString() : "None"); 183 b.append("("); 184 Object p = this.pushes.get(i); 185 b.append(p != null ? "" + this.pushes.indexOf(p) : "None"); 186 b.append(")"); 187 if (li.hasNext()) b.append(", "); 188 } 189 for (i++; i < this.pushes.size(); i++) { 190 if (i > 0) b.append(", "); 191 b.append("None ("); 192 Object p = this.pushes.get(i); 193 b.append(p != null ? "" + this.pushes.indexOf(p) : "None"); 194 b.append(")"); 195 } 196 return b.toString(); 197 } 198 } 199 200 | Popular Tags |