1 30 31 32 package org.hsqldb.lib; 33 34 import java.util.NoSuchElementException ; 35 36 43 abstract class BaseList { 44 45 protected int elementCount; 46 47 abstract Object get(int index); 48 49 abstract Object remove(int index); 50 51 abstract boolean add(Object o); 52 53 abstract int size(); 54 55 public boolean contains(Object o) { 56 return find(o) == -1 ? false 57 : true; 58 } 59 60 public boolean remove(Object o) { 61 62 int i = find(o); 63 64 if (i == -1) { 65 return false; 66 } 67 68 remove(i); 69 70 return true; 71 } 72 73 int find(Object o) { 74 75 for (int i = 0, size = size(); i < size; i++) { 76 Object current = get(i); 77 78 if (current == null) { 79 if (o == null) { 80 return i; 81 } 82 } else if (current.equals(o)) { 83 return i; 84 } 85 } 86 87 return -1; 88 } 89 90 public boolean addAll(Collection other) { 91 92 boolean result = false; 93 Iterator it = other.iterator(); 94 95 while (it.hasNext()) { 96 result = true; 97 98 add(it.next()); 99 } 100 101 return result; 102 } 103 104 public boolean isEmpty() { 105 return elementCount == 0; 106 } 107 108 109 public String toString() { 110 111 StringBuffer sb = new StringBuffer (32 + elementCount * 3); 112 113 sb.append("List : size="); 114 sb.append(elementCount); 115 sb.append(' '); 116 sb.append('{'); 117 118 Iterator it = iterator(); 119 120 while (it.hasNext()) { 121 sb.append(it.next()); 122 123 if (it.hasNext()) { 124 sb.append(','); 125 sb.append(' '); 126 } 127 } 128 129 sb.append('}'); 130 131 return sb.toString(); 132 } 133 134 public Iterator iterator() { 135 return new BaseListIterator(); 136 } 137 138 private class BaseListIterator implements Iterator { 139 140 int counter = 0; 141 boolean removed; 142 143 public boolean hasNext() { 144 return counter < elementCount; 145 } 146 147 public Object next() { 148 149 if (counter < elementCount) { 150 removed = false; 151 152 Object returnValue = get(counter); 153 154 counter++; 155 156 return returnValue; 157 } 158 159 throw new NoSuchElementException (); 160 } 161 162 public int nextInt() { 163 throw new NoSuchElementException (); 164 } 165 166 public long nextLong() { 167 throw new NoSuchElementException (); 168 } 169 170 public void remove() { 171 172 if (removed) { 173 throw new NoSuchElementException ("Iterator"); 174 } 175 176 removed = true; 177 178 if (counter != 0) { 179 BaseList.this.remove(counter - 1); 180 181 counter--; 183 return; 184 } 185 186 throw new NoSuchElementException (); 187 } 188 } 189 } 190 | Popular Tags |