1 19 package bak.pcj.list; 20 21 import bak.pcj.IntIterator; 22 import bak.pcj.IntCollection; 23 import bak.pcj.AbstractIntCollection; 24 import bak.pcj.hash.DefaultIntHashFunction; 25 import bak.pcj.util.Exceptions; 26 27 39 public abstract class AbstractIntList extends AbstractIntCollection implements IntList { 40 41 42 protected AbstractIntList() { } 43 44 public boolean add(int v) 45 { add(size(), v); return true; } 46 47 53 public void add(int index, int v) 54 { Exceptions.unsupported("add"); } 55 56 57 public boolean addAll(int index, IntCollection c) { 58 if (index < 0 || index > size()) 59 Exceptions.indexOutOfBounds(index, 0, size()); 60 IntIterator 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(int c) { 70 return indexOf(0, c); 71 } 72 73 76 public int indexOf(int index, int c) { 77 IntListIterator i = listIterator(index); 78 while (i.hasNext()) 79 if (i.next() == c) 80 return i.previousIndex(); 81 return -1; 82 } 83 84 public IntIterator iterator() 85 { return listIterator(); } 86 87 public int lastIndexOf(int c) { 88 IntListIterator 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, int c) { 96 IntListIterator i = listIterator(index); 97 while (i.hasPrevious()) 98 if (i.previous() == c) 99 return i.nextIndex(); 100 return -1; 101 } 102 103 public IntListIterator listIterator() 104 { return listIterator(0); } 105 106 public IntListIterator listIterator(final int index) { 107 if (index < 0 || index > size()) 108 Exceptions.indexOutOfBounds(index, 0, size()); 109 110 return new IntListIterator() { 111 private int ptr = index; 112 private int lptr = -1; 113 114 118 public boolean hasNext() { 119 return ptr < size(); 120 } 121 122 public int 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 AbstractIntList.this.removeElementAt(lptr); 133 if (lptr < ptr) ptr--; 134 lptr = -1; 135 } 136 137 141 public void add(int v) { 142 AbstractIntList.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 int 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(int v) { 165 if (lptr == -1) 166 Exceptions.noElementToSet(); 167 AbstractIntList.this.set(lptr, v); 168 } 169 170 }; 171 } 172 173 179 public int 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 IntList)) 186 return false; 187 IntListIterator i1 = listIterator(); 188 IntListIterator i2 = ((IntList)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 IntIterator i = iterator(); 198 while (i.hasNext()) 199 h = (int)(31*h + DefaultIntHashFunction.INSTANCE.hash(i.next())); 200 return h; 201 } 202 203 } | Popular Tags |