1 19 package bak.pcj.list; 20 21 import bak.pcj.ShortIterator; 22 import bak.pcj.ShortCollection; 23 import bak.pcj.AbstractShortCollection; 24 import bak.pcj.hash.DefaultShortHashFunction; 25 import bak.pcj.util.Exceptions; 26 27 39 public abstract class AbstractShortList extends AbstractShortCollection implements ShortList { 40 41 42 protected AbstractShortList() { } 43 44 public boolean add(short v) 45 { add(size(), v); return true; } 46 47 53 public void add(int index, short v) 54 { Exceptions.unsupported("add"); } 55 56 57 public boolean addAll(int index, ShortCollection c) { 58 if (index < 0 || index > size()) 59 Exceptions.indexOutOfBounds(index, 0, size()); 60 ShortIterator i = c.iterator(); 61 boolean result = i.hasNext(); 62 while (i.hasNext()) { 63 add(index, i.next()); 64 index++; 65 } 66 return result; 67 } 68 69 public int indexOf(short c) { 70 return indexOf(0, c); 71 } 72 73 76 public int indexOf(int index, short c) { 77 ShortListIterator i = listIterator(index); 78 while (i.hasNext()) 79 if (i.next() == c) 80 return i.previousIndex(); 81 return -1; 82 } 83 84 public ShortIterator iterator() 85 { return listIterator(); } 86 87 public int lastIndexOf(short c) { 88 ShortListIterator i = listIterator(size()); 89 while (i.hasPrevious()) 90 if (i.previous() == c) 91 return i.nextIndex(); 92 return -1; 93 } 94 95 public int lastIndexOf(int index, short c) { 96 ShortListIterator i = listIterator(index); 97 while (i.hasPrevious()) 98 if (i.previous() == c) 99 return i.nextIndex(); 100 return -1; 101 } 102 103 public ShortListIterator listIterator() 104 { return listIterator(0); } 105 106 public ShortListIterator listIterator(final int index) { 107 if (index < 0 || index > size()) 108 Exceptions.indexOutOfBounds(index, 0, size()); 109 110 return new ShortListIterator() { 111 private int ptr = index; 112 private int lptr = -1; 113 114 118 public boolean hasNext() { 119 return ptr < size(); 120 } 121 122 public short next() { 123 if (ptr == size()) 124 Exceptions.endOfIterator(); 125 lptr = ptr++; 126 return get(lptr); 127 } 128 129 public void remove() { 130 if (lptr == -1) 131 Exceptions.noElementToRemove(); 132 AbstractShortList.this.removeElementAt(lptr); 133 if (lptr < ptr) ptr--; 134 lptr = -1; 135 } 136 137 141 public void add(short v) { 142 AbstractShortList.this.add(ptr++, v); 143 lptr = -1; 144 } 145 146 public boolean hasPrevious() { 147 return ptr > 0; 148 } 149 150 public int nextIndex() 151 { return ptr; } 152 153 public short previous() { 154 if (ptr == 0) 155 Exceptions.startOfIterator(); 156 ptr--; 157 lptr = ptr; 158 return get(ptr); 159 } 160 161 public int previousIndex() 162 { return ptr-1; } 163 164 public void set(short v) { 165 if (lptr == -1) 166 Exceptions.noElementToSet(); 167 AbstractShortList.this.set(lptr, v); 168 } 169 170 }; 171 } 172 173 179 public short removeElementAt(int index) 180 { Exceptions.unsupported("removeElementAt"); throw new RuntimeException (); } 181 182 public boolean equals(Object obj) { 183 if (this == obj) 184 return true; 185 if (!(obj instanceof ShortList)) 186 return false; 187 ShortListIterator i1 = listIterator(); 188 ShortListIterator i2 = ((ShortList)obj).listIterator(); 189 while(i1.hasNext() && i2.hasNext()) 190 if (i1.next() != i2.next()) 191 return false; 192 return !(i1.hasNext() || i2.hasNext()); 193 } 194 195 public int hashCode() { 196 int h = 1; 197 ShortIterator i = iterator(); 198 while (i.hasNext()) 199 h = (int)(31*h + DefaultShortHashFunction.INSTANCE.hash(i.next())); 200 return h; 201 } 202 203 } | Popular Tags |